From 5698b6d62530948a0169676747bc2f4419aeccb6 Mon Sep 17 00:00:00 2001
From: HanseGucker <joniweth@gmx.de>
Date: Thu, 29 Nov 2018 18:37:20 +0100
Subject: [PATCH] Validate uploaded files

---
 schoolapps/mealplan/forms.py                   | 10 +++++++---
 schoolapps/mealplan/templates/menu/index.html  | 14 +++++++++++++-
 schoolapps/mealplan/templates/menu/upload.html |  2 +-
 schoolapps/mealplan/views.py                   |  5 ++++-
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/schoolapps/mealplan/forms.py b/schoolapps/mealplan/forms.py
index f1c8903a5..0260c2860 100644
--- a/schoolapps/mealplan/forms.py
+++ b/schoolapps/mealplan/forms.py
@@ -1,16 +1,20 @@
 from django import forms
+from django.core.validators import FileExtensionValidator
 from django.utils import timezone
 
 from mealplan.models import MealPlan
 
+current_year = timezone.datetime.now().year
+options_for_year = [(current_year, current_year),
+                    (current_year + 1, current_year + 1)]
+
 
 class MenuUploadForm(forms.ModelForm):
     calendar_week = forms.ChoiceField(label="KW", choices=[(cw, cw) for cw in range(1, 53)])
     year = forms.ChoiceField(label="Jahr", initial=timezone.datetime.now().year,
-                             choices=[(timezone.datetime.now().year, timezone.datetime.now().year),
-                                      (timezone.datetime.now().year + 1, timezone.datetime.now().year + 1)])
+                             choices=options_for_year)
 
-    # pdf = forms.FileField(label="PDF-Datei")
+    pdf = forms.FileField(label="PDF-Datei", validators=[FileExtensionValidator(allowed_extensions=["pdf"])])
 
     class Meta:
         model = MealPlan
diff --git a/schoolapps/mealplan/templates/menu/index.html b/schoolapps/mealplan/templates/menu/index.html
index 9fb222e97..88c73af8f 100755
--- a/schoolapps/mealplan/templates/menu/index.html
+++ b/schoolapps/mealplan/templates/menu/index.html
@@ -14,6 +14,18 @@
     <a class="waves-effect waves-light btn green" href="{% url "menu_upload" %}"><i class="material-icons left">add</i>
         Neuen Speiseplan hochladen</a>
     <h5>Übersicht der hochgeladenen Speisepläne</h5>
-
+    <ul class="collection">
+        {% for menu in menus %}
+            <li class="collection-item ">
+                <span class="title">{{ menu }}</span>
+                <p>
+                    <a class="btn-flat" href="/media/{{ menu.pdf }}" target="_blank">
+                        <i class="material-icons left">picture_as_pdf</i> PDF anzeigen
+                    </a>
+                </p>
+                <a href="#!" class="secondary-content"><i class="material-icons">grade</i></a>
+            </li>
+        {% endfor %}
+    </ul>
 </main>
 {% include 'partials/footer.html' %}
diff --git a/schoolapps/mealplan/templates/menu/upload.html b/schoolapps/mealplan/templates/menu/upload.html
index 368edc3be..dc20877f3 100755
--- a/schoolapps/mealplan/templates/menu/upload.html
+++ b/schoolapps/mealplan/templates/menu/upload.html
@@ -14,7 +14,7 @@
                 {{ form.calendar_week }}
             </div>
             <div class="col s2 center-align">
-                <p>&nbsp;</p>
+                <small>&nbsp;</small>
                 <h4>/</h4>
             </div>
             <div class="input-field col s5">
diff --git a/schoolapps/mealplan/views.py b/schoolapps/mealplan/views.py
index 55700604a..a73d6e873 100644
--- a/schoolapps/mealplan/views.py
+++ b/schoolapps/mealplan/views.py
@@ -1,4 +1,6 @@
 from django.shortcuts import render, redirect
+
+from mealplan.models import MealPlan
 from .forms import MenuUploadForm
 
 
@@ -18,4 +20,5 @@ def upload(request):
 
 
 def index(request, msg=None):
-    return render(request, 'menu/index.html', {"msg": msg})
+    menus = MealPlan.objects.all().order_by("calendar_week", "year")
+    return render(request, 'menu/index.html', {"msg": msg, "menus": menus})
-- 
GitLab