From bf4052976fb28996c20758b510d97dc6877015aa Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Tue, 3 Aug 2021 21:06:40 +0200
Subject: [PATCH] Use LiveDocument as base class for AutomaticPlan

---
 .../chronos/migrations/0009_automatic_plan.py | 45 -------------------
 .../chronos/migrations/0009_automaticplan.py  | 32 +++++++++++++
 aleksis/apps/chronos/models.py                | 28 +++---------
 3 files changed, 37 insertions(+), 68 deletions(-)
 delete mode 100644 aleksis/apps/chronos/migrations/0009_automatic_plan.py
 create mode 100644 aleksis/apps/chronos/migrations/0009_automaticplan.py

diff --git a/aleksis/apps/chronos/migrations/0009_automatic_plan.py b/aleksis/apps/chronos/migrations/0009_automatic_plan.py
deleted file mode 100644
index 157ba3e2..00000000
--- a/aleksis/apps/chronos/migrations/0009_automatic_plan.py
+++ /dev/null
@@ -1,45 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-23 19:48
-
-import django.contrib.sites.managers
-import django.core.validators
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('reversion', '0001_squashed_0004_auto_20160611_1202'),
-        ('sites', '0002_alter_domain_unique'),
-        ('chronos', '0008_unique_constraints'),
-    ]
-
-    operations = [
-        migrations.CreateModel(
-            name='AutomaticPlan',
-            fields=[
-                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
-                ('extended_data', models.JSONField(default=dict, editable=False)),
-                ('slug', models.SlugField(help_text='This will be used for the name of the PDF file with the generated plan.', verbose_name='Slug')),
-                ('name', models.CharField(max_length=255, verbose_name='Name')),
-                ('number_of_days', models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(1)], verbose_name='Number of days shown in the plan')),
-                ('show_header_box', models.BooleanField(default=True, help_text='The header box shows affected teachers/groups.', verbose_name='Show header box')),
-                ('last_update', models.DateTimeField(blank=True, null=True, verbose_name='Date and time of the last update')),
-                ('last_update_triggered_manually', models.BooleanField(default=False, verbose_name='Was the last update triggered manually?')),
-                ('current_file', models.FileField(blank=True, null=True, upload_to='chronos/plan_pdfs/', verbose_name='Current file')),
-                ('last_revision', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='reversion.revision', verbose_name='Revision which triggered the last update')),
-                ('site', models.ForeignKey(default=1, editable=False, on_delete=django.db.models.deletion.CASCADE, to='sites.site')),
-            ],
-            options={
-                'verbose_name': 'Automatic plan',
-                'verbose_name_plural': 'Automatic plans',
-            },
-            managers=[
-                ('objects', django.contrib.sites.managers.CurrentSiteManager()),
-            ],
-        ),
-        migrations.AddConstraint(
-            model_name='automaticplan',
-            constraint=models.UniqueConstraint(fields=('site_id', 'slug'), name='site_slug'),
-        ),
-    ]
diff --git a/aleksis/apps/chronos/migrations/0009_automaticplan.py b/aleksis/apps/chronos/migrations/0009_automaticplan.py
new file mode 100644
index 00000000..26c97bb0
--- /dev/null
+++ b/aleksis/apps/chronos/migrations/0009_automaticplan.py
@@ -0,0 +1,32 @@
+# Generated by Django 3.2.5 on 2021-08-03 18:30
+
+import django.core.validators
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('resint', '0006_livedocument'),
+        ('reversion', '0001_squashed_0004_auto_20160611_1202'),
+        ('chronos', '0008_unique_constraints'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='AutomaticPlan',
+            fields=[
+                ('livedocument_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='resint.livedocument')),
+                ('number_of_days', models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(1)], verbose_name='Number of days shown in the plan')),
+                ('show_header_box', models.BooleanField(default=True, help_text='The header box shows affected teachers/groups.', verbose_name='Show header box')),
+                ('last_update_triggered_manually', models.BooleanField(default=False, verbose_name='Was the last update triggered manually?', editable=False)),
+                ('last_revision', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='reversion.revision', verbose_name='Revision which triggered the last update', editable=False)),
+            ],
+            options={
+                'verbose_name': 'Automatic plan',
+                'verbose_name_plural': 'Automatic plans',
+            },
+            bases=('resint.livedocument',),
+        ),
+    ]
diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index c57a874c..335e2bcf 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -57,6 +57,7 @@ from aleksis.apps.chronos.mixins import (
 from aleksis.apps.chronos.util.change_tracker import substitutions_changed
 from aleksis.apps.chronos.util.date import get_current_year
 from aleksis.apps.chronos.util.format import format_m2m
+from aleksis.apps.resint.models import LiveDocument
 from aleksis.core.managers import CurrentSiteManagerWithoutMigrations
 from aleksis.core.mixins import (
     ExtensibleModel,
@@ -1122,14 +1123,9 @@ class ExtraLesson(
         indexes = [models.Index(fields=["week", "year"], name="extra_lesson_week_year")]
 
 
-class AutomaticPlan(ExtensibleModel):
+class AutomaticPlan(LiveDocument):
     """Model for configuring automatically updated PDF substitution plans."""
 
-    slug = models.SlugField(
-        verbose_name=_("Slug"),
-        help_text=_("This will be used for the name of the PDF file with the generated plan."),
-    )
-    name = models.CharField(max_length=255, verbose_name=_("Name"))
     number_of_days = models.PositiveIntegerField(
         default=1,
         validators=[MinValueValidator(1)],
@@ -1146,16 +1142,11 @@ class AutomaticPlan(ExtensibleModel):
         blank=True,
         null=True,
         verbose_name=_("Revision which triggered the last update"),
+        editable=False,
     )
 
-    last_update = models.DateTimeField(
-        blank=True, null=True, verbose_name=_("Date and time of the last update")
-    )
     last_update_triggered_manually = models.BooleanField(
-        default=False, verbose_name=_("Was the last update triggered manually?")
-    )
-    current_file = models.FileField(
-        upload_to="chronos/plan_pdfs/", null=True, blank=True, verbose_name=_("Current file")
+        default=False, verbose_name=_("Was the last update triggered manually?"), editable=False
     )
 
     @property
@@ -1213,16 +1204,8 @@ class AutomaticPlan(ExtensibleModel):
             result.wait()
             file_object.refresh_from_db()
             if result.status == SUCCESS and file_object.file:
-                self.current_file.save("current.pdf", file_object.file.file)
-                self.last_update = timezone.now()
                 self.last_update_triggered_manually = triggered_manually
-                self.save()
-
-    def get_current_file(self) -> File:
-        """Get current PDF file."""
-        if not self.current_file:
-            self.update()
-        return self.current_file.file
+                super().update(file_object.file.file)
 
     @property
     def filename(self) -> str:
@@ -1232,7 +1215,6 @@ class AutomaticPlan(ExtensibleModel):
     class Meta:
         verbose_name = _("Automatic plan")
         verbose_name_plural = _("Automatic plans")
-        constraints = [models.UniqueConstraint(fields=["site_id", "slug"], name="site_slug")]
 
 
 @receiver(substitutions_changed)
-- 
GitLab