Permalink
Browse files

add ability to search over tags

  • Loading branch information...
1 parent 30c078e commit a3dd6b209e97f91d56aef15fbc0309a72af182da @denibertovic denibertovic committed Feb 9, 2014
Showing with 19 additions and 2 deletions.
  1. +19 −2 search/forms.py
View
@@ -1,11 +1,21 @@
# -*- coding: utf-8 -*-
+from itertools import chain
+
from django import forms
+from django.db.models import Q
+
+from djorm_expressions.base import SqlExpression
from events.models import Event
from news.models import News
+def flatten(list_of_lists):
+ "Flatten one level of nesting"
+ return chain.from_iterable(list_of_lists)
+
+
class SearchForm(forms.Form):
query = forms.CharField(max_length=64)
@@ -18,7 +28,14 @@ def clean_query(self):
def get_results(self):
news = News.objects.filter(
subject__icontains=self.cleaned_data['query']).order_by('-created_at')
- events = Event.objects.filter(
- title__icontains=self.cleaned_data['query']).order_by('-date')
+
+ query_string_args = self.cleaned_data['query'].split(':')
+
+ if query_string_args[0] == 'tags':
+ tags = map(lambda t: t.strip(), flatten(map(lambda t: t.split(','), query_string_args[1:])))
+ events = Event.objects.where(SqlExpression("tags", "@>", tags))
+ else:
+ events = Event.objects.filter(
+ title__icontains=self.cleaned_data['query']).order_by('-date')
return news, events

0 comments on commit a3dd6b2

Please sign in to comment.