From 0d1db941b81aba7a7b1d205104a08801c2a5c7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Sun, 9 Jun 2013 16:41:58 +0200 Subject: [PATCH] Insert LocalizedModelChoiceField for use in MotionSetWorkflowMixin. Fixed #713. --- openslides/motion/forms.py | 5 ++--- openslides/utils/forms.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/openslides/motion/forms.py b/openslides/motion/forms.py index c4df70ec6..f5d92bb7f 100644 --- a/openslides/motion/forms.py +++ b/openslides/motion/forms.py @@ -14,8 +14,7 @@ from django import forms from django.utils.translation import ugettext as _, ugettext_lazy from openslides.config.api import config -from openslides.utils.forms import CssClassMixin -from openslides.utils.forms import CleanHtmlFormMixin +from openslides.utils.forms import CssClassMixin, CleanHtmlFormMixin, LocalizedModelChoiceField from openslides.utils.person import PersonFormField, MultiplePersonFormField from .models import Motion, Category, Workflow @@ -144,7 +143,7 @@ class MotionSetWorkflowMixin(forms.ModelForm): so, the motion's state is reset. """ - set_workflow = forms.ModelChoiceField( + set_workflow = LocalizedModelChoiceField( queryset=Workflow.objects.all(), required=False, label=ugettext_lazy('Workflow'), diff --git a/openslides/utils/forms.py b/openslides/utils/forms.py index 6a43ed630..ebc16d492 100644 --- a/openslides/utils/forms.py +++ b/openslides/utils/forms.py @@ -13,7 +13,7 @@ import bleach from django import forms -from django.utils.translation import ugettext_lazy +from django.utils.translation import ugettext as _, ugettext_lazy # Allowed tags, attributes and styles allowed in textareas edited with a JS @@ -48,6 +48,18 @@ class CssClassMixin(object): required_css_class = 'required' +class LocalizedModelChoiceField(forms.ModelChoiceField): + """ + Subclass of Django's ModelChoiceField to translate the labels of the + model's objects. + """ + def label_from_instance(self, *args, **kwargs): + """ + Translates the output from Django's label_from_instance method. + """ + return _(super(LocalizedModelChoiceField, self).label_from_instance(*args, **kwargs)) + + class LocalizedModelMultipleChoiceField(forms.ModelMultipleChoiceField): def __init__(self, *args, **kwargs): self.to_field_name = kwargs.get('to_field_name', None)