Commit e0913a2b authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

Remove crispy_forms dependency for language picker

parent 1a2dead9
......@@ -78,8 +78,6 @@ In den Projekteinstellungen (`settings.py`) müssen folgende Anpassungen vorgeno
]
```
:warning: **Achtung:** Der Sprachumschalter benötigt außerdem `crispy_forms`. Die Installation und Konfiguration ist weiter unten beschrieben.
Nutzung in Templates
--------------------
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django import forms
try:
# Available in Django 1.10+
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.translation import ugettext_lazy
class ChangeLanguageForm(forms.Form):
"""
Form for selecting the main language.
No crispy_forms required.
"""
next = forms.CharField(
required=False,
widget=forms.HiddenInput()
)
language = forms.ChoiceField(
choices=settings.LANGUAGES,
label=ugettext_lazy('Language'),
)
def __init__(self, redirect_to, *args, **kwargs):
super(ChangeLanguageForm, self).__init__(*args, **kwargs)
self.form_action = reverse('tuc2014:djangoi18n:set_language')
self.fields['next'].initial = redirect_to
......@@ -4,17 +4,13 @@ Form base classes which can be used with tuc2014, crispy_forms and bootstrap.
@author: klada
"""
from django import forms
try:
# Available in Django 1.10+
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.translation import ugettext as _, ugettext_lazy
from crispy_forms.bootstrap import FormActions, FieldWithButtons
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div, Layout, Submit
from tuc2014._forms import ChangeLanguageForm as _BaseChangeLanguageForm
class TucFormMixin(object):
"""
......@@ -82,19 +78,10 @@ class TucModelForm(forms.ModelForm, TucFormMixin):
self.add_crispy_attributes()
class ChangeLanguageForm(TucForm):
class ChangeLanguageForm(_BaseChangeLanguageForm, TucForm):
"""
Form for selecting the main language.
"""
next = forms.CharField(
required=False,
widget=forms.HiddenInput()
)
language = forms.ChoiceField(
choices=settings.LANGUAGES,
label=ugettext_lazy('Language'),
)
layout = Layout(
'next',
FieldWithButtons('language', Submit('submit', ugettext_lazy('Submit')))
......@@ -102,5 +89,6 @@ class ChangeLanguageForm(TucForm):
def __init__(self, redirect_to, *args, **kwargs):
super(ChangeLanguageForm, self).__init__(*args, **kwargs)
self.helper.form_action = reverse('tuc2014:djangoi18n:set_language')
self.fields['next'].initial = redirect_to
self.helper.form_action = self.form_action
self.add_aria_attributes()
self.add_crispy_attributes()
{% extends "tuc2014/base.html" %}
{% load i18n %}
{% block content %}
<h1>Sprache wählen/Change Language</h1>
<form method="post" action="{{form.form_action}}">
{% csrf_token %}
{{form.as_p}}
<p>
<input type="submit" />
</p>
</form>
{% endblock %}
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf.urls import include, url
from django.core.exceptions import ImproperlyConfigured
try:
from .views import change_language
except ImportError:
raise ImproperlyConfigured("crispy_forms is required for the tuc2014 language picker.")
from .views import change_language
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n', namespace='djangoi18n')),
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
from django.shortcuts import render
from django.utils import translation
from .forms import ChangeLanguageForm
# Check if crispy_forms is installed and set up correctly
if 'crispy_forms' in getattr(settings, 'INSTALLED_APPS', []):
from .forms import ChangeLanguageForm
HAVE_CRISPY = True
else:
from ._forms import ChangeLanguageForm
HAVE_CRISPY = False
def change_language(request, **kwargs):
"""
......@@ -14,7 +23,11 @@ def change_language(request, **kwargs):
referer = ""
cur_language = translation.get_language()
form = ChangeLanguageForm(referer, initial={'language': cur_language})
return render(request, 'tuc2014/change_language.html', {'form': form, 'current': cur_language})
if HAVE_CRISPY:
template = 'tuc2014/change_language.html'
else:
template = 'tuc2014/change_language_nocrispy.html'
return render(request, template, {'form': form, 'current': cur_language})
def handle_403(request, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment