Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

append HTTP_LANGUAGE_ACCEPT languages to the beginning of the languag…

…e choices
  • Loading branch information...
commit 2eda7852a34341011aa969cd8db43433549fde89 1 parent cc2afd0
@sergiocharpineljr sergiocharpineljr authored
Showing with 53 additions and 15 deletions.
  1. +35 −2 ispdb/config/models.py
  2. +18 −13 ispdb/config/views.py
View
37 ispdb/config/models.py
@@ -12,6 +12,7 @@
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import get_language_info
+from django.utils.functional import curry
import ispdb.audit as audit
class Domain(models.Model):
@@ -317,11 +318,21 @@ class Meta:
exclude = ['docurl']
def __init__(self, *args, **kwargs):
+ http_accept_language = kwargs.pop('http_accept_language', '')
super(DocURLDescForm, self).__init__(*args, **kwargs)
self.fields['description'].widget.attrs.update({'rows': 2, 'cols': 20})
+ choices = []
+ # add HTTP_ACCEPTED_LANG first
+ if http_accept_language:
+ codes = re.split(';|,', http_accept_language)
+ for code in codes:
+ try:
+ li = get_language_info(code)
+ except:
+ continue
+ choices.append((code, li['name'] + ' - ' + li['name_local']))
# Redefine our choices, so we can add the translated language names and
# sort the list by language name
- choices = []
choices.append(self.fields['language'].choices[0])
langs = self.fields['language'].choices[1:]
langs.sort(key=lambda l: l[1].lower())
@@ -407,11 +418,17 @@ class BaseDocURLFormSet(DynamicBaseModelFormSet):
def __init__(self, *args, **kwargs):
kwargs['prefix'] = 'docurl'
+ # get request.META to add HTTP_ACCEPT_LANGUAGE on top of languages
+ # choices
+ meta = kwargs.pop('meta', {})
+ accept_lang = meta.get('HTTP_ACCEPT_LANGUAGE', '')
super(BaseDocURLFormSet, self).__init__(*args, **kwargs)
# construct description formsets
self.DocURLDescFormSet = modelformset_factory(DocURLDesc,
extra=self.extra, form=DocURLDescForm,
formset=BaseDocURLDescFormSet)
+ self.DocURLDescFormSet.form = staticmethod(curry(DocURLDescForm,
+ http_accept_language=accept_lang))
for index, form in enumerate(self.forms):
prefix = 'desc_%s' % index
data = self.data or None
@@ -468,10 +485,20 @@ class Meta:
exclude = ['enableurl']
def __init__(self, *args, **kwargs):
+ http_accept_language = kwargs.pop('http_accept_language', '')
super(EnableURLInstForm, self).__init__(*args, **kwargs)
+ choices = []
+ # add HTTP_ACCEPTED_LANG first
+ if http_accept_language:
+ codes = re.split(';|,', http_accept_language)
+ for code in codes:
+ try:
+ li = get_language_info(code)
+ except:
+ continue
+ choices.append((code, li['name'] + ' - ' + li['name_local']))
# Redefine our choices, so we can add the translated language names and
# sort the list by language name
- choices = []
choices.append(self.fields['language'].choices[0])
langs = self.fields['language'].choices[1:]
langs.sort(key=lambda l: l[1].lower())
@@ -557,11 +584,17 @@ class BaseEnableURLFormSet(DynamicBaseModelFormSet):
def __init__(self, *args, **kwargs):
kwargs['prefix'] = 'enableurl'
+ # get request.META to add HTTP_ACCEPT_LANGUAGE on top of languages
+ # choices
+ meta = kwargs.pop('meta', {})
+ accept_lang = meta.get('HTTP_ACCEPT_LANGUAGE', '')
super(BaseEnableURLFormSet, self).__init__(*args, **kwargs)
# construct instructions formsets
self.EnableURLInstFormSet = modelformset_factory(EnableURLInst,
extra=self.extra, form=EnableURLInstForm,
formset=BaseEnableURLInstFormSet)
+ self.EnableURLInstFormSet.form = staticmethod(curry(EnableURLInstForm,
+ http_accept_language=accept_lang))
for index, form in enumerate(self.forms):
prefix = 'inst_%s' % index
data = self.data or None
View
31 ispdb/config/views.py
@@ -165,9 +165,9 @@ def edit(request, config_id):
domain_formset = DomainFormSet(request.POST, request.FILES,
queryset=domain_queryset)
docurl_formset = DocURLFormSet(request.POST, request.FILES,
- queryset=docurl_queryset)
+ queryset=docurl_queryset, meta=request.META)
enableurl_formset = EnableURLFormSet(request.POST, request.FILES,
- queryset=enableurl_queryset)
+ queryset=enableurl_queryset, meta=request.META)
config_form = ConfigForm(request.POST,
request.FILES,
instance=config,
@@ -183,8 +183,10 @@ def edit(request, config_id):
return HttpResponseRedirect(reverse('ispdb_details',
args=[config.id]))
else:
- docurl_formset = DocURLFormSet(queryset=docurl_queryset)
- enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset)
+ docurl_formset = DocURLFormSet(queryset=docurl_queryset,
+ meta=request.META)
+ enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset,
+ meta=request.META)
domain_formset = DomainFormSet(queryset=domain_queryset)
config_form = ConfigForm(instance=config,
domain_formset=domain_formset,
@@ -213,9 +215,9 @@ def add(request, domain=None):
if request.method == 'POST': # If the form has been submitted...
data = request.POST
docurl_formset = DocURLFormSet(request.POST, request.FILES,
- queryset=DocURL.objects.none())
+ queryset=DocURL.objects.none(), meta=request.META)
enableurl_formset = EnableURLFormSet(request.POST, request.FILES,
- queryset=EnableURL.objects.none())
+ queryset=EnableURL.objects.none(), meta=request.META)
domain_formset = DomainFormSet(request.POST, request.FILES,
queryset=DomainRequest.objects.none())
# did the user fill in a full form, or are they just asking for some
@@ -254,14 +256,15 @@ def add(request, domain=None):
else:
has_errors = True
else:
- docurl_formset = DocURLFormSet(queryset=DocURL.objects.none())
- enableurl_formset = EnableURLFormSet(queryset=EnableURL.objects.none())
+ docurl_formset = DocURLFormSet(queryset=DocURL.objects.none(),
+ meta=request.META)
+ enableurl_formset = EnableURLFormSet(queryset=EnableURL.objects.none(),
+ meta=request.META)
domain_formset = DomainFormSet(initial=[{'name': domain}],
queryset=DomainRequest.objects.none())
config_form = ConfigForm(domain_formset=domain_formset,
docurl_formset=docurl_formset,
enableurl_formset=enableurl_formset)
-
return render_to_response('config/enter_config.html', {
'config_form': config_form,
'action': action,
@@ -402,9 +405,9 @@ def report(request, id):
domain_formset = DomainFormSet(request.POST, request.FILES,
queryset=domain_queryset)
docurl_formset = DocURLFormSet(request.POST, request.FILES,
- queryset=docurl_queryset)
+ queryset=docurl_queryset, meta=request.META)
enableurl_formset = EnableURLFormSet(request.POST, request.FILES,
- queryset=enableurl_queryset)
+ queryset=enableurl_queryset, meta=request.META)
p_config = Config(owner=request.user, status='suggested')
issue = Issue(config=config, owner=request.user)
config_form = ConfigForm(request.POST,
@@ -472,8 +475,10 @@ def report(request, id):
else:
domain_formset = DomainFormSet(queryset=domain_queryset)
- docurl_formset = DocURLFormSet(queryset=docurl_queryset)
- enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset)
+ docurl_formset = DocURLFormSet(queryset=docurl_queryset,
+ meta=request.META)
+ enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset,
+ meta=request.META)
config_form = ConfigForm(instance=config,
domain_formset=domain_formset,
docurl_formset=docurl_formset,
Please sign in to comment.
Something went wrong with that request. Please try again.