Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prepared search form for multiple languages

  • Loading branch information...
commit cb66da9a4d67649b88f2e5b989dd510b404246e5 1 parent b109f88
@claudep claudep authored
View
15 docs/forms.py
@@ -2,18 +2,19 @@
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):
- initial_rel = kwargs.pop('release', DocumentRelease.objects.current())
- super(DocSearchForm, self).__init__(*args, **kwargs)
+ def __init__(self, data=None, **kwargs):
+ if data and 'release' in data:
+ self.initial_rel = DocumentRelease.objects.get(pk=data['release'])
+ else:
+ self.initial_rel = kwargs.pop('release', DocumentRelease.objects.current())
+ super(DocSearchForm, self).__init__(data=data, **kwargs)
self.fields['q'].widget = SearchInput()
self.fields['release'] = DocumentReleaseChoiceField(
- queryset = DocumentRelease.objects.all().order_by('version'),
- initial = initial_rel,
+ queryset = DocumentRelease.objects.filter(lang=self.initial_rel.lang).order_by('version'),
+ initial = self.initial_rel,
empty_label = None,
required = False,
)
View
10 docs/models.py
@@ -5,8 +5,14 @@
class DocumentReleaseManager(models.Manager):
- def current(self):
- return self.get(is_default=True)
+ def current(self, lang='en'):
+ current = self.get(is_default=True)
+ if lang != 'en':
+ try:
+ return self.get(lang=lang, version=current.version)
+ except DocumentRelease.DoesNotExist:
+ pass
+ return current
def current_version(self):
current_version = cache.get(DocumentRelease.DEFAULT_CACHE_KEY)
View
2  docs/templatetags/docs.py
@@ -11,7 +11,7 @@
def search_form(context, search_form_id='sidebar_search'):
request = context['request']
auto_id = 'id_%s_%%s' % search_form_id
- release = DocumentRelease.objects.get(version=context['version'])
+ release = DocumentRelease.objects.get(version=context['version'], lang=context['lang'])
return {
'form': DocSearchForm(initial=request.GET, auto_id=auto_id, release=release),
'search_form_id': search_form_id,
View
11 docs/views.py
@@ -20,7 +20,7 @@ def index(request):
return redirect(DocumentRelease.objects.current())
def language(request, lang):
- return redirect(DocumentRelease.objects.current())
+ return redirect(DocumentRelease.objects.current(lang))
def stable(request, lang, version, url):
path = request.get_full_path()
@@ -101,10 +101,9 @@ def __init__(self, **kwargs):
def extra_context(self):
# Constuct a context that matches the rest of the doc page views.
- default_release = DocumentRelease.objects.current()
+ current_release = self.form.initial_rel
return {
- 'lang': default_release.lang,
- 'version': default_release.version,
- 'release': default_release,
+ 'lang': current_release.lang,
+ 'version': current_release.version,
+ 'release': current_release,
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.