From a549e87a141459ca3ba770920b46bf1483a93b47 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sun, 1 Sep 2019 20:44:28 +0200
Subject: [PATCH] Fix up view for lesson substitution editing. Closes #12.

---
 biscuit/apps/chronos/tables.py |  4 ++--
 biscuit/apps/chronos/urls.py   |  2 +-
 biscuit/apps/chronos/views.py  | 16 ++++++++++------
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/biscuit/apps/chronos/tables.py b/biscuit/apps/chronos/tables.py
index 2492ca1f..38625021 100644
--- a/biscuit/apps/chronos/tables.py
+++ b/biscuit/apps/chronos/tables.py
@@ -26,8 +26,8 @@ class LessonsTable(tables.Table):
     lesson__teachers = tables.Column(accessor='lesson.teacher_names', verbose_name=_('Teachers'))
     lesson__subject = tables.Column(accessor='lesson.subject')
     room = tables.Column(accessor='room')
-    edit = tables.LinkColumn('edit_substitution_by_id', args=[A('id')])
+    edit_substitution = tables.LinkColumn('edit_substitution_by_id', args=[A('id')], text=_('Substitution'))
 
     def __init__(self, week, *args, **kwargs):
-        self._week = week
+        self.edit_substitution.args.append(week)
         super().__init__(*args, **kwargs)
diff --git a/biscuit/apps/chronos/urls.py b/biscuit/apps/chronos/urls.py
index 6720c5c3..87e879b7 100644
--- a/biscuit/apps/chronos/urls.py
+++ b/biscuit/apps/chronos/urls.py
@@ -7,5 +7,5 @@ urlpatterns = [
     path('timetable', views.timetable, name='timetable'),
     path('lessons', views.lessons_day, name='lessons_day'),
     path('lessons/<when>', views.lessons_day, name='lessons_day_by_date'),
-    path('substitutions/<id>/edit', views.edit_substitution, name='edit_substitution')
+    path('lessons/<int:id_>/<int:week>/substition', views.edit_substitution, name='edit_substitution')
 ]
diff --git a/biscuit/apps/chronos/views.py b/biscuit/apps/chronos/views.py
index f0579acf..bbb56809 100644
--- a/biscuit/apps/chronos/views.py
+++ b/biscuit/apps/chronos/views.py
@@ -14,8 +14,8 @@ from biscuit.core.decorators import admin_required
 from biscuit.core.models import Group, Person
 
 from .forms import SelectForm, LessonSubstitutionForm
-from .models import LessonPeriod, TimePeriod, Room
-from .util import current_week, week_weekday_from_date
+from .models import LessonPeriod, TimePeriod, Room, LessonSubstitution
+from .util import current_week, week_weekday_from_date, week_days
 from .tables import LessonsTable
 
 
@@ -115,12 +115,16 @@ def lessons_day(request: HttpRequest, when: Optional[str] = None) -> HttpRespons
     return render(request, 'chronos/lessons_day.html', context)
 
 @admin_required
-def edit_substitution(request: HttpRequest, id_: int) -> HttpResponse:
+def edit_substitution(request: HttpRequest, id_: int, week: int) -> HttpResponse:
     context = {}
 
-    substitution = get_object_or_404(Substitution, id=id_)
+    lesson_period = get_object_or_404(LessonPeriod, id_)
 
-    edit_substitution_form = LessonSubstitutionForm(request.POST or None, instance=substitution)
+    lesson_substitution = LessonSubstitution.objects.filter(week=week, lesson_period=lesson_period).first()
+    if lesson_substitution:
+        edit_substitution_form = LessonSubstitutionForm(request.POST or None, instance=lesson_substitution)
+    else:
+        edit_substitution_form = LessonSubstitutionForm(request.POST or None, initial={'week': week, 'lesson_period': lesson_period})
 
     context['substitution'] = substitution
 
@@ -129,7 +133,7 @@ def edit_substitution(request: HttpRequest, id_: int) -> HttpResponse:
             edit_substitution_form.save(commit=True)
 
             messages.success(request, _('The substitution has been saved.'))
-            return redirect('edit_substitution_by_id', id_=substitution.id)
+            return redirect('lessons_day_by_date', when=week_days(week)[lesson_period.period.weekday].strftime('%Y-%m-%d'))
 
     context['edit_substitution_form'] = edit_substitution_form
 
-- 
GitLab