|
17 | 17 | SoortPartij,
|
18 | 18 | )
|
19 | 19 | from .mixins import BezoekadresMixin, ContactnaamMixin, CorrespondentieadresMixin
|
20 |
| -from .validators import PartijIdentificatorValidator |
| 20 | +from .validators import PartijIdentificatorValidator, UniquePartijIdentificatorValidator |
21 | 21 |
|
22 | 22 |
|
23 | 23 | class Partij(APIMixin, BezoekadresMixin, CorrespondentieadresMixin):
|
@@ -335,6 +335,15 @@ class PartijIdentificator(models.Model):
|
335 | 335 | "Unieke (technische) identificatiecode van de partij-identificator."
|
336 | 336 | ),
|
337 | 337 | )
|
| 338 | + sub_identificator_van = models.ForeignKey( |
| 339 | + "self", |
| 340 | + on_delete=models.SET_NULL, |
| 341 | + verbose_name=_("sub identificator van"), |
| 342 | + help_text=_("Expresses that one PartijIdentificator is bound to another one"), |
| 343 | + blank=True, |
| 344 | + null=True, |
| 345 | + related_name="parent_partij_identificator", |
| 346 | + ) |
338 | 347 | partij = models.ForeignKey(
|
339 | 348 | Partij,
|
340 | 349 | on_delete=models.CASCADE,
|
@@ -406,22 +415,46 @@ class PartijIdentificator(models.Model):
|
406 | 415 | class Meta:
|
407 | 416 | verbose_name = _("partij identificator")
|
408 | 417 | verbose_name_plural = _("partij identificatoren")
|
| 418 | + constraints = [ |
| 419 | + models.CheckConstraint( |
| 420 | + check=~models.Q(sub_identificator_van=models.F("id")), |
| 421 | + name="check_sub_identificator_van_not_self", |
| 422 | + ), |
| 423 | + models.UniqueConstraint( |
| 424 | + fields=[ |
| 425 | + "sub_identificator_van", |
| 426 | + "partij_identificator_code_objecttype", |
| 427 | + "partij_identificator_code_soort_object_id", |
| 428 | + "partij_identificator_object_id", |
| 429 | + "partij_identificator_code_register", |
| 430 | + ], |
| 431 | + condition=models.Q(sub_identificator_van__isnull=False), |
| 432 | + name="scoped_identificator_globally_unique", |
| 433 | + ), |
| 434 | + models.UniqueConstraint( |
| 435 | + fields=[ |
| 436 | + "partij_identificator_code_objecttype", |
| 437 | + "partij_identificator_code_soort_object_id", |
| 438 | + "partij_identificator_object_id", |
| 439 | + "partij_identificator_code_register", |
| 440 | + ], |
| 441 | + name="non_scoped_identificator_globally_unique", |
| 442 | + condition=models.Q(sub_identificator_van__isnull=True), |
| 443 | + ), |
| 444 | + ] |
409 | 445 |
|
410 | 446 | def __str__(self):
|
411 | 447 | soort_object = self.partij_identificator_code_soort_object_id
|
412 | 448 | object = self.partij_identificator_object_id
|
413 | 449 |
|
414 | 450 | return f"{soort_object} - {object}"
|
415 | 451 |
|
416 |
| - def save(self, *args, **kwargs): |
417 |
| - self.full_clean() |
418 |
| - super().save(*args, **kwargs) |
419 |
| - |
420 | 452 | def clean(self):
|
421 | 453 | super().clean()
|
422 |
| - PartijIdentificatorValidator( |
423 |
| - code_register=self.partij_identificator_code_register, |
424 |
| - code_objecttype=self.partij_identificator_code_objecttype, |
425 |
| - code_soort_object_id=self.partij_identificator_code_soort_object_id, |
426 |
| - object_id=self.partij_identificator_object_id, |
427 |
| - ).validate() |
| 454 | + PartijIdentificatorValidator()(self.partij_identificator) |
| 455 | + UniquePartijIdentificatorValidator()( |
| 456 | + { |
| 457 | + "sub_identificator_van": self.sub_identificator_van, |
| 458 | + "partij_identificator": self.partij_identificator, |
| 459 | + } |
| 460 | + ) |
0 commit comments