Use date only input for dates without time

This commit is contained in:
Christopher C. Wells 2022-08-14 12:34:32 -07:00 committed by Christopher C. Wells
parent c1e9f28215
commit a2e203a3f8
2 changed files with 17 additions and 12 deletions

View File

@ -8,7 +8,7 @@ from django.utils.translation import gettext as _
from taggit.forms import TagField from taggit.forms import TagField
from core import models 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): def set_initial_values(kwargs, form_type):
@ -97,7 +97,7 @@ class ChildForm(forms.ModelForm):
if settings.BABY_BUDDY["ALLOW_UPLOADS"]: if settings.BABY_BUDDY["ALLOW_UPLOADS"]:
fields.append("picture") fields.append("picture")
widgets = { widgets = {
"birth_date": DateTimeInput(), "birth_date": DateInput(),
} }
@ -237,7 +237,7 @@ class WeightForm(CoreModelForm, TaggableModelForm):
fields = ["child", "weight", "date", "notes", "tags"] fields = ["child", "weight", "date", "notes", "tags"]
widgets = { widgets = {
"child": ChildRadioSelect, "child": ChildRadioSelect,
"date": DateTimeInput(), "date": DateInput(),
"notes": forms.Textarea(attrs={"rows": 5}), "notes": forms.Textarea(attrs={"rows": 5}),
} }
@ -248,7 +248,7 @@ class HeightForm(CoreModelForm, TaggableModelForm):
fields = ["child", "height", "date", "notes", "tags"] fields = ["child", "height", "date", "notes", "tags"]
widgets = { widgets = {
"child": ChildRadioSelect, "child": ChildRadioSelect,
"date": DateTimeInput(), "date": DateInput(),
"notes": forms.Textarea(attrs={"rows": 5}), "notes": forms.Textarea(attrs={"rows": 5}),
} }
@ -259,7 +259,7 @@ class HeadCircumferenceForm(CoreModelForm, TaggableModelForm):
fields = ["child", "head_circumference", "date", "notes", "tags"] fields = ["child", "head_circumference", "date", "notes", "tags"]
widgets = { widgets = {
"child": ChildRadioSelect, "child": ChildRadioSelect,
"date": DateTimeInput(), "date": DateInput(),
"notes": forms.Textarea(attrs={"rows": 5}), "notes": forms.Textarea(attrs={"rows": 5}),
} }
@ -270,7 +270,7 @@ class BMIForm(CoreModelForm, TaggableModelForm):
fields = ["child", "bmi", "date", "notes", "tags"] fields = ["child", "bmi", "date", "notes", "tags"]
widgets = { widgets = {
"child": ChildRadioSelect, "child": ChildRadioSelect,
"date": DateTimeInput(), "date": DateInput(),
"notes": forms.Textarea(attrs={"rows": 5}), "notes": forms.Textarea(attrs={"rows": 5}),
} }

View File

@ -1,13 +1,12 @@
import datetime import datetime
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from django.forms import Widget, RadioSelect, DateTimeInput as DateTimeInputBase from django.forms import RadioSelect, widgets
from django.utils import timezone
from . import models from . import models
class TagsEditor(Widget): class TagsEditor(widgets.Widget):
""" """
Custom widget that provides an alternative editor for tags provided by the Custom widget that provides an alternative editor for tags provided by the
taggit library. taggit library.
@ -107,10 +106,16 @@ class ChildRadioSelect(RadioSelect):
return option return option
class DateTimeInput(DateTimeInputBase): class DateTimeBaseInput(widgets.DateTimeBaseInput):
input_type = "datetime-local"
def format_value(self, value): def format_value(self, value):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = value.isoformat() value = value.isoformat()
return value return value
class DateTimeInput(DateTimeBaseInput):
input_type = "datetime-local"
class DateInput(DateTimeBaseInput):
input_type = "date"