diff --git a/aleksis/apps/mensa/settings.py b/aleksis/apps/mensa/settings.py index 4198d3cf7bec319ee93eacb50b4e37222f017ad1..f8d40936cabc3ab66c49bf7c856e57dc38602a96 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 f8b0abcf6adf775926db0ef1efd731555c0c66db..d4860f82bf99104a9a02bb5659d25aaf629523c4 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