Skip to content

Commit

Permalink
Merge pull request #274 from VSEScala/mhvis-patch-20231228-datetimelocal
Browse files Browse the repository at this point in the history
Fix datetime-local input field
  • Loading branch information
mhvis authored Dec 31, 2023
2 parents 3a0fc77 + cb99261 commit 08cc428
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dining/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from general.forms import ConcurrenflictFormMixin
from general.mail_control import construct_templated_mail
from general.util import SelectWithDisabled
from scaladining.fields import DateTimeControlField
from userdetails.models import Association, User, UserMembership

__all__ = [
Expand Down Expand Up @@ -219,6 +220,7 @@ class DiningInfoForm(ConcurrenflictFormMixin, ServeTimeCheckMixin, forms.ModelFo
class Meta:
model = DiningList
fields = ["owners", "dish", "serve_time", "max_diners", "sign_up_deadline"]
field_classes = {"sign_up_deadline": DateTimeControlField}
widgets = {
"owners": ModelSelect2Multiple(
url="people_autocomplete", attrs={"data-minimum-input-length": "1"}
Expand All @@ -228,7 +230,6 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["serve_time"].widget.input_type = "time"
self.fields["sign_up_deadline"].widget.input_type = "datetime-local"
self.set_bounds(
"serve_time", "min", settings.KITCHEN_USE_START_TIME.strftime("%H:%M")
)
Expand Down
27 changes: 27 additions & 0 deletions scaladining/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Custom form fields."""

from django import forms


class DateTimeControlInput(forms.DateTimeInput):
"""See DateTimeControlField."""

input_type = "datetime-local"

def format_value(self, value):
# The value given seems to be naive. If it wasn't, we would need to
# make it naive first.
return value.strftime("%Y-%m-%dT%H:%M")


class DateTimeControlField(forms.DateTimeField):
"""Field for input type datetime-local.
The field requires a very specific format for the value attribute. This
subclass sets the formatted value and the input format correctly.
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
"""

input_formats = ("%Y-%m-%dT%H:%M",)
widget = DateTimeControlInput

0 comments on commit 08cc428

Please sign in to comment.