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

Add current plan

parent 22dc8ef5
No related branches found
No related tags found
1 merge request!86Merge school-apps
......@@ -6,7 +6,7 @@ from django.template.loader_tags import register
def path_and_rename(instance, filename):
upload_to = 'menus'
ext = filename.split('.')[-1]
ext = filename.split('.')[-1].lower()
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
......
......@@ -13,6 +13,8 @@
</script>
<a class="waves-effect waves-light btn green" href="{% url "menu_upload" %}"><i class="material-icons left">add</i>
Neuen Speiseplan hochladen</a>
<a class="waves-effect waves-light btn" href="{% url "menu_show_current" %}"><i class="material-icons left">picture_as_pdf</i>
Aktuellen Speiseplan anzeigen</a>
<h5>Übersicht der hochgeladenen Speisepläne</h5>
<ul class="collection">
{% for menu in menus %}
......
......@@ -30,36 +30,41 @@ def index(request, msg=None):
def return_pdf(filename):
print(filename)
# Read and response PDF
"""Read and response a PDF file"""
file = open(filename, "rb")
return FileResponse(file, content_type="application/pdf")
def return_default_pdf():
"""Response the default PDF"""
return return_pdf(os.path.join("mealplan", "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]
# print(calendar_week)
# current_date += datetime.timedelta(days=4)
# Calculate the number of days to next friday
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)
# Create datetime with next friday and time 14:10
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)
# Check whether to show the plan of the next week or the current week
if current_date > friday_14_10:
calendar_week += 1
print(calendar_week)
# Look for matching PDF in DB
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)))
# Or show the default PDF
except MealPlan.DoesNotExist:
return return_default_pdf()
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