Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated the search manager to not store recent empty queries

  • Loading branch information...
commit 3503ad7ea1024b6a4df3507595d0a193a1b0a54c 1 parent 2ac21bd
@rosarior rosarior authored
Showing with 16 additions and 6 deletions.
  1. +16 −6 apps/dynamic_search/managers.py
View
22 apps/dynamic_search/managers.py
@@ -1,15 +1,25 @@
+import urlparse
from datetime import datetime
from django.db import models
+from django.utils.http import urlencode
from dynamic_search.conf.settings import RECENT_COUNT
class RecentSearchManager(models.Manager):
def add_query_for_user(self, user, query, hits):
- new_recent, created = self.model.objects.get_or_create(user=user, query=query, defaults={'hits': hits})
- new_recent.hits = hits
- new_recent.save()
- to_delete = self.model.objects.filter(user=user)[RECENT_COUNT:]
- for recent_to_delete in to_delete:
- recent_to_delete.delete()
+ parsed_query = urlparse.parse_qs(query)
+ if ('q=' in query) and not parsed_query.get('q'):
+ # Don't store empty simple searches
+ return
+ else:
+ parsed_query = {'q': u' '.join(parsed_query['q'])}
+ if parsed_query:
+ # If the URL query has at least one variable with a value
+ new_recent, created = self.model.objects.get_or_create(user=user, query=urlencode(parsed_query), defaults={'hits': hits})
+ new_recent.hits = hits
+ new_recent.save()
+ to_delete = self.model.objects.filter(user=user)[RECENT_COUNT:]
+ for recent_to_delete in to_delete:
+ recent_to_delete.delete()
Please sign in to comment.
Something went wrong with that request. Please try again.