Update the RecentSearch model to work with the new SearchModel class
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import urlparse
|
import urlparse
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
@@ -9,8 +11,8 @@ from django.contrib.auth.models import User
|
|||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils.encoding import smart_unicode, smart_str
|
from django.utils.encoding import smart_unicode, smart_str
|
||||||
|
|
||||||
from dynamic_search.managers import RecentSearchManager
|
from .managers import RecentSearchManager
|
||||||
from dynamic_search.api import registered_search_dict
|
from .classes import SearchModel
|
||||||
|
|
||||||
|
|
||||||
class RecentSearch(models.Model):
|
class RecentSearch(models.Model):
|
||||||
@@ -25,24 +27,22 @@ class RecentSearch(models.Model):
|
|||||||
objects = RecentSearchManager()
|
objects = RecentSearchManager()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
|
document_search = SearchModel.get('documents.Document')
|
||||||
|
|
||||||
query_dict = urlparse.parse_qs(urllib.unquote_plus(smart_str(self.query)))
|
query_dict = urlparse.parse_qs(urllib.unquote_plus(smart_str(self.query)))
|
||||||
if 'q' in query_dict:
|
|
||||||
# Is a simple search
|
if self.is_advanced():
|
||||||
display_string = smart_unicode(' '.join(query_dict['q']))
|
|
||||||
else:
|
|
||||||
# Advanced search
|
# Advanced search
|
||||||
advanced_string = []
|
advanced_string = []
|
||||||
for key, value in query_dict.items():
|
for key, value in query_dict.items():
|
||||||
# Get model name
|
search_field = document_search.get_search_field(key)
|
||||||
model, field_name = key.split('__', 1)
|
advanced_string.append(u'%s: %s' % (search_field.label, smart_unicode(' '.join(value))))
|
||||||
model_entry = registered_search_dict.get(model, {})
|
|
||||||
if model_entry:
|
|
||||||
# Find the field name title
|
|
||||||
for model_field in model_entry.get('fields', [{}]):
|
|
||||||
if model_field.get('name') == field_name:
|
|
||||||
advanced_string.append(u'%s: %s' % (model_field.get('title', model_field['name']), smart_unicode(' '.join(value))))
|
|
||||||
|
|
||||||
display_string = u', '.join(advanced_string)
|
display_string = u', '.join(advanced_string)
|
||||||
|
else:
|
||||||
|
# Is a simple search
|
||||||
|
display_string = smart_unicode(' '.join(query_dict['q']))
|
||||||
|
|
||||||
return u'%s (%s)' % (display_string, self.hits)
|
return u'%s (%s)' % (display_string, self.hits)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user