PEP8 and pylint cleanups

This commit is contained in:
Roberto Rosario
2011-04-19 20:30:18 -04:00
parent 4a0b9193c9
commit 5528312872
5 changed files with 44 additions and 50 deletions

View File

@@ -6,28 +6,29 @@ from django.db.models import Q
from conf.settings import LIMIT
search_list = {}
def register(model, text, field_list):
if model in search_list:
search_list[model]['fields'].append(field_list)
else:
search_list[model] = {'fields':field_list, 'text':text}
search_list[model] = {'fields': field_list, 'text': text}
#original code from:
#http://www.julienphalip.com/blog/2008/08/16/adding-search-django-site-snap/
def normalize_query(query_string,
findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
normspace=re.compile(r'\s{2,}').sub):
''' Splits the query string in invidual keywords, getting rid of unecessary spaces
and grouping quoted words together.
Example:
>>> normalize_query(' some random words "with quotes " and spaces')
['some', 'random', 'words', 'with quotes', 'and', 'spaces']
'''
return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)]
@@ -35,7 +36,7 @@ def normalize_query(query_string,
def get_query(query_string, terms, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
aims to search keywords within a model by testing the given search fields.
'''
queries = []
for term in terms:
@@ -49,12 +50,12 @@ def get_query(query_string, terms, search_fields):
field_name = field.get('field_name', '')
if field_name:
q = Q(**{'%s__%s' % (field_name, comparison):term})
q = Q(**{'%s__%s' % (field_name, comparison): term})
if or_query is None:
or_query = q
else:
or_query = or_query | q
queries.append(or_query)
return queries
@@ -69,22 +70,22 @@ def perform_search(query_string):
if query_string:
start_time = datetime.datetime.now()
terms = normalize_query(query_string)
for model, data in search_list.items():
queries = get_query(query_string, terms, data['fields'])
model_result_ids = None
for query in queries:
single_result_ids = set(model.objects.filter(query).values_list('pk', flat=True))
#Convert queryset to python set and perform the
#Convert queryset to python set and perform the
#AND operation on the program and not as a query
if model_result_ids == None:
model_result_ids = single_result_ids
else:
model_result_ids &= single_result_ids
result_count += len(model_result_ids)
results = model.objects.in_bulk(list(model_result_ids)[:LIMIT]).values()
results = model.objects.in_bulk(list(model_result_ids)[: LIMIT]).values()
shown_result_count += len(results)
if results:
model_list[data['text']] = results
@@ -92,5 +93,5 @@ def perform_search(query_string):
if result not in flat_list:
flat_list.append(result)
elapsed_time = unicode(datetime.datetime.now() - start_time).split(':')[2]
return model_list, flat_list, shown_result_count, result_count, elapsed_time

View File

@@ -1,7 +1,6 @@
from django import forms
from django import forms
from django.utils.translation import ugettext_lazy as _
class SearchForm(forms.Form):
q = forms.CharField(max_length=128, label=_(u'Search term'))

View File

@@ -1,20 +1,19 @@
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import TemplateSyntaxError, Library, \
VariableDoesNotExist, Node, Variable
from django.core.urlresolvers import reverse
from django.template import Library
from django.utils.translation import ugettext as _
from dynamic_search.forms import SearchForm
register = Library()
@register.inclusion_tag('search_results_subtemplate.html', takes_context=True)
def search_form(context):
context.update({
'form':SearchForm(initial={'q':context.get('query_string', '')}),
'request':context['request'],
'MEDIA_URL':context['MEDIA_URL'],
'form_action':reverse('results'),
'form_title':_(u'Search')
'form': SearchForm(initial={'q': context.get('query_string', '')}),
'request': context['request'],
'MEDIA_URL': context['MEDIA_URL'],
'form_action': reverse('results'),
'form_title': _(u'Search')
})
return context

View File

@@ -1,9 +1,6 @@
from django.conf.urls.defaults import *
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('dynamic_search.views',
url(r'^search/$', 'search', (), 'search'),
url(r'^results/$', 'results', (), 'results'),
)

View File

@@ -4,33 +4,32 @@ from django.utils.translation import ugettext as _
from django.contrib import messages
from django.conf import settings
from api import perform_search
from forms import SearchForm
from conf.settings import SHOW_OBJECT_TYPE
def results(request, form=None):
query_string = ''
context = {}
result_count = 0
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
try:
model_list, flat_list, shown_result_count, total_result_count, elapsed_time = perform_search(query_string)
if shown_result_count != total_result_count:
title = _(u'results with: %(query_string)s (showing only %(shown_result_count)s out of %(total_result_count)s)') % {
'query_string':query_string, 'shown_result_count':shown_result_count,
'total_result_count':total_result_count}
'query_string': query_string, 'shown_result_count': shown_result_count,
'total_result_count': total_result_count}
else:
title = _(u'results with: %s') % query_string
context.update({
'found_entries': model_list,
'object_list':flat_list,
'title':title,
'time_delta':elapsed_time,
'object_list': flat_list,
'title': title,
'time_delta': elapsed_time,
})
except Exception, e:
if settings.DEBUG:
raise
@@ -39,31 +38,30 @@ def results(request, form=None):
else:
context.update({
'found_entries': [],
'object_list':[],
'title':_(u'results'),
'object_list': [],
'title': _(u'results'),
})
context.update({
'query_string':query_string,
'form':form,
'form_title':_(u'Search'),
'hide_header':True,
'form_hide_required_text':True,
'query_string': query_string,
'form': form,
'form_title': _(u'Search'),
'hide_header': True,
'form_hide_required_text': True,
})
if SHOW_OBJECT_TYPE:
context.update({'extra_columns':
[{'name':_(u'type'), 'attribute':lambda x:x._meta.verbose_name[0].upper() + x._meta.verbose_name[1:]}]})
[{'name': _(u'type'), 'attribute': lambda x: x._meta.verbose_name[0].upper() + x._meta.verbose_name[1:]}]})
return render_to_response('search_results.html', context,
context_instance=RequestContext(request))
def search(request):
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
form = SearchForm(initial={'q':query_string})
form = SearchForm(initial={'q': query_string})
return results(request, form=form)
else:
form = SearchForm()