From a2e203a3f86fd80609177b8ca419525320e86abe Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 14 Aug 2022 12:34:32 -0700 Subject: [PATCH] Use date only input for dates without time --- core/forms.py | 12 ++++++------ core/widgets.py | 17 +++++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/forms.py b/core/forms.py index 5822d761..22c8e65d 100644 --- a/core/forms.py +++ b/core/forms.py @@ -8,7 +8,7 @@ from django.utils.translation import gettext as _ from taggit.forms import TagField from core import models -from core.widgets import TagsEditor, ChildRadioSelect, DateTimeInput +from core.widgets import TagsEditor, ChildRadioSelect, DateInput, DateTimeInput def set_initial_values(kwargs, form_type): @@ -97,7 +97,7 @@ class ChildForm(forms.ModelForm): if settings.BABY_BUDDY["ALLOW_UPLOADS"]: fields.append("picture") widgets = { - "birth_date": DateTimeInput(), + "birth_date": DateInput(), } @@ -237,7 +237,7 @@ class WeightForm(CoreModelForm, TaggableModelForm): fields = ["child", "weight", "date", "notes", "tags"] widgets = { "child": ChildRadioSelect, - "date": DateTimeInput(), + "date": DateInput(), "notes": forms.Textarea(attrs={"rows": 5}), } @@ -248,7 +248,7 @@ class HeightForm(CoreModelForm, TaggableModelForm): fields = ["child", "height", "date", "notes", "tags"] widgets = { "child": ChildRadioSelect, - "date": DateTimeInput(), + "date": DateInput(), "notes": forms.Textarea(attrs={"rows": 5}), } @@ -259,7 +259,7 @@ class HeadCircumferenceForm(CoreModelForm, TaggableModelForm): fields = ["child", "head_circumference", "date", "notes", "tags"] widgets = { "child": ChildRadioSelect, - "date": DateTimeInput(), + "date": DateInput(), "notes": forms.Textarea(attrs={"rows": 5}), } @@ -270,7 +270,7 @@ class BMIForm(CoreModelForm, TaggableModelForm): fields = ["child", "bmi", "date", "notes", "tags"] widgets = { "child": ChildRadioSelect, - "date": DateTimeInput(), + "date": DateInput(), "notes": forms.Textarea(attrs={"rows": 5}), } diff --git a/core/widgets.py b/core/widgets.py index aaec4a6f..d66a22f3 100644 --- a/core/widgets.py +++ b/core/widgets.py @@ -1,13 +1,12 @@ import datetime from typing import Any, Dict, Optional -from django.forms import Widget, RadioSelect, DateTimeInput as DateTimeInputBase -from django.utils import timezone +from django.forms import RadioSelect, widgets from . import models -class TagsEditor(Widget): +class TagsEditor(widgets.Widget): """ Custom widget that provides an alternative editor for tags provided by the taggit library. @@ -107,10 +106,16 @@ class ChildRadioSelect(RadioSelect): return option -class DateTimeInput(DateTimeInputBase): - input_type = "datetime-local" - +class DateTimeBaseInput(widgets.DateTimeBaseInput): def format_value(self, value): if isinstance(value, datetime.datetime): value = value.isoformat() return value + + +class DateTimeInput(DateTimeBaseInput): + input_type = "datetime-local" + + +class DateInput(DateTimeBaseInput): + input_type = "date"