From 64d1e893fe99d7ca735a54656a23166711b38992 Mon Sep 17 00:00:00 2001
From: Grollicus <cccvgitlab.db5c7b60@grollmann.eu>
Date: Mon, 28 Dec 2020 22:59:59 +0100
Subject: [PATCH] timezone warning

---
 src/core/models/conference.py                 |  1 +
 src/plainui/jinja2/plainui/fahrplan.html      |  4 +++
 src/plainui/jinja2/plainui/my_fahrplan.html   |  3 +++
 .../jinja2/plainui/public_fahrplan.html       |  3 +++
 src/plainui/locale/de/LC_MESSAGES/django.po   |  4 +++
 src/plainui/locale/en/LC_MESSAGES/django.po   |  4 +++
 src/plainui/views.py                          | 26 +++++++++++++++++++
 7 files changed, 45 insertions(+)

diff --git a/src/core/models/conference.py b/src/core/models/conference.py
index 773a09b1e..922246c2f 100644
--- a/src/core/models/conference.py
+++ b/src/core/models/conference.py
@@ -152,6 +152,7 @@ class Conference(models.Model):
         help_text=_('Conference__end__help'),
         verbose_name=_('Conference__end'))
 
+    # TODO Feld auf TimeZoneField umbauen
     timezone = models.CharField(
         choices=[(tz, tz) for tz in pytz.common_timezones],
         max_length=30,
diff --git a/src/plainui/jinja2/plainui/fahrplan.html b/src/plainui/jinja2/plainui/fahrplan.html
index 297413cd2..a97b49290 100644
--- a/src/plainui/jinja2/plainui/fahrplan.html
+++ b/src/plainui/jinja2/plainui/fahrplan.html
@@ -8,6 +8,10 @@
     {{ titleMacro.title(_("schedule"),
         share_url = url('plainui:fahrplan', conf_slug=conf.slug),
         report_url = url('plainui:fahrplan', conf_slug=conf.slug)) }}
+    {% if timezone_warning %}
+    <div class="text-warning">{{ _("Warning, your timezone is configured to %(user_timezone)s and does not match the conference timezone %(conf_timezone)s", user_timezone=current_timezone, conf_timezone=conference_timezone) }}</div>
+    {% endif %}
+
 
     {# TODO: Download options
     <h2>{{ _("download") }}</h2>
diff --git a/src/plainui/jinja2/plainui/my_fahrplan.html b/src/plainui/jinja2/plainui/my_fahrplan.html
index 5b7f3de3a..60d5bd108 100644
--- a/src/plainui/jinja2/plainui/my_fahrplan.html
+++ b/src/plainui/jinja2/plainui/my_fahrplan.html
@@ -7,6 +7,9 @@
     {{ titleMacro.title(_("personal schedule"),
         share_url = url('plainui:my_fahrplan', conf_slug=conf.slug),
         report_url = url('plainui:my_fahrplan', conf_slug=conf.slug)) }}
+    {% if timezone_warning %}
+    <div class="text-warning">{{ _("Warning, your timezone is configured to %(user_timezone)s and does not match the conference timezone %(conf_timezone)s", user_timezone=current_timezone, conf_timezone=conference_timezone) }}</div>
+    {% endif %}
 
     {# TODO: Download options
     <h2>{{ _("download") }}</h2>
diff --git a/src/plainui/jinja2/plainui/public_fahrplan.html b/src/plainui/jinja2/plainui/public_fahrplan.html
index f506ab812..913a0f146 100644
--- a/src/plainui/jinja2/plainui/public_fahrplan.html
+++ b/src/plainui/jinja2/plainui/public_fahrplan.html
@@ -17,6 +17,9 @@
     </head>
     <body class="rc3-fahrplan__pub-body p-4">
         <h1 class="h2">{{ _("%(conf)s Fahrplan", conf=conf.name) }}</h1>
+        {% if timezone_warning %}
+            <div class="text-warning">{{ _("Warning, your timezone is configured to %(user_timezone)s and does not match the conference timezone %(conf_timezone)s", user_timezone=current_timezone, conf_timezone=conference_timezone) }}</div>
+        {% endif %}
 
         {% if mode == 'list' %}
             {{ list_events.list(events, [], []) }}
diff --git a/src/plainui/locale/de/LC_MESSAGES/django.po b/src/plainui/locale/de/LC_MESSAGES/django.po
index 4c5304796..9d5c6719a 100644
--- a/src/plainui/locale/de/LC_MESSAGES/django.po
+++ b/src/plainui/locale/de/LC_MESSAGES/django.po
@@ -778,6 +778,10 @@ msgstr "Anhänge"
 msgid "schedule"
 msgstr "Zeitplan"
 
+#, python-format
+msgid "Warning, your timezone is configured to %(user_timezone)s and does not match the conference timezone %(conf_timezone)s"
+msgstr "Achtung, deine Zeitzone ist auf %(user_timezone)s eingestellt und entspricht nicht der Konferenzzeitzone %(conf_timezone)s"
+
 #, python-format
 msgid "Day %(n)s"
 msgstr ""
diff --git a/src/plainui/locale/en/LC_MESSAGES/django.po b/src/plainui/locale/en/LC_MESSAGES/django.po
index 0617abe4e..6536c1adb 100644
--- a/src/plainui/locale/en/LC_MESSAGES/django.po
+++ b/src/plainui/locale/en/LC_MESSAGES/django.po
@@ -778,6 +778,10 @@ msgstr ""
 msgid "schedule"
 msgstr ""
 
+#, python-format
+msgid "Warning, your timezone is configured to %(user_timezone)s and does not match the conference timezone %(conf_timezone)s"
+msgstr ""
+
 #, python-format
 msgid "Day %(n)s"
 msgstr ""
diff --git a/src/plainui/views.py b/src/plainui/views.py
index 97019fb26..5cf780429 100644
--- a/src/plainui/views.py
+++ b/src/plainui/views.py
@@ -1,6 +1,7 @@
 from datetime import time, timedelta
 from functools import lru_cache
 from typing import List
+import pytz
 
 from django.conf import settings
 from django.contrib import messages
@@ -1224,6 +1225,17 @@ class FahrplanView(ConferenceRequiredMixin, TemplateView):
         context = super().get_context_data(conf_slug=conf_slug, **kwargs)
         context['conf'] = self.conf
 
+        try:
+            timezone_warning = timezone.get_current_timezone() != pytz.timezone(self.conf.timezone)
+            context['timezone_warning'] = timezone_warning
+            if timezone_warning:
+                current_timezone = timezone.get_current_timezone()
+                if current_timezone is not None:
+                    context['current_timezone'] = current_timezone.zone
+                    context['conference_timezone'] = self.conf.timezone
+        except pytz.UnknownTimeZoneError:
+            context['timezone_warning'] = False
+
         context['my_favorite_events'] = _session_get_favorite_events(self.request.session, self.request.user)
         context['my_scheduled_events'] = _session_get_scheduled_events(self.request.session, self.request.user)
 
@@ -1344,6 +1356,20 @@ class PublicFahrplanView(ConferenceRequiredMixin, TemplateView):
     def get_context_data(self, **kwargs):
         context = super().get_context_data(conf=self.conf, **kwargs)
         context['mode'] = 'calendar'
+
+        # TODO: Timezone warning macht im public fahrplan keinen Sinn, wir stirppen momentan die Sessions im Loadbalancer.
+        #       Da sollten wir immer in der offiziellen Zeit anzeigen. Eventuell mit dauerhaftem Timezone-Hinweis?
+        try:
+            timezone_warning = self.conf.timezone and timezone.get_current_timezone() != pytz.timezone(self.conf.timezone)
+            context['timezone_warning'] = timezone_warning
+            if timezone_warning:
+                current_timezone = timezone.get_current_timezone()
+                if current_timezone is not None:
+                    context['current_timezone'] = current_timezone.zone
+                    context['conference_timezone'] = self.conf.timezone
+        except pytz.UnknownTimeZoneError:
+            context['timezone_warning'] = False
+
         events = _event_filter(self.request.user, self.conf, public_fahrplan=True)
         # events = _event_filter(self.request.user, self.conf)
         context['events'] = _organize_events_for_calendar(self.conf, events)
-- 
GitLab