Verified Commit 8249be96 authored by Jonathan Weth's avatar Jonathan Weth

Rewrite show_current using constance settings and calendar week module

parent 1446f148
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"),
}
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment