From 49a0ade315342fac45b1320daef897f172a0d98c Mon Sep 17 00:00:00 2001 From: tleb Date: Tue, 21 Apr 2020 23:18:15 +0200 Subject: [PATCH] core: create TzAwareDateTimeField to replace forms.DateTimeField --- club/forms.py | 16 ++++---------- com/templates/com/poster_list.jinja | 4 ++-- com/views.py | 34 ++++++----------------------- core/views/forms.py | 27 +++++++++++++++++++++++ counter/views.py | 15 +++---------- election/views.py | 29 +++++------------------- subscription/views.py | 15 +++++-------- 7 files changed, 54 insertions(+), 86 deletions(-) diff --git a/club/forms.py b/club/forms.py index 519f54f8..658b343f 100644 --- a/club/forms.py +++ b/club/forms.py @@ -34,6 +34,7 @@ from club.models import Mailing, MailingSubscription, Club, Membership from core.models import User from core.views.forms import SelectDate, SelectDateTime from counter.models import Counter +from core.views.forms import TzAwareDateTimeField class ClubEditForm(forms.ModelForm): @@ -158,18 +159,9 @@ class MailingForm(forms.Form): class SellingsForm(forms.Form): - begin_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Begin date"), - required=False, - widget=SelectDateTime, - ) - end_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - required=False, - widget=SelectDateTime, - ) + begin_date = TzAwareDateTimeField(label=_("Begin date"), required=False) + end_date = TzAwareDateTimeField(label=_("End date"), required=False) + counters = forms.ModelMultipleChoiceField( Counter.objects.order_by("name").all(), label=_("Counter"), required=False ) diff --git a/com/templates/com/poster_list.jinja b/com/templates/com/poster_list.jinja index 7bd340fd..f752013e 100644 --- a/com/templates/com/poster_list.jinja +++ b/com/templates/com/poster_list.jinja @@ -36,8 +36,8 @@
{{ poster.name }}
-
{{ poster.date_begin | date("d/M/Y H:m") }}
-
{{ poster.date_end | date("d/M/Y H:m") }}
+
{{ poster.date_begin | localtime | date("d/M/Y H:m") }}
+
{{ poster.date_end | localtime | date("d/M/Y H:m") }}
{% if app == "com" %} {% trans %}Edit{% endtrans %} diff --git a/com/views.py b/com/views.py index fa135135..7ac5f7a2 100644 --- a/com/views.py +++ b/com/views.py @@ -52,6 +52,7 @@ from core.views import ( from core.views.forms import SelectDateTime, MarkdownInput from core.models import Notification, RealGroup, User from club.models import Club, Mailing +from core.views.forms import TzAwareDateTimeField # Sith object @@ -73,19 +74,12 @@ class PosterForm(forms.ModelForm): ] widgets = {"screens": forms.CheckboxSelectMultiple} - date_begin = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], + date_begin = TzAwareDateTimeField( label=_("Start date"), - widget=SelectDateTime, required=True, initial=timezone.now().strftime("%Y-%m-%d %H:%M:%S"), ) - date_end = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - widget=SelectDateTime, - required=False, - ) + date_end = TzAwareDateTimeField(label=_("End date"), required=False) def __init__(self, *args, **kwargs): self.user = kwargs.pop("user", None) @@ -199,24 +193,10 @@ class NewsForm(forms.ModelForm): "content": MarkdownInput, } - start_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Start date"), - widget=SelectDateTime, - required=False, - ) - end_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - widget=SelectDateTime, - required=False, - ) - until = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Until"), - widget=SelectDateTime, - required=False, - ) + start_date = TzAwareDateTimeField(label=_("Start date"), required=False) + end_date = TzAwareDateTimeField(label=_("End date"), required=False) + until = TzAwareDateTimeField(label=_("Until"), required=False) + automoderation = forms.BooleanField(label=_("Automoderation"), required=False) def clean(self): diff --git a/core/views/forms.py b/core/views/forms.py index 392acb3e..f9f8fe00 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -42,6 +42,10 @@ from django.utils.translation import ugettext from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget from ajax_select.fields import AutoCompleteSelectField from ajax_select import make_ajax_field +from django.utils.dateparse import parse_datetime +from django.utils import timezone +import datetime +from django.forms.utils import to_current_timezone import re @@ -392,3 +396,26 @@ class GiftForm(forms.ModelForm): id=user_id ) self.fields["user"].widget = forms.HiddenInput() + + +class TzAwareDateTimeField(forms.DateTimeField): + def __init__( + self, input_formats=["%Y-%m-%d %H:%M:%S"], widget=SelectDateTime, **kwargs + ): + super().__init__(input_formats=input_formats, widget=widget, **kwargs) + + def prepare_value(self, value): + # the db value is a datetime as a string in UTC + if isinstance(value, str): + # convert it into a naive datetime (no timezone attached) + value = parse_datetime(value) + # attach it to the UTC timezone (so that to_current_timezone() + # converts it to the local timezone) + value = timezone.make_aware(value, timezone.utc) + + if isinstance(value, datetime.datetime): + value = to_current_timezone(value) + # otherwise it is formatted according to locale (in french) + value = str(value) + + return value diff --git a/counter/views.py b/counter/views.py index bf29f476..e03831e1 100644 --- a/counter/views.py +++ b/counter/views.py @@ -70,6 +70,7 @@ from counter.models import ( Permanency, ) from accounting.models import CurrencyField +from core.views.forms import TzAwareDateTimeField class CounterAdminMixin(View): @@ -1584,18 +1585,8 @@ class CashSummaryEditView(CounterAdminTabsMixin, CounterAdminMixin, UpdateView): class CashSummaryFormBase(forms.Form): - begin_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Begin date"), - required=False, - widget=SelectDateTime, - ) - end_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - required=False, - widget=SelectDateTime, - ) + begin_date = TzAwareDateTimeField(label=_("Begin date"), required=False) + end_date = TzAwareDateTimeField(label=_("End date"), required=False) class CashSummaryListView(CounterAdminTabsMixin, CounterAdminMixin, ListView): diff --git a/election/views.py b/election/views.py index b8bd4580..ad0cdb3e 100644 --- a/election/views.py +++ b/election/views.py @@ -14,6 +14,7 @@ from core.views import CanViewMixin, CanEditMixin, CanCreateMixin from django.db.models.query import QuerySet from core.views.forms import SelectDateTime, MarkdownInput from election.models import Election, Role, Candidature, ElectionList, Vote +from core.views.forms import TzAwareDateTimeField from ajax_select.fields import AutoCompleteSelectField from ajax_select import make_ajax_field @@ -167,30 +168,12 @@ class ElectionForm(forms.ModelForm): label=_("candidature groups"), ) - start_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Start date"), - widget=SelectDateTime, - required=True, - ) - end_date = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - widget=SelectDateTime, - required=True, - ) - start_candidature = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Start candidature"), - widget=SelectDateTime, - required=True, - ) - end_candidature = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End candidature"), - widget=SelectDateTime, - required=True, + start_date = TzAwareDateTimeField(label=_("Start date"), required=True) + end_date = TzAwareDateTimeField(label=_("End date"), required=True) + start_candidature = TzAwareDateTimeField( + label=_("Start candidature"), required=True ) + end_candidature = TzAwareDateTimeField(label=_("End candidature"), required=True) # Display elections diff --git a/subscription/views.py b/subscription/views.py index 45e04a55..63ce0a33 100644 --- a/subscription/views.py +++ b/subscription/views.py @@ -36,22 +36,17 @@ from subscription.models import Subscription from core.views.forms import SelectDateTime from core.models import User from core.views.forms import SelectDate +from core.views.forms import TzAwareDateTimeField class SelectionDateForm(forms.Form): def __init__(self, *args, **kwargs): super(SelectionDateForm, self).__init__(*args, **kwargs) - self.fields["start_date"] = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("Start date"), - widget=SelectDateTime, - required=True, + self.fields["start_date"] = TzAwareDateTimeField( + label=_("Start date"), required=True ) - self.fields["end_date"] = forms.DateTimeField( - input_formats=["%Y-%m-%d %H:%M:%S"], - label=_("End date"), - widget=SelectDateTime, - required=True, + self.fields["end_date"] = TzAwareDateTimeField( + label=_("End date"), required=True )