1 Commits

Author SHA1 Message Date
beec919b2e Add date and time pickers to forms
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-16 22:05:02 +01:00
2 changed files with 12 additions and 31 deletions

View File

@ -4,23 +4,6 @@ from .models import Game, Platform, Purchase, Session
class SessionForm(forms.ModelForm):
custom_datetime_widget = forms.SplitDateTimeWidget(
date_format=("%d-%m-%Y"),
time_format=("%H:%M"),
date_attrs={"type": "date"},
time_attrs={"type": "time"},
)
timestamp_start = forms.SplitDateTimeField(
input_date_formats="['%d-%m-%Y]",
input_time_formats="['%H:%M']",
widget=custom_datetime_widget,
)
timestamp_end = forms.SplitDateTimeField(
input_date_formats="['%d-%m-%Y]",
input_time_formats="['%H:%M']",
widget=custom_datetime_widget,
)
class Meta:
model = Session
fields = [
@ -30,15 +13,13 @@ class SessionForm(forms.ModelForm):
"duration_manual",
"note",
]
# fields_classes = {
# "timestamp_start": custom_datetime_field,
# "timestamp_end": custom_datetime_field,
# }
# widgets = {
# "timestamp_start": custom_datetime_widget,
# "timestamp_end": custom_datetime_widget,
# }
custom_datetime_widget = forms.SplitDateTimeWidget(
date_attrs={"type": "date"}, time_attrs={"type": "time"}
)
widgets = {
"timestamp_start": custom_datetime_widget,
"timestamp_end": custom_datetime_widget,
}
class PurchaseForm(forms.ModelForm):
@ -64,3 +45,7 @@ class PlatformForm(forms.ModelForm):
class Meta:
model = Platform
fields = ["name", "group"]
class UnifiedGameForm(forms.ModelForm):
GameFormSet = forms.inlineformset_factory(Game, Purchase)

View File

@ -23,11 +23,7 @@ def add_session(request):
context = {}
now = now_with_tz()
last = Session.objects.all().last()
initial = {
"timestamp_start_0": now.date(),
"timestamp_start_1": now.time(),
"purchase": last.purchase,
}
initial = {"timestamp_start": now, "purchase": last.purchase}
form = SessionForm(request.POST or None, initial=initial)
if form.is_valid():
form.save()