Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 5.1.7 on 2025-04-15 07:26

import vng_api_common.fields
from django.db import migrations
from vng_api_common.constants import VA_MAPPING, REVERSE_VA_MAPPING


def populate_max_vertrouwelijkheidaanduiding_integer_field(apps, schema_editor):
Autorisatie = apps.get_model("authorizations", "Autorisatie")
BATCH_SIZE = 2000
batch = []
for instance in Autorisatie.objects.iterator():
if not (max_va := VA_MAPPING.get(instance.max_vertrouwelijkheidaanduiding)):
continue

instance._max_vertrouwelijkheidaanduiding = max_va
batch.append(instance)

if len(batch) >= BATCH_SIZE:
Autorisatie.objects.bulk_update(batch, ["_max_vertrouwelijkheidaanduiding"])
batch.clear()

# Handle final batch
if batch:
Autorisatie.objects.bulk_update(batch, ["_max_vertrouwelijkheidaanduiding"])


def populate_max_vertrouwelijkheidaanduiding_char_field(apps, schema_editor):
Autorisatie = apps.get_model("authorizations", "Autorisatie")
BATCH_SIZE = 2000
batch = []
for instance in Autorisatie.objects.filter(
max_vertrouwelijkheidaanduiding=""
).iterator():
if not (
max_va := REVERSE_VA_MAPPING.get(instance._max_vertrouwelijkheidaanduiding)
):
continue

instance.max_vertrouwelijkheidaanduiding = max_va
batch.append(instance)

if len(batch) >= BATCH_SIZE:
Autorisatie.objects.bulk_update(batch, ["max_vertrouwelijkheidaanduiding"])
batch.clear()

# Handle final batch
if batch:
Autorisatie.objects.bulk_update(batch, ["max_vertrouwelijkheidaanduiding"])


class Migration(migrations.Migration):

dependencies = [
("authorizations", "0017_alter_applicatie_client_ids_and_more"),
]

operations = [
migrations.AddField(
model_name="autorisatie",
name="_max_vertrouwelijkheidaanduiding",
field=vng_api_common.fields.VertrouwelijkheidsAanduidingFieldInt(
blank=True,
choices=[
(10, "Openbaar"),
(20, "Beperkt openbaar"),
(30, "Intern"),
(40, "Zaakvertrouwelijk"),
(50, "Vertrouwelijk"),
(60, "Confidentieel"),
(70, "Geheim"),
(80, "Zeer geheim"),
],
db_index=True,
help_text="Maximaal toegelaten vertrouwelijkheidaanduiding (inclusief).",
null=True,
),
),
migrations.RunPython(
populate_max_vertrouwelijkheidaanduiding_integer_field,
populate_max_vertrouwelijkheidaanduiding_char_field,
),
]
11 changes: 10 additions & 1 deletion vng_api_common/authorizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

from ..constants import ComponentTypes, VertrouwelijkheidsAanduiding
from ..decorators import field_default
from ..fields import VertrouwelijkheidsAanduidingField
from ..fields import (
VertrouwelijkheidsAanduidingField,
VertrouwelijkheidsAanduidingFieldInt,
)
from ..models import APIMixin


Expand Down Expand Up @@ -158,6 +161,12 @@ class Autorisatie(APIMixin, models.Model):
help_text=_("Maximaal toegelaten vertrouwelijkheidaanduiding (inclusief)."),
blank=True,
)
_max_vertrouwelijkheidaanduiding = VertrouwelijkheidsAanduidingFieldInt(
help_text=_("Maximaal toegelaten vertrouwelijkheidaanduiding (inclusief)."),
blank=True,
null=True,
db_index=True,
)

objects = AutorisatieManager()

Expand Down
25 changes: 25 additions & 0 deletions vng_api_common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
VERZOEK_CONST = "verzoek"
VERZOEK_CHOICE = VERZOEK_CONST, _("Verzoek")

VA_MAPPING: dict[str, int] = {
"openbaar": 10,
"beperkt_openbaar": 20,
"intern": 30,
"zaakvertrouwelijk": 40,
"vertrouwelijk": 50,
"confidentieel": 60,
"geheim": 70,
"zeer_geheim": 80,
}
REVERSE_VA_MAPPING: dict[int, str] = {
int_value: str_value for str_value, int_value in VA_MAPPING.items()
}


class VertrouwelijkheidsAanduiding(models.TextChoices):
openbaar = "openbaar", _("Openbaar")
Expand Down Expand Up @@ -54,6 +68,17 @@ def get_choice_order(cls, value) -> Optional[int]:
return orders.get(value)


class VertrouwelijkheidsAanduidingInt(models.IntegerChoices):
openbaar = 10, _("Openbaar")
beperkt_openbaar = 20, _("Beperkt openbaar")
intern = 30, _("Intern")
zaakvertrouwelijk = 40, _("Zaakvertrouwelijk")
vertrouwelijk = 50, _("Vertrouwelijk")
confidentieel = 60, _("Confidentieel")
geheim = 70, _("Geheim")
zeer_geheim = 80, _("Zeer geheim")


class RolOmschrijving(TextChoicesWithDescriptions):
adviseur = "adviseur", _("Adviseur")
behandelaar = "behandelaar", _("Behandelaar")
Expand Down
36 changes: 35 additions & 1 deletion vng_api_common/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

from iso639 import iter_langs

from .constants import BSN_LENGTH, RSIN_LENGTH, VertrouwelijkheidsAanduiding
from .constants import (
BSN_LENGTH,
RSIN_LENGTH,
VertrouwelijkheidsAanduidingInt,
VertrouwelijkheidsAanduiding,
)
from .validators import validate_bsn, validate_rsin

LANGUAGE_CHOICES = tuple([(lg.pt2b, lg.name) for lg in iter_langs() if lg.pt2b])
Expand Down Expand Up @@ -99,6 +104,13 @@ def _check_choices(self, **kwargs):


class VertrouwelijkheidsAanduidingField(models.CharField):
"""
XXX: deprecation

For backwards compatibility in older migrations, should be deprecated in next major
release
"""

def __init__(self, *args, **kwargs):
kwargs.setdefault("max_length", 20)
kwargs.setdefault("choices", VertrouwelijkheidsAanduiding.choices)
Expand All @@ -121,6 +133,28 @@ def _check_choices(self, **kwargs):
return []


class VertrouwelijkheidsAanduidingFieldInt(models.IntegerField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("choices", VertrouwelijkheidsAanduiding.choices)
super().__init__(*args, **kwargs)

def check(self, **kwargs):
errors = super().check(**kwargs)
errors.extend(self._check_choices(**kwargs))
return errors

def _check_choices(self, **kwargs):
if self.choices != VertrouwelijkheidsAanduiding.choices:
return [
checks.Error(
"VertrouwelijkheidsAanduidingField may not override 'choices' attribute.",
obj=self,
id="vng_api_common.fields.E005",
)
]
return []


class DaysDurationField(models.DurationField):
"""
Express duration in number of calendar days.
Expand Down
Loading