From d2ed51d9418061ad1da0f57f7609ab38d0b999a7 Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Wed, 21 Aug 2019 17:50:56 +0200 Subject: [PATCH] Link substitutions to week number and period. --- .../migrations/0004_auto_20190821_1550.py | 30 +++++++++++++++++++ biscuit/apps/chronos/models.py | 25 +++++++++++----- biscuit/apps/chronos/util.py | 5 ++++ biscuit/apps/chronos/views.py | 2 ++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 biscuit/apps/chronos/migrations/0004_auto_20190821_1550.py create mode 100644 biscuit/apps/chronos/util.py diff --git a/biscuit/apps/chronos/migrations/0004_auto_20190821_1550.py b/biscuit/apps/chronos/migrations/0004_auto_20190821_1550.py new file mode 100644 index 00000000..af742992 --- /dev/null +++ b/biscuit/apps/chronos/migrations/0004_auto_20190821_1550.py @@ -0,0 +1,30 @@ +# Generated by Django 2.2.4 on 2019-08-21 15:50 + +import biscuit.apps.chronos.util +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('chronos', '0003_lesson_substitution'), + ] + + operations = [ + migrations.RemoveField( + model_name='lessonperiod', + name='substitution', + ), + migrations.AddField( + model_name='lessonsubstitution', + name='lesson_period', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='substitutions', to='chronos.LessonPeriod'), + preserve_default=False, + ), + migrations.AddField( + model_name='lessonsubstitution', + name='week', + field=models.IntegerField(default=biscuit.apps.chronos.util.current_week, verbose_name='Kalenderwoche'), + ), + ] diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py index 3842b5e4..82680c95 100644 --- a/biscuit/apps/chronos/models.py +++ b/biscuit/apps/chronos/models.py @@ -2,6 +2,8 @@ from django.core import validators from django.db import models from django.utils.translation import ugettext_lazy as _ +from .util import current_week + class TimePeriod(models.Model): WEEKDAY_CHOICES = [ @@ -82,24 +84,25 @@ class LessonPeriod(models.Model): room = models.ForeignKey('Room', models.CASCADE, null=True) - substitution = models.OneToOneField('LessonSubstitution', models.CASCADE, - related_name='lesson_period', null=True) + def get_substitution(self, week=None): + wanted_week = week or current_week() + return self.substitutions.filter(week=wanted_week).first() def get_subject(self): - if self.substitution: - return self.substitution.subject + if self.get_substitution(): + return self.get_substitution().subject else: return self.lesson.subject def get_teachers(self): - if self.substitution: - return self.substitution.teachers + if self.get_substitution(): + return self.get_substitution().teachers else: return self.lesson.teachers def get_room(self): - if self.substitution: - return self.substitution.room + if self.get_substitution(): + return self.get_substitution().room else: return self.room @@ -108,6 +111,12 @@ class LessonPeriod(models.Model): class LessonSubstitution(models.Model): + week = models.IntegerField(verbose_name=_('Kalenderwoche'), + default=current_week) + + lesson_period = models.ForeignKey( + 'LessonPeriod', models.CASCADE, 'substitutions') + subject = models.ForeignKey( 'Subject', on_delete=models.CASCADE, related_name='lesson_substitutions', null=True) diff --git a/biscuit/apps/chronos/util.py b/biscuit/apps/chronos/util.py new file mode 100644 index 00000000..8d5c781a --- /dev/null +++ b/biscuit/apps/chronos/util.py @@ -0,0 +1,5 @@ +from datetime import datetime + + +def current_week(): + return int(datetime.now().strftime('%V')) diff --git a/biscuit/apps/chronos/views.py b/biscuit/apps/chronos/views.py index e4bb8898..7822866a 100644 --- a/biscuit/apps/chronos/views.py +++ b/biscuit/apps/chronos/views.py @@ -9,6 +9,7 @@ from biscuit.core.decorators import admin_required from biscuit.core.models import Group, Person from .models import LessonPeriod, TimePeriod, Room +from .util import current_week @login_required @@ -73,5 +74,6 @@ def timetable(request): context['filter_descs'] = ', '.join(filter_descs) context['periods'] = TimePeriod.get_times_dict() context['weekdays'] = dict(TimePeriod.WEEKDAY_CHOICES) + context['current_week'] = current_week() return render(request, 'chronos/tt_week.html', context) -- GitLab