From 525dcbf3bbc70c0d23da83dafb575f9da4e790e1 Mon Sep 17 00:00:00 2001 From: shagi Date: Mon, 11 Jan 2021 16:15:57 +0100 Subject: [PATCH] Helper function to use SplitDateTimeField for datetimes --- gas/forms.py | 23 +++++++++++++++++++++++ gas/templates/gas/tags/forms/field.html | 1 - gas/templatetags/form_tags.py | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/gas/forms.py b/gas/forms.py index 5fe4091..3fc654a 100644 --- a/gas/forms.py +++ b/gas/forms.py @@ -55,3 +55,26 @@ class UserForm(forms.ModelForm): self.save_m2m() return obj + + +def split_datetime_field(form, field_name): + old_field = form.fields[field_name] + new_field = forms.SplitDateTimeField( + label=old_field.label, + label_suffix=old_field.label_suffix, + required=old_field.required, + help_text=old_field.help_text, + initial=old_field.initial, + validators=old_field.validators, + localize=old_field.localize, + disabled=old_field.disabled, + input_date_formats=("%Y-%m-%d",), + input_time_formats=("%H:%M", "%H:%M:%S"), + widget=forms.SplitDateTimeWidget( + date_format='%Y-%m-%d', + time_format='%H:%M:%S', + time_attrs={'placeholder': '00:00'}, + ), + ) + new_field.widget.widgets[0].input_type = 'date' + form.fields[field_name] = new_field diff --git a/gas/templates/gas/tags/forms/field.html b/gas/templates/gas/tags/forms/field.html index 8bf61c3..458abc3 100644 --- a/gas/templates/gas/tags/forms/field.html +++ b/gas/templates/gas/tags/forms/field.html @@ -5,7 +5,6 @@ {% if field.errors %} has-error{% endif %} "> - {% add_widget_attrs field placeholder=field.label %} {{ field }} {% if add_another_url %} diff --git a/gas/templatetags/form_tags.py b/gas/templatetags/form_tags.py index a49f965..a17ac69 100644 --- a/gas/templatetags/form_tags.py +++ b/gas/templatetags/form_tags.py @@ -22,6 +22,11 @@ def base_form_field(field, css=None, container_class='', add_another_url=None, f elif isinstance(field.field, forms.DateField): field.field.widget.input_type = 'date' + if isinstance(field.field, forms.SplitDateTimeField): + field.field.widget.widgets[1].attrs['placeholder'] = '00:00:00' + else: + field.field.widget.attrs['placeholder'] = field.label + if field_template is None: if isinstance(field.field.widget, forms.CheckboxInput): field_template = 'gas/tags/forms/checkbox.html'