From a33ee7dac0ee1f1c561664598eddfdfa3d2e9314 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 15 Jan 2018 18:34:54 +0100 Subject: UI: adapt each form to use the new date widget. Force localization of format. --- ishtar_common/forms.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'ishtar_common/forms.py') diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index b31a3143f..a92aef548 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -28,10 +28,11 @@ from django import forms from django.core.urlresolvers import reverse from django.core import validators from django.forms.formsets import BaseFormSet, DELETION_FIELD_NAME -from django.utils import formats +from django.utils import formats, translation from django.utils.functional import lazy from django.utils.translation import ugettext_lazy as _ +from bootstrap_datepicker.widgets import DatePicker import models import widgets from ishtar_common.utils import MultiValueDict @@ -241,7 +242,31 @@ class FormSet(CustomForm, BaseFormSet): form.fields[DELETION_FIELD_NAME].widget = widgets.DeleteWidget() -class TableSelect(forms.Form): +DATE_FORMAT = { + 'fr': "dd/mm/yyyy", + 'en': "yyyy/mm/dd", +} + + +class IshtarForm(forms.Form): + def __init__(self, *args, **kwargs): + super(IshtarForm, self).__init__(*args, **kwargs) + for k in self.fields: + cls = 'form-control' + if 'class' in self.fields[k].widget.attrs: + cls = self.fields[k].widget.attrs['class'] + " " + cls + self.fields[k].widget.attrs['class'] = cls + widget = self.fields[k].widget + if not isinstance(widget, DatePicker): + continue + lang = translation.get_language() + if lang in DATE_FORMAT: + widget.options['format'] = DATE_FORMAT[lang] + if 'autoclose' not in widget.options: + widget.options['autoclose'] = 'true' + + +class TableSelect(IshtarForm): def __init__(self, *args, **kwargs): super(TableSelect, self).__init__(*args, **kwargs) # no field is required for search @@ -267,7 +292,7 @@ def get_now(): class ClosingDateFormSelection(forms.Form): form_label = _("Closing date") end_date = forms.DateField(label=_(u"Closing date"), - widget=widgets.JQueryDate) + widget=DatePicker) def __init__(self, *args, **kwargs): if 'initial' not in kwargs: @@ -339,16 +364,6 @@ def get_data_from_formset(data): return values -class IshtarForm(forms.Form): - def __init__(self, *args, **kwargs): - super(IshtarForm, self).__init__(*args, **kwargs) - for k in self.fields: - cls = 'form-control' - if 'class' in self.fields[k].widget.attrs: - cls = self.fields[k].widget.attrs['class'] + " " + cls - self.fields[k].widget.attrs['class'] = cls - - class FieldType(object): def __init__(self, key, model, is_multiple=False, extra_args=None): self.key = key -- cgit v1.2.3