Skip to content
Snippets Groups Projects
Commit 22dc8ef5 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Show current menu

parent 6f0f439b
No related branches found
No related tags found
1 merge request!86Merge school-apps
......@@ -4,5 +4,6 @@ from . import views
urlpatterns = [
path('', views.index, name="menu_index"),
path('upload/', views.upload, name="menu_upload"),
path('<str:msg>', views.index, name="menu_index_msg")
path('current.pdf', views.show_current, name="menu_show_current"),
path('<str:msg>', views.index, name="menu_index_msg"),
]
import datetime
import os
from django.http import FileResponse
from django.shortcuts import render, redirect
from django.utils import timezone
from mealplan.models import MealPlan
from .forms import MenuUploadForm
......@@ -22,3 +27,39 @@ def upload(request):
def index(request, msg=None):
menus = MealPlan.objects.all().order_by("calendar_week", "year")
return render(request, 'menu/index.html', {"msg": msg, "menus": menus})
def return_pdf(filename):
print(filename)
# Read and response PDF
file = open(filename, "rb")
return FileResponse(file, content_type="application/pdf")
def return_default_pdf():
return return_pdf(os.path.join("mealplan", "default.pdf"))
def show_current(request):
current_date = timezone.datetime.now()
year, calendar_week = current_date.isocalendar()[:2]
# print(calendar_week)
# current_date += datetime.timedelta(days=4)
days_to_add = 5 - current_date.isoweekday()
# print(days_to_add)
if days_to_add < 0:
days_to_add = days_to_add + 7
# print(days_to_add)
friday = current_date + datetime.timedelta(days=days_to_add)
# print(friday)
friday_14_10 = timezone.datetime(friday.year, friday.month, friday.day, 14, 10)
# print(friday_14_10)
if current_date > friday_14_10:
calendar_week += 1
print(calendar_week)
try:
obj = MealPlan.objects.get(year=year, calendar_week=calendar_week)
print(os.path.join("media", str(obj.pdf)))
return return_pdf(os.path.join("media", str(obj.pdf)))
except MealPlan.DoesNotExist:
return return_default_pdf()
......@@ -122,6 +122,19 @@
<div class="divider"></div>
</li>
{% endif %}
{% if perms.mealplan.can_add_mealplan %}
<li>
<a class="subheader grey lighten-3">Speiseplan</a>
</li>
<li>
<a href="{% url 'menu_index' %}">
<i class="material-icons">restaurant_menu</i> Speiseplan hochladen
</a>
</li>
<li>
<div class="divider"></div>
</li>
{% endif %}
<li>
<a href="{% url 'logout' %}">
<i class="material-icons">exit_to_app</i> Abmelden
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment