annotate custom_search/forms.py @ 757:f36864bc919e

Remove NSSR banner from base template.
author Brian Neal <bgneal@gmail.com>
date Sun, 05 Jan 2014 13:06:53 -0600
parents ad53d929281a
children 20a3bf7a6370
rev   line source
bgneal@469 1 """
bgneal@469 2 This module contains custom forms to tailor the Haystack search application to
bgneal@469 3 our needs.
bgneal@469 4
bgneal@469 5 """
bgneal@753 6 import logging
bgneal@753 7
bgneal@469 8 from django import forms
bgneal@753 9 from django.conf import settings
bgneal@469 10 from haystack.forms import ModelSearchForm
bgneal@469 11
bgneal@469 12
bgneal@469 13 MODEL_CHOICES = (
bgneal@469 14 ('forums.topic', 'Forum Topics'),
bgneal@469 15 ('forums.post', 'Forum Posts'),
bgneal@469 16 ('news.story', 'News Stories'),
bgneal@469 17 ('bio.userprofile', 'User Profiles'),
bgneal@469 18 ('weblinks.link', 'Links'),
bgneal@469 19 ('downloads.download', 'Downloads'),
bgneal@469 20 ('podcast.item', 'Podcasts'),
bgneal@469 21 ('ygroup.post', 'Yahoo Group Archives'),
bgneal@469 22 )
bgneal@469 23
bgneal@753 24 logger = logging.getLogger(__name__)
bgneal@753 25
bgneal@469 26
bgneal@469 27 class CustomModelSearchForm(ModelSearchForm):
bgneal@469 28 """
bgneal@469 29 This customized ModelSearchForm allows us to explictly label and order
bgneal@469 30 the model choices.
bgneal@469 31
bgneal@753 32 We also provide "all words", "exact phrase", and "exclude" text input boxes.
bgneal@753 33 Haystack 2.1.0's auto_query() function did not seem to work right so we just
bgneal@753 34 rolled our own.
bgneal@753 35
bgneal@469 36 """
bgneal@753 37 q = forms.CharField(required=False, label='All these words',
bgneal@753 38 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search',
bgneal@753 39 'size': 48}))
bgneal@753 40 exact = forms.CharField(required=False, label='This exact word or phrase',
bgneal@753 41 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search',
bgneal@753 42 'size': 48}))
bgneal@753 43 exclude = forms.CharField(required=False, label='None of these words',
bgneal@753 44 widget=forms.TextInput(attrs={'type': 'search', 'class': 'search',
bgneal@753 45 'size': 48}))
bgneal@469 46
bgneal@469 47 def __init__(self, *args, **kwargs):
bgneal@469 48 super(CustomModelSearchForm, self).__init__(*args, **kwargs)
bgneal@469 49 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES,
bgneal@753 50 label='Search in', widget=forms.CheckboxSelectMultiple)
bgneal@753 51
bgneal@753 52 def clean(self):
bgneal@753 53 if not settings.SEARCH_QUEUE_ENABLED:
bgneal@753 54 raise forms.ValidationError("Our search function is offline for "
bgneal@753 55 "maintenance. Please try again later. "
bgneal@753 56 "We apologize for any inconvenience.")
bgneal@753 57
bgneal@753 58 if not (self.cleaned_data['q'] or self.cleaned_data['exact'] or
bgneal@753 59 self.cleaned_data['exclude']):
bgneal@753 60 raise forms.ValidationError('Please supply some search terms')
bgneal@753 61
bgneal@753 62 return self.cleaned_data
bgneal@753 63
bgneal@753 64 def search(self):
bgneal@753 65 if not self.is_valid():
bgneal@753 66 return self.no_query_found()
bgneal@753 67
bgneal@753 68 logger.info('Search executed: /%s/%s/%s/ in %s',
bgneal@753 69 self.cleaned_data['q'],
bgneal@753 70 self.cleaned_data['exact'],
bgneal@753 71 self.cleaned_data['exclude'],
bgneal@753 72 self.cleaned_data['models'])
bgneal@753 73
bgneal@753 74 sqs = self.searchqueryset
bgneal@753 75
bgneal@753 76 # Note that in Haystack 2.x content is untrusted and is automatically
bgneal@753 77 # auto-escaped for us.
bgneal@753 78 #
bgneal@753 79 # Filter on the q terms; these should be and'ed together:
bgneal@753 80 terms = self.cleaned_data['q'].split()
bgneal@753 81 for term in terms:
bgneal@753 82 sqs = sqs.filter(content=term)
bgneal@753 83
bgneal@753 84 # Exact words or phrases:
bgneal@753 85 if self.cleaned_data['exact']:
bgneal@753 86 sqs = sqs.filter(content__exact=self.cleaned_data['exact'])
bgneal@753 87
bgneal@753 88 # Exclude terms:
bgneal@753 89 terms = self.cleaned_data['exclude'].split()
bgneal@753 90 for term in terms:
bgneal@753 91 sqs = sqs.exclude(content=term)
bgneal@753 92
bgneal@753 93 if self.load_all:
bgneal@753 94 sqs = sqs.load_all()
bgneal@753 95
bgneal@753 96 # Apply model filtering
bgneal@753 97 sqs = sqs.models(*self.get_models())
bgneal@753 98
bgneal@753 99 return sqs