Skip to content
Snippets Groups Projects
Commit b73c9a3d authored by Julian's avatar Julian
Browse files

Move tardiness to Participationstatus, simplify constraints

parent 7167e1f8
No related branches found
No related tags found
1 merge request!362Resolve "Add personal note management dialog in course book"
# Generated by Django 4.2.10 on 2024-06-18 09:44 # Generated by Django 4.2.10 on 2024-07-01 13:44
# Updated for more custom logic # Updated for more custom logic
from django.db import migrations, models from django.db import migrations, models
def forwards__one_value_per_note(apps, schema_editor):
# We get the model from the versioned app registry;
# if we directly import it, it'll be the wrong version
NewPersonalNote = apps.get_model("alsijil", "NewPersonalNote") # noqa
db_alias = schema_editor.connection.alias
NewPersonalNote.objects.using(db_alias).filter(note="").update(note=None)
def reverse__one_value_per_note(apps, schema_editor):
NewPersonalNote = apps.get_model("alsijil", "NewPersonalNote") # noqa
db_alias = schema_editor.connection.alias
NewPersonalNote.objects.using(db_alias).filter(note=None).update(note="")
def forwards__unique_extra_mark_documentation(apps, schema_editor): def forwards__unique_extra_mark_documentation(apps, schema_editor):
NewPersonalNote = apps.get_model("alsijil", "NewPersonalNote") # noqa NewPersonalNote = apps.get_model("alsijil", "NewPersonalNote") # noqa
db_alias = schema_editor.connection.alias db_alias = schema_editor.connection.alias
...@@ -39,10 +23,12 @@ def forwards__unique_extra_mark_documentation(apps, schema_editor): ...@@ -39,10 +23,12 @@ def forwards__unique_extra_mark_documentation(apps, schema_editor):
)[1:] )[1:]
NewPersonalNote.objects.using(db_alias).filter(pk__in=pks).delete() NewPersonalNote.objects.using(db_alias).filter(pk__in=pks).delete()
def reverse__unique_extra_mark_documentation(apps, schema_editor): def reverse__unique_extra_mark_documentation(apps, schema_editor):
# Nothing to do, we cannot bring back the deleted objects, but they were duplicate data, so they are not needed anyway. # Nothing to do, we cannot bring back the deleted objects, but they were duplicate data, so they are not needed anyway.
pass pass
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('alsijil', '0022_documentation_participation_touched_at'), ('alsijil', '0022_documentation_participation_touched_at'),
...@@ -54,35 +40,20 @@ class Migration(migrations.Migration): ...@@ -54,35 +40,20 @@ class Migration(migrations.Migration):
name='unique_absence_per_documentation', name='unique_absence_per_documentation',
), ),
migrations.AddField( migrations.AddField(
model_name='newpersonalnote', model_name='participationstatus',
name='tardiness', name='tardiness',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='Tardiness'), field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='Tardiness'),
), ),
migrations.AlterField( migrations.AlterField(
model_name='newpersonalnote', model_name='newpersonalnote',
name='note', name='note',
field=models.TextField(blank=True, null=True, verbose_name='Note'), field=models.TextField(blank=True, default='', verbose_name='Note'),
), ),
migrations.RunPython(forwards__one_value_per_note, reverse__one_value_per_note),
migrations.AddConstraint( migrations.AddConstraint(
model_name='newpersonalnote', model_name='newpersonalnote',
constraint=models.CheckConstraint( constraint=models.CheckConstraint(
check=models.Q( check=models.Q(models.Q(('note', ''), _negated=True), ('extra_mark__isnull', False), _connector='OR'),
models.Q(('extra_mark__isnull', True), ('note__isnull', False), ('tardiness__isnull', True)), name='either_note_or_extra_mark_per_note'),
models.Q(('extra_mark__isnull', False), ('note__isnull', True), ('tardiness__isnull', True)),
models.Q(('extra_mark__isnull', True), ('note__isnull', True), ('tardiness__isnull', False)),
_connector='OR'
),
name='one_value_per_personal_note'
),
),
migrations.AddConstraint(
model_name='newpersonalnote',
constraint=models.UniqueConstraint(
condition=models.Q(('tardiness', None), _negated=True),
fields=('person', 'documentation'),
name='unique_person_documentation_tardiness'
),
), ),
migrations.RunPython(forwards__unique_extra_mark_documentation, reverse__unique_extra_mark_documentation), migrations.RunPython(forwards__unique_extra_mark_documentation, reverse__unique_extra_mark_documentation),
migrations.AddConstraint( migrations.AddConstraint(
...@@ -90,7 +61,7 @@ class Migration(migrations.Migration): ...@@ -90,7 +61,7 @@ class Migration(migrations.Migration):
constraint=models.UniqueConstraint( constraint=models.UniqueConstraint(
condition=models.Q(('extra_mark', None), _negated=True), condition=models.Q(('extra_mark', None), _negated=True),
fields=('person', 'documentation', 'extra_mark'), fields=('person', 'documentation', 'extra_mark'),
name='unique_person_documentation_extra_mark' name='unique_person_documentation_extra_mark',
), violation_error_message='A person got assigned the same extra mark multiple times per documentation.'),
), ),
] ]
...@@ -790,6 +790,8 @@ class ParticipationStatus(CalendarEvent): ...@@ -790,6 +790,8 @@ class ParticipationStatus(CalendarEvent):
verbose_name=_("Base Absence"), verbose_name=_("Base Absence"),
) )
tardiness = models.PositiveSmallIntegerField(blank=True, null=True, verbose_name=_("Tardiness"))
def fill_from_kolego(self, kolego_absence: KolegoAbsence): def fill_from_kolego(self, kolego_absence: KolegoAbsence):
"""Take over data from a Kolego absence.""" """Take over data from a Kolego absence."""
self.base_absence = kolego_absence self.base_absence = kolego_absence
...@@ -828,33 +830,27 @@ class NewPersonalNote(ExtensibleModel): ...@@ -828,33 +830,27 @@ class NewPersonalNote(ExtensibleModel):
null=True, null=True,
) )
note = models.TextField(blank=True, null=True, verbose_name=_("Note")) note = models.TextField(blank=True, default="", verbose_name=_("Note"))
extra_mark = models.ForeignKey( extra_mark = models.ForeignKey(
ExtraMark, on_delete=models.PROTECT, blank=True, null=True, verbose_name=_("Extra Mark") ExtraMark, on_delete=models.PROTECT, blank=True, null=True, verbose_name=_("Extra Mark")
) )
tardiness = models.PositiveSmallIntegerField(blank=True, null=True, verbose_name=_("Tardiness"))
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.person}, {self.note}, {self.extra_mark}, {self.tardiness}" return f"{self.person}, {self.note}, {self.extra_mark}"
class Meta: class Meta:
verbose_name = _("Personal Note") verbose_name = _("Personal Note")
verbose_name_plural = _("Personal Notes") verbose_name_plural = _("Personal Notes")
constraints = [ constraints = [
# This constraint could be dropped in future scenarios
models.CheckConstraint( models.CheckConstraint(
check=Q(note__isnull=False, extra_mark__isnull=True, tardiness__isnull=True) check=~Q(note="") | Q(extra_mark__isnull=False),
| Q(note__isnull=True, extra_mark__isnull=False, tardiness__isnull=True) name="either_note_or_extra_mark_per_note",
| Q(note__isnull=True, extra_mark__isnull=True, tardiness__isnull=False),
name="one_value_per_personal_note",
),
models.UniqueConstraint(
fields=["person", "documentation"],
name="unique_person_documentation_tardiness",
condition=~Q(tardiness=None),
), ),
models.UniqueConstraint( models.UniqueConstraint(
fields=["person", "documentation", "extra_mark"], fields=["person", "documentation", "extra_mark"],
name="unique_person_documentation_extra_mark", name="unique_person_documentation_extra_mark",
violation_error_message=_("A person got assigned the same extra mark multiple times per documentation."),
condition=~Q(extra_mark=None), condition=~Q(extra_mark=None),
), ),
] ]
......
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