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 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}),
}

View File

@ -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"