Skip to content
Snippets Groups Projects
Verified Commit bf405297 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Use LiveDocument as base class for AutomaticPlan

parent 2cf36964
No related branches found
No related tags found
1 merge request!191Resolve "Automatically create a PDF file of the substitution plan"
# Generated by Django 3.2.4 on 2021-07-23 19:48
# Generated by Django 3.2.5 on 2021-08-03 18:30
import django.contrib.sites.managers
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
......@@ -9,8 +8,8 @@ import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('resint', '0006_livedocument'),
('reversion', '0001_squashed_0004_auto_20160611_1202'),
('sites', '0002_alter_domain_unique'),
('chronos', '0008_unique_constraints'),
]
......@@ -18,28 +17,16 @@ class Migration(migrations.Migration):
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')),
('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', 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')),
('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',
},
managers=[
('objects', django.contrib.sites.managers.CurrentSiteManager()),
],
),
migrations.AddConstraint(
model_name='automaticplan',
constraint=models.UniqueConstraint(fields=('site_id', 'slug'), name='site_slug'),
bases=('resint.livedocument',),
),
]
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment