forked from django/djangoproject.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
32 lines (26 loc) · 1.12 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import haystack.forms
from django import forms
from .models import DocumentRelease
# Right now this just does version because we don't really have
# multiple languages. If we get them, we'll need to deal with that.
class DocSearchForm(haystack.forms.SearchForm):
def __init__(self, *args, **kwargs):
super(DocSearchForm, self).__init__(*args, **kwargs)
self.fields['q'].widget = SearchInput()
self.fields['release'] = DocumentReleaseChoiceField(
queryset = DocumentRelease.objects.all().order_by('version'),
initial = DocumentRelease.objects.default(),
empty_label = None,
required = False,
)
def search(self):
sqs = super(DocSearchForm, self).search()
if self.is_valid():
rel = self.cleaned_data['release'] or DocumentRelease.objects.default()
sqs = sqs.filter(lang=rel.lang, version=rel.version)
return sqs
class DocumentReleaseChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.human_version
class SearchInput(forms.TextInput):
input_type = 'search'