diff --git a/greg/migrations/0026_remove_ouidentifier_unique_identifier_and_more.py b/greg/migrations/0026_remove_ouidentifier_unique_identifier_and_more.py new file mode 100644 index 0000000000000000000000000000000000000000..6c8478544c3d594698c328abdd473848f32c5c2f --- /dev/null +++ b/greg/migrations/0026_remove_ouidentifier_unique_identifier_and_more.py @@ -0,0 +1,50 @@ +""" +This migrations was made necessary due to OrgReg using the same short +name for multiple OrganizationUnits. + +The restriction existed to prevent multiple units having the same +legacy_stedkode. This should never be the case but this value is not +used for anything other than clarity when choosing an OrganizationUnit +from a dropdown in the frontend and should thus be safe. +""" +# Generated by Django 4.1.3 on 2022-11-29 11:44 + +from django.db import migrations, models + + +def cleanup_duplicates(apps, schema_editor): + """ + Keep most recently updated value for each name on each ou + """ + OuIdentifier = apps.get_model("greg", "OuIdentifier") + previous = None + for oui in OuIdentifier.objects.order_by("name", "orgunit", "updated"): + if ( + previous + and oui.name == previous.name + and oui.orgunit.id == previous.orgunit.id + ): + previous.delete() + previous = oui + return + + +class Migration(migrations.Migration): + + dependencies = [ + ("greg", "0025_alter_ouidentifier_source"), + ] + + operations = [ + migrations.RunPython(cleanup_duplicates), + migrations.RemoveConstraint( + model_name="ouidentifier", + name="unique_identifier", + ), + migrations.AddConstraint( + model_name="ouidentifier", + constraint=models.UniqueConstraint( + fields=("name", "orgunit"), name="unique_identifier" + ), + ), + ] diff --git a/greg/models.py b/greg/models.py index af7aabad73322295442996e6fc586186e292547b..5e23a1ccbefd2f4a963a6f6dbf815a5e340c9242 100644 --- a/greg/models.py +++ b/greg/models.py @@ -467,7 +467,9 @@ class OuIdentifier(BaseModel): class Meta: constraints = [ - models.UniqueConstraint(name="unique_identifier", fields=["name", "value"]) + models.UniqueConstraint( + name="unique_identifier", fields=["name", "orgunit"] + ) ] def __str__(self) -> str: diff --git a/greg/utils.py b/greg/utils.py index 1a2b4ba2067458d02d17b12b64d33aec23ebac00..fa61a818f4ddfd303c34f85df281607b15ad4384 100644 --- a/greg/utils.py +++ b/greg/utils.py @@ -1,6 +1,7 @@ import re import typing -from datetime import date, datetime, timedelta +from datetime import date, datetime, timedelta, timezone as dt_timezone + from rest_framework import serializers from django.db import transaction from django.conf import settings @@ -149,7 +150,7 @@ def _compute_checksum(input_digits: str) -> bool: def date_to_datetime_midnight(in_date: date) -> datetime: """Convert a date object to a datetime object with timezone utc""" - return datetime.combine(in_date, datetime.min.time(), tzinfo=timezone.utc) + return datetime.combine(in_date, datetime.min.time(), tzinfo=dt_timezone.utc) def str2date(in_date: typing.Union[str, date]):