From ea2cc40730d8242da9c5f2e7de6f5ce025e6c6da Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Wed, 12 Aug 2020 16:51:11 +0200
Subject: [PATCH] Move settings check for carry_data_over to save method

Makes it possible to use the function in other contexts
---
 aleksis/apps/alsijil/models.py | 59 +++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py
index b648b3f7b..a7f6485c9 100644
--- a/aleksis/apps/alsijil/models.py
+++ b/aleksis/apps/alsijil/models.py
@@ -111,44 +111,43 @@ class LessonDocumentation(ExtensibleModel):
 
         Can be deactivated using site preference ``alsijil__carry_over``.
         """
-        if get_site_preferences()["alsijil__carry_over"] and (
-            self.topic or self.homework or self.group_note
-        ):
-            try:
-                second_period = LessonPeriod.objects.get(
-                    lesson=self.lesson_period.lesson,
-                    period__weekday=self.lesson_period.period.weekday,
-                    period__period=self.lesson_period.period.period + 1,
-                )
-                lesson_documentation = second_period.get_or_create_lesson_documentation(
-                    CalendarWeek(
-                        week=self.week,
-                        year=self.lesson_period.lesson.get_year(self.week),
-                    )
+        try:
+            second_period = LessonPeriod.objects.get(
+                lesson=self.lesson_period.lesson,
+                period__weekday=self.lesson_period.period.weekday,
+                period__period=self.lesson_period.period.period + 1,
+            )
+            lesson_documentation = second_period.get_or_create_lesson_documentation(
+                CalendarWeek(
+                    week=self.week, year=self.lesson_period.lesson.get_year(self.week),
                 )
+            )
 
-                changed = False
+            changed = False
 
-                if not lesson_documentation.topic:
-                    lesson_documentation.topic = self.topic
-                    changed = True
+            if not lesson_documentation.topic:
+                lesson_documentation.topic = self.topic
+                changed = True
 
-                if not lesson_documentation.homework:
-                    lesson_documentation.homework = self.homework
-                    changed = True
+            if not lesson_documentation.homework:
+                lesson_documentation.homework = self.homework
+                changed = True
 
-                if not lesson_documentation.group_note:
-                    lesson_documentation.group_note = self.group_note
-                    changed = True
+            if not lesson_documentation.group_note:
+                lesson_documentation.group_note = self.group_note
+                changed = True
 
-                if changed:
-                    lesson_documentation.save()
-            except LessonPeriod.DoesNotExist:
-                # Do nothing if it's a single period
-                pass
+            if changed:
+                lesson_documentation.save()
+        except LessonPeriod.DoesNotExist:
+            # Do nothing if it's a single period
+            pass
 
     def save(self, *args, **kwargs):
-        self._carry_over_data()
+        if get_site_preferences()["alsijil__carry_over"] and (
+            self.topic or self.homework or self.group_note
+        ):
+            self._carry_over_data()
         super().save(*args, **kwargs)
 
     class Meta:
-- 
GitLab