From fb4fd70bb05c23258c456768a9022a4a87203e4f Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Fri, 23 Aug 2019 09:35:21 +0200 Subject: [PATCH] Implement personal notes editing in lesson. --- biscuit/apps/alsijil/forms.py | 6 ++++- biscuit/apps/alsijil/models.py | 2 +- .../alsijil/templates/alsijil/lesson.html | 27 ++++++++++++++++--- biscuit/apps/alsijil/views.py | 17 +++++++++++- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/biscuit/apps/alsijil/forms.py b/biscuit/apps/alsijil/forms.py index f95b379b5..877863eb3 100644 --- a/biscuit/apps/alsijil/forms.py +++ b/biscuit/apps/alsijil/forms.py @@ -1,10 +1,14 @@ from django import forms from django.utils.translation import ugettext_lazy as _ -from .models import LessonDocumentation +from .models import LessonDocumentation, PersonalNote class LessonDocumentationForm(forms.ModelForm): class Meta: model = LessonDocumentation fields = ['topic', 'homework'] + + +PersonalNoteFormSet = forms.modelformset_factory( + PersonalNote, fields=('person', 'absent', 'late', 'excused', 'remarks')) diff --git a/biscuit/apps/alsijil/models.py b/biscuit/apps/alsijil/models.py index 5e9094d75..265d2e33d 100644 --- a/biscuit/apps/alsijil/models.py +++ b/biscuit/apps/alsijil/models.py @@ -5,7 +5,7 @@ from biscuit.core.mixins import SchoolRelated class PersonalNote(SchoolRelated): - person = models.ForeignKey('core.Person', models.CASCADE) + person = models.ForeignKey('core.Person', models.CASCADE, editable=False) week = models.IntegerField() lesson_period = models.ForeignKey('chronos.LessonPeriod', models.CASCADE) diff --git a/biscuit/apps/alsijil/templates/alsijil/lesson.html b/biscuit/apps/alsijil/templates/alsijil/lesson.html index c4c59af63..963f2997d 100644 --- a/biscuit/apps/alsijil/templates/alsijil/lesson.html +++ b/biscuit/apps/alsijil/templates/alsijil/lesson.html @@ -39,9 +39,30 @@ {% blocktrans %}Personal notes{% endblocktrans %} </div> <div class="card-body"> - <p class="card-text"> - This feature is still missing. - </p> + <form method="post"> + {% csrf_token %} + <table class="table table-striped table-bordered table-hover table-condensed table-responsive w-100"> + <tr> + <th>{% blocktrans %}Person{% endblocktrans %}</th> + <th>{% blocktrans %}Absent{% endblocktrans %}</th> + <th>{% blocktrans %}Tardiness{% endblocktrans %}</th> + <th>{% blocktrans %}Excused{% endblocktrans %}</th> + <th>{% blocktrans %}Remarks{% endblocktrans %}</th> + </tr> + {% for form in personal_note_formset %} + <tr> + <td>{{ form.person }}</td> + <td>{{ form.absent }}</td> + <td>{{ form.late }}</td> + <td>{{ form.excused }}</td> + <td>{{ form.remarks }}</td> + </tr> + {% endfor %} + </table> + + <input type="hidden" name="action" value="personal_notes" /> + <input type="submit" value="Update" /> + </form> </div> </div> {% endblock %} diff --git a/biscuit/apps/alsijil/views.py b/biscuit/apps/alsijil/views.py index 84a4c9b66..2358624a6 100644 --- a/biscuit/apps/alsijil/views.py +++ b/biscuit/apps/alsijil/views.py @@ -9,7 +9,7 @@ from biscuit.apps.chronos.models import LessonPeriod from biscuit.apps.chronos.util import current_lesson_periods, current_week from .forms import LessonDocumentationForm -from .models import LessonDocumentation +from .models import LessonDocumentation, PersonalNote @login_required @@ -34,12 +34,27 @@ def lesson(request: HttpRequest, week: Optional[int] = None, period_id: Optional lesson_documentation, created = LessonDocumentation.objects.get_or_create(lesson_period=lesson_period, week=wanted_week) lesson_documentation_form = LessonDocumentationForm(request.POST or None, instance=lesson_documentation) + # Create all missing personal notes about members of all groups in lesson + for group in lesson_period.lesson.groups: + for person in group.members: + note, created = PersonalNote.objects.get_or_create(person=person, lesson_period=lesson_period, + week=wanted_week) + + # Create a formset that holds all personal notes for all persons in this lesson + persons_qs = PersonalNote.objects.filter(lesson_period=lesson_period, week=wanted_week) + personal_note_formset = PersonalNoteFormSet(request.POST or None, queryset=persons_qs) + if request.method == 'POST': if request.POST.get('action', None) == 'lesson_documentation': # Save the lesson documentation if lesson_documentation_form.is_valid(): lesson_documentation_form.save() + elif request.POST.get('action', None) == 'personal_notes': + # Save all personal notes + if personal_note_formset.is_valid(): + lesson_documentation_form.save() context['lesson_documentation_form'] = lesson_documentation_form + context['personal_note_formset'] = personal_note_formset return render(request, 'alsijil/lesson.html', context) -- GitLab