PEP8 cleanups.
This commit is contained in:
@@ -72,13 +72,15 @@ class SearchModel(object):
|
||||
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.
|
||||
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)]
|
||||
return [
|
||||
normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)
|
||||
]
|
||||
|
||||
def search(self, query_string, user, global_and_search=False):
|
||||
elapsed_time = 0
|
||||
@@ -102,7 +104,9 @@ class SearchModel(object):
|
||||
search_dict[search_field.get_model()]['searches'].append(
|
||||
{
|
||||
'field_name': [search_field.field],
|
||||
'terms': self.normalize_query(query_string.get('q', '').strip())
|
||||
'terms': self.normalize_query(
|
||||
query_string.get('q', '').strip()
|
||||
)
|
||||
}
|
||||
)
|
||||
else:
|
||||
@@ -117,7 +121,9 @@ class SearchModel(object):
|
||||
search_dict[search_field.get_model()]['searches'].append(
|
||||
{
|
||||
'field_name': [search_field.field],
|
||||
'terms': self.normalize_query(query_string[search_field.field])
|
||||
'terms': self.normalize_query(
|
||||
query_string[search_field.field]
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -129,7 +135,9 @@ class SearchModel(object):
|
||||
|
||||
for query_entry in data['searches']:
|
||||
# Fashion a list of queries for a field for each term
|
||||
field_query_list = self.assemble_query(query_entry['terms'], query_entry['field_name'])
|
||||
field_query_list = self.assemble_query(
|
||||
query_entry['terms'], query_entry['field_name']
|
||||
)
|
||||
|
||||
logger.debug('field_query_list: %s', field_query_list)
|
||||
|
||||
@@ -139,20 +147,26 @@ class SearchModel(object):
|
||||
# Get results per search field
|
||||
for query in field_query_list:
|
||||
logger.debug('query: %s', query)
|
||||
term_query_result_set = set(model.objects.filter(query).values_list(data['return_value'], flat=True))
|
||||
term_query_result_set = set(
|
||||
model.objects.filter(query).values_list(
|
||||
data['return_value'], flat=True
|
||||
)
|
||||
)
|
||||
|
||||
# Convert the QuerySet to a Python set and perform the
|
||||
# AND operation on the program and not as a query.
|
||||
# This operation ANDs all the field term results
|
||||
# belonging to a single model, making sure to only include
|
||||
# results in the final field result variable if all the terms
|
||||
# are found in a single field.
|
||||
# results in the final field result variable if all the
|
||||
# terms are found in a single field.
|
||||
if not field_result_set:
|
||||
field_result_set = term_query_result_set
|
||||
else:
|
||||
field_result_set &= term_query_result_set
|
||||
|
||||
logger.debug('term_query_result_set: %s', term_query_result_set)
|
||||
logger.debug(
|
||||
'term_query_result_set: %s', term_query_result_set
|
||||
)
|
||||
logger.debug('field_result_set: %s', field_result_set)
|
||||
|
||||
if global_and_search:
|
||||
@@ -165,24 +179,33 @@ class SearchModel(object):
|
||||
|
||||
result_set = result_set | model_result_set
|
||||
|
||||
elapsed_time = unicode(datetime.datetime.now() - start_time).split(':')[2]
|
||||
elapsed_time = unicode(
|
||||
datetime.datetime.now() - start_time
|
||||
).split(':')[2]
|
||||
|
||||
queryset = self.model.objects.filter(pk__in=list(result_set)[:setting_limit.value])
|
||||
queryset = self.model.objects.filter(
|
||||
pk__in=list(result_set)[:setting_limit.value]
|
||||
)
|
||||
|
||||
if self.permission:
|
||||
try:
|
||||
Permission.check_permissions(user, [self.permission])
|
||||
except PermissionDenied:
|
||||
queryset = AccessControlList.objects.filter_by_access(self.permission, user, queryset)
|
||||
queryset = AccessControlList.objects.filter_by_access(
|
||||
self.permission, user, queryset
|
||||
)
|
||||
|
||||
RecentSearch.objects.add_query_for_user(user, query_string, len(result_set))
|
||||
RecentSearch.objects.add_query_for_user(
|
||||
user, query_string, len(result_set)
|
||||
)
|
||||
|
||||
return queryset, result_set, elapsed_time
|
||||
|
||||
def assemble_query(self, 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.
|
||||
aims to search keywords within a model by testing the given search
|
||||
fields.
|
||||
"""
|
||||
queries = []
|
||||
for term in terms:
|
||||
|
||||
Reference in New Issue
Block a user