From a393934e4c718106af19a92093f9d65423f00b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Tue, 24 Aug 2021 21:42:48 +0200 Subject: [PATCH] Fix election edit datetime input --- helios/forms.py | 13 ++++++++++++- helios/widgets.py | 9 +++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/helios/forms.py b/helios/forms.py index c08d8d0..b1d6687 100644 --- a/helios/forms.py +++ b/helios/forms.py @@ -3,8 +3,11 @@ Forms for Helios """ +import pytz + from django import forms from django.conf import settings +from django.utils.timezone import make_aware, make_naive from .fields import SplitDateTimeField from .models import Election @@ -27,7 +30,15 @@ class ElectionForm(forms.Form): if settings.ALLOW_ELECTION_INFO_URL: election_info_url = forms.CharField(required=False, initial="", label=u"URL pro stažení informací o hlasování", help_text=u"URL dokumentu ve formátu PDF, obsahujícího doplňkové informace k hlasování, např. životopisy a profily kandidátů") - pass + def __init__(self, data=None, *args, **kwargs): + # v DB se ukládá naivní UTC, ale do formuláře potřebujeme převést zpět na Europe/Prague + if data: + tz = pytz.timezone("Europe/Prague") + if "voting_starts_at" in data: + data["voting_starts_at"] = make_naive(make_aware(data["voting_starts_at"], pytz.UTC), tz) + if "voting_ends_at" in data: + data["voting_ends_at"] = make_naive(make_aware(data["voting_ends_at"], pytz.UTC), tz) + super().__init__(data, *args, **kwargs) class ElectionTimeExtensionForm(forms.Form): voting_extended_until = SplitDateTimeField(help_text = u'datum a čas prodlouženého ukončení hlasování; v lokálním čase!', diff --git a/helios/widgets.py b/helios/widgets.py index ba271ce..3e8c36d 100644 --- a/helios/widgets.py +++ b/helios/widgets.py @@ -95,16 +95,17 @@ class SelectTimeWidget(Widget): self.meridiem_val = 'a.m.' else: self.meridiem_val = None - + # If we're doing a 12-hr clock, there will be a meridiem value, so make sure the # hours get printed correctly if self.twelve_hr and self.meridiem_val: if self.meridiem_val.lower().startswith('p') and hour_val > 12 and hour_val < 24: hour_val = hour_val % 12 - elif hour_val == 0: - hour_val = 12 - + # BUG this breaks 24h clock on 0 hours + # elif hour_val == 0: + # hour_val = 12 + output = [] if 'id' in self.attrs: id_ = self.attrs['id'] -- GitLab