Skip to content

Instantly share code, notes, and snippets.

@grobertson
Created April 28, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grobertson/b9feb943de0a3f6192fc0c7361a5c39c to your computer and use it in GitHub Desktop.
Save grobertson/b9feb943de0a3f6192fc0c7361a5c39c to your computer and use it in GitHub Desktop.
from django import forms
from haystack.forms import SearchForm
from datetime import datetime, timedelta
ORDER_BY_CHOICES = (
('r', "Relevance"),
('d', "Descending"),
('a', "Ascending")
)
DATE_RANGE_CHOICES = (
(0, 'Anytime'),
(24, 'Past 24 hours'),
(168, 'Past week'),
(720, 'Past month'),
(8760, 'Past year')
)
class DDSearchForm(SearchForm):
o = forms.MultipleChoiceField(required=False, widget=forms.Select, choices=ORDER_BY_CHOICES)
d = forms.MultipleChoiceField(required=False, widget=forms.Select, choices=DATE_RANGE_CHOICES)
def search(self):
print "In dd.search.forms.DDSearchForm.search()"
queryset = super(DDSearchForm, self).search()
if not self.is_valid():
print "dd.search.forms.DDSearchForm.search: Returning No Query Found"
return self.no_query_found()
if self.cleaned_data['o'] == "d":
queryset = queryset.order_by('-pub_date')
elif self.cleaned_data['o'] == "a":
queryset = queryset.order_by('pub_date')
'''if self.cleaned_data['d'] > 0:
self.cleaned_data['d']
start_date = datetime.now() - timedelta(days=)
queryset = queryset.filter(pub_date__gte=start_date)'''
return queryset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment