From 7311df9a36e23ee5b2839bddec7b7ee591cea77c Mon Sep 17 00:00:00 2001
From: HanseGucker <joniweth@gmx.de>
Date: Sat, 20 Apr 2019 20:31:04 +0200
Subject: [PATCH] Add deleting and editing to hint management

---
 .../timetable/{addhint.html => hintform.html} | 11 ++++--
 .../chronos/templates/timetable/hints.html    | 15 ++++++--
 biscuit/apps/chronos/urls.py                  |  7 ++--
 biscuit/apps/chronos/views.py                 | 36 +++++++++++++++++--
 4 files changed, 60 insertions(+), 9 deletions(-)
 rename biscuit/apps/chronos/templates/timetable/{addhint.html => hintform.html} (93%)

diff --git a/biscuit/apps/chronos/templates/timetable/addhint.html b/biscuit/apps/chronos/templates/timetable/hintform.html
similarity index 93%
rename from biscuit/apps/chronos/templates/timetable/addhint.html
rename to biscuit/apps/chronos/templates/timetable/hintform.html
index 15a9231a..5251dadd 100755
--- a/biscuit/apps/chronos/templates/timetable/addhint.html
+++ b/biscuit/apps/chronos/templates/timetable/hintform.html
@@ -5,7 +5,13 @@
 {% load widget_tweaks %}
 
 <main>
-    <h4>Neuen Hinweis erstellen</h4>
+    <h4>
+        {% if mode == "new" %}
+            Neuen Hinweis erstellen
+        {% else %}
+            Hinweis bearbeiten
+        {% endif %}
+    </h4>
 
     {% if msg == "success" %}
         <div class="alert success">
@@ -100,7 +106,8 @@
 
 
         <button type="submit" class="waves-effect waves-light btn green">
-            <i class="material-icons left">add</i> Hinweis erstellen und veröffentlichen
+            <i class="material-icons left">save</i> Hinweis {% if mode == "new" %} erstellen und
+            veröffentlichen {% else %} aktualisieren {% endif %}
         </button>
     </form>
 
diff --git a/biscuit/apps/chronos/templates/timetable/hints.html b/biscuit/apps/chronos/templates/timetable/hints.html
index c1a5a711..b7930785 100755
--- a/biscuit/apps/chronos/templates/timetable/hints.html
+++ b/biscuit/apps/chronos/templates/timetable/hints.html
@@ -3,6 +3,15 @@
 {% load martortags %}
 
 <main>
+    {% if msg %}
+        <div class="alert success">
+            <p>
+                <i class="material-icons left">check_circle</i>
+                Der Hinweis wurde erfolgreich {% if msg == "success_edit" %}gespeichert. {% else %}
+                gelöscht. {% endif %}
+            </p>
+        </div>
+    {% endif %}
     <h4>Hinweise</h4>
 
 
@@ -55,11 +64,13 @@
                 <div class="collapsible-body row">
                     {#                    <div class="col s12 m4">#}
                     <div class="right">
-                        <a class="btn-flat waves-effect waves-teal green-text">
+                        <a class="btn-flat waves-effect waves-teal green-text"
+                           href="{% url "timetable_edit_hint" hint.id %}">
                             <i class="material-icons left">edit</i>
                             <span class="hide-on-small-only">Bearbeiten</span>
                         </a>
-                        <a class="btn-flat waves-effect waves-teal red-text">
+                        <a class="btn-flat waves-effect waves-teal red-text delete-button"
+                           href="{% url "timetable_delete_hint" hint.id %}">
                             <i class="material-icons left">delete</i>
                             <span class="hide-on-small-only">Löschen</span>
                         </a>
diff --git a/biscuit/apps/chronos/urls.py b/biscuit/apps/chronos/urls.py
index 2c40960d..e5f64712 100755
--- a/biscuit/apps/chronos/urls.py
+++ b/biscuit/apps/chronos/urls.py
@@ -2,6 +2,10 @@ from django.urls import path
 from . import views
 
 urlpatterns = [
+    path('hints', views.hints, name="timetable_hints"),
+    path('hints/add', views.add_hint, name="timetable_add_hint"),
+    path('hints/<int:id>/edit', views.edit_hint, name="timetable_edit_hint"),
+    path('hints/<int:id>/delete', views.delete_hint, name="timetable_delete_hint"),
     path('', views.all, name='timetable_admin_all'),
     path('my', views.my_plan, name='timetable_my_plan'),
     path('my/<int:year>/<int:month>/<int:day>/', views.my_plan, name='timetable_my_plan'),
@@ -14,6 +18,5 @@ urlpatterns = [
     path('substitutions/', views.substitutions, name='timetable_substitutions'),
     path('substitutions/<int:year>/<int:month>/<int:day>/', views.substitutions, name='timetable_substitutions_date'),
     path('class.pdf', views.sub_pdf, name="timetable_substitutions_pdf"),
-    path('hints', views.hints, name="timetable_hints"),
-    path('hints/add', views.add_hint, name="timetable_add_hint"),
+
 ]
diff --git a/biscuit/apps/chronos/views.py b/biscuit/apps/chronos/views.py
index 7e60c31b..138079ae 100755
--- a/biscuit/apps/chronos/views.py
+++ b/biscuit/apps/chronos/views.py
@@ -4,7 +4,7 @@ import os
 from PyPDF2 import PdfFileMerger
 from django.contrib.auth.decorators import login_required, permission_required
 from django.http import Http404, FileResponse
-from django.shortcuts import render, redirect
+from django.shortcuts import render, redirect, get_object_or_404
 from django.utils import timezone
 from material import Fieldset, Row
 
@@ -261,8 +261,12 @@ def substitutions(request, year=None, day=None, month=None):
 @permission_required("timetable.can_view_hint")
 def hints(request):
     f = HintFilter(request.GET, queryset=Hint.objects.all())
+    msg = None
+    if request.session.get("msg", False):
+        msg = request.session["msg"]
+        request.session["msg"] = None
     # f.form.layout = Fieldset("Hi", Row("from_date", "to_date", "classes", "teachers"))
-    return render(request, "timetable/hints.html", {"f": f})
+    return render(request, "timetable/hints.html", {"f": f, "msg": msg})
 
 
 @login_required
@@ -280,4 +284,30 @@ def add_hint(request):
     else:
         form = HintForm()
 
-    return render(request, 'timetable/addhint.html', {'form': form, "martor": True, "msg": msg})
+    return render(request, 'timetable/hintform.html', {'form': form, "martor": True, "msg": msg, "mode": "new"})
+
+
+@login_required
+@permission_required("timetable.can_edit_hint")
+def edit_hint(request, id):
+    hint = get_object_or_404(Hint, pk=id)
+    if request.method == 'POST':
+        form = HintForm(request.POST, instance=hint)
+
+        if form.is_valid():
+            form.save()
+            request.session["msg"] = "success_edit"
+            return redirect('timetable_hints')
+    else:
+        form = HintForm(instance=hint)
+
+    return render(request, 'timetable/hintform.html', {'form': form, "martor": True, "mode": "edit"})
+
+
+@login_required
+@permission_required("timetable.can_delete_hint")
+def delete_hint(request, id):
+    hint = get_object_or_404(Hint, pk=id)
+    hint.delete()
+    request.session["msg"] = "success_delete"
+    return redirect('timetable_hints')
-- 
GitLab