diff --git a/la_chariotte/order/forms.py b/la_chariotte/order/forms.py index eb8e20e..3f7bb48 100644 --- a/la_chariotte/order/forms.py +++ b/la_chariotte/order/forms.py @@ -1,9 +1,14 @@ import datetime +from typing import Any, Optional, Sequence, Type, Union +import pytz from django import forms from django.contrib.auth import get_user_model from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.db import IntegrityError +from django.forms.utils import to_current_timezone +from django.forms.widgets import Widget +from django.utils import timezone from la_chariotte import settings from la_chariotte.order.models import GroupedOrder, Item @@ -56,6 +61,7 @@ class GroupedOrderForm(forms.ModelForm): # When updating an existing order, reuse existing values if kwargs["instance"]: + self.instance.deadline = to_current_timezone(self.instance.deadline) self.initial["deadline_date"] = self.instance.deadline.date() self.initial["deadline_time"] = self.instance.deadline.time() # TODO: there is a bug here ; the time displayed in form is 2 hours earlier than the actual deadline time @@ -66,8 +72,11 @@ class GroupedOrderForm(forms.ModelForm): def clean(self): cleaned_data = self.cleaned_data - self.instance.deadline = datetime.datetime.combine( - self.cleaned_data["deadline_date"], self.cleaned_data["deadline_time"] + self.instance.deadline = timezone.make_aware( + datetime.datetime.combine( + self.cleaned_data["deadline_date"], + self.cleaned_data["deadline_time"], + ) ) return cleaned_data