From 8249be96959126e71e1a7ad847f891b28f797350 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sat, 18 Jan 2020 15:10:20 +0100
Subject: [PATCH] Rewrite show_current using constance settings and calendar
 week module

---
 aleksis/apps/mensa/settings.py | 10 ++++++++++
 aleksis/apps/mensa/views.py    | 27 ++++++++++-----------------
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/aleksis/apps/mensa/settings.py b/aleksis/apps/mensa/settings.py
index 4198d3c..f8d4093 100644
--- a/aleksis/apps/mensa/settings.py
+++ b/aleksis/apps/mensa/settings.py
@@ -1,3 +1,13 @@
 import os
+from datetime import time
+
+from django.utils.translation import gettext_lazy as _
 
 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+CONSTANCE_CONFIG = {
+    "MENSA_NEW_WEEK_DAY": (4, _("Weekday at which the plan of the next week is to be shown"), "weekday_field"),
+    "MENSA_NEW_WEEK_TIME": (time(14, 00), _("Time at which the plan of the next week is to be shown"), time)
+}
+CONSTANCE_CONFIG_FIELDSETS = {
+    "Mensa settings": ("MENSA_NEW_WEEK_DAY", "MENSA_NEW_WEEK_TIME"),
+}
diff --git a/aleksis/apps/mensa/views.py b/aleksis/apps/mensa/views.py
index f8b0abc..d4860f8 100644
--- a/aleksis/apps/mensa/views.py
+++ b/aleksis/apps/mensa/views.py
@@ -1,11 +1,12 @@
-import datetime
 import os
-import time
 
+from calendarweek import CalendarWeek
+from constance import config
 from django.contrib.auth.decorators import login_required, permission_required
 from django.http import FileResponse
 from django.shortcuts import render, redirect, get_object_or_404
 from django.utils import timezone
+from django.conf import settings
 
 from .models import Menu
 from .settings import BASE_DIR
@@ -64,27 +65,19 @@ def return_default_pdf():
 def show_current(request):
     # Get current date with year and calendar week
     current_date = timezone.datetime.now()
-    year, calendar_week = current_date.isocalendar()[:2]
+    cw = CalendarWeek.from_date(current_date)
 
-    # Calculate the number of days to next friday
-    days_to_add = 5 - current_date.isoweekday()
-    if days_to_add < 0:
-        days_to_add = days_to_add + 7
-
-    if days_to_add == 6 or days_to_add == 7:
-        calendar_week += 1
-
-    # Create datetime with next friday and time 14:10
-    friday = current_date + datetime.timedelta(days=days_to_add)
-    friday_14_10 = timezone.datetime(friday.year, friday.month, friday.day, 14, 10)
+    # Create datetime with the friday of the week and the toggle time
+    friday = cw[int(config.MENSA_NEW_WEEK_DAY)]
+    friday = timezone.datetime.combine(friday, config.MENSA_NEW_WEEK_TIME)
 
     # Check whether to show the plan of the next week or the current week
-    if current_date > friday_14_10:
-        calendar_week += 1
+    if current_date > friday:
+        cw += 1
 
     # Look for matching PDF in DB
     try:
-        obj = Menu.objects.get(year=year, calendar_week=calendar_week)
+        obj = Menu.objects.get(year=cw.year, calendar_week=cw.week)
         return return_pdf(os.path.join(settings.MEDIA_ROOT, str(obj.pdf)))
 
     # Or show the default PDF
-- 
GitLab