Skip to content

Commit

Permalink
models: Use more sensitive on_delete clauses for foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
rsebille committed Aug 8, 2024
1 parent 97df937 commit 1156086
Show file tree
Hide file tree
Showing 25 changed files with 929 additions and 80 deletions.
229 changes: 229 additions & 0 deletions itou/approvals/migrations/0004_alter_approval_created_by_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# Generated by Django 5.0.7 on 2024-08-06 09:04

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("approvals", "0003_alter_approval_updated_at"),
("companies", "0005_company_rdv_insertion_id"),
("eligibility", "0004_geiqadministrativecriteria_certifiable"),
("prescribers", "0004_poleemploi_to_francetravail"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name="approval",
name="created_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to=settings.AUTH_USER_MODEL,
verbose_name="créé par",
),
),
migrations.AlterField(
model_name="approval",
name="eligibility_diagnosis",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="eligibility.eligibilitydiagnosis",
verbose_name="diagnostic d'éligibilité",
),
),
migrations.AlterField(
model_name="approval",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="approvals",
to=settings.AUTH_USER_MODEL,
verbose_name="demandeur d'emploi",
),
),
migrations.AlterField(
model_name="prolongation",
name="created_by",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_created",
to=settings.AUTH_USER_MODEL,
verbose_name="créé par",
),
),
migrations.AlterField(
model_name="prolongation",
name="declared_by",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_declared",
to=settings.AUTH_USER_MODEL,
verbose_name="déclarée par",
),
),
migrations.AlterField(
model_name="prolongation",
name="declared_by_siae",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="companies.company",
verbose_name="SIAE du déclarant",
),
),
migrations.AlterField(
model_name="prolongation",
name="prescriber_organization",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="prescribers.prescriberorganization",
verbose_name="organisation du prescripteur habilité",
),
),
migrations.AlterField(
model_name="prolongation",
name="updated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_updated",
to=settings.AUTH_USER_MODEL,
verbose_name="modifié par",
),
),
migrations.AlterField(
model_name="prolongation",
name="validated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_validated",
to=settings.AUTH_USER_MODEL,
verbose_name="prescripteur habilité qui a autorisé cette prolongation",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="created_by",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_created",
to=settings.AUTH_USER_MODEL,
verbose_name="créé par",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="declared_by",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_declared",
to=settings.AUTH_USER_MODEL,
verbose_name="déclarée par",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="declared_by_siae",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="companies.company",
verbose_name="SIAE du déclarant",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="prescriber_organization",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to="prescribers.prescriberorganization",
verbose_name="organisation du prescripteur habilité",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="processed_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)s_processed",
to=settings.AUTH_USER_MODEL,
verbose_name="traité par",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="updated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_updated",
to=settings.AUTH_USER_MODEL,
verbose_name="modifié par",
),
),
migrations.AlterField(
model_name="prolongationrequest",
name="validated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="%(class)ss_validated",
to=settings.AUTH_USER_MODEL,
verbose_name="prescripteur habilité qui a autorisé cette prolongation",
),
),
migrations.AlterField(
model_name="suspension",
name="created_by",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="approvals_suspended_set",
to=settings.AUTH_USER_MODEL,
verbose_name="créé par",
),
),
migrations.AlterField(
model_name="suspension",
name="siae",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.RESTRICT,
related_name="approvals_suspended",
to="companies.company",
verbose_name="SIAE",
),
),
migrations.AlterField(
model_name="suspension",
name="updated_by",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.RESTRICT,
to=settings.AUTH_USER_MODEL,
verbose_name="mis à jour par",
),
),
]
27 changes: 14 additions & 13 deletions itou/approvals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,15 @@ class Approval(PENotificationMixin, CommonApprovalMixin):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="demandeur d'emploi",
on_delete=models.CASCADE,
on_delete=models.PROTECT, # 2-step deletion, first the Approval to create a CancelledApproval then the User
related_name="approvals",
)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="créé par",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
)
origin = models.CharField(
verbose_name="origine du pass",
Expand All @@ -536,7 +536,7 @@ class Approval(PENotificationMixin, CommonApprovalMixin):
verbose_name="diagnostic d'éligibilité",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # To not mess with the `approval_eligibility_diagnosis` constraint
)

updated_at = models.DateTimeField(verbose_name="date de modification", auto_now=True)
Expand Down Expand Up @@ -674,6 +674,7 @@ def delete(self, *args, **kwargs):
origin_sender_kind=sender_kind,
origin_prescriber_organization_kind=prescriber_organization_kind,
).save()
self.jobapplication_set.update(approval=None)
super().delete()

def clean(self):
Expand Down Expand Up @@ -1104,7 +1105,7 @@ def displayed_choices_for_siae(siae):
"companies.Company",
verbose_name="SIAE",
null=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # Prevent a soft lock, also for traceability and accountability
related_name="approvals_suspended",
)
reason = models.CharField(
Expand All @@ -1119,7 +1120,7 @@ def displayed_choices_for_siae(siae):
settings.AUTH_USER_MODEL,
verbose_name="créé par",
null=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="approvals_suspended_set",
)
updated_at = models.DateTimeField(verbose_name="date de modification", auto_now=True)
Expand All @@ -1128,7 +1129,7 @@ def displayed_choices_for_siae(siae):
verbose_name="mis à jour par",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability, the dates can be edited
)

objects = SuspensionQuerySet.as_manager()
Expand Down Expand Up @@ -1404,14 +1405,14 @@ class CommonProlongation(models.Model):
settings.AUTH_USER_MODEL,
verbose_name="déclarée par",
null=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="%(class)ss_declared",
)
declared_by_siae = models.ForeignKey(
"companies.Company",
verbose_name="SIAE du déclarant",
null=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability, people's organization can change
)

# It is assumed that an authorized prescriber has validated the prolongation beforehand.
Expand All @@ -1420,7 +1421,7 @@ class CommonProlongation(models.Model):
verbose_name="prescripteur habilité qui a autorisé cette prolongation",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="%(class)ss_validated",
)

Expand All @@ -1429,7 +1430,7 @@ class CommonProlongation(models.Model):
verbose_name="organisation du prescripteur habilité",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability, people's organization can change
)

# `created_by` can be different from `declared_by` when created in admin.
Expand All @@ -1438,7 +1439,7 @@ class CommonProlongation(models.Model):
settings.AUTH_USER_MODEL,
verbose_name="créé par",
null=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="%(class)ss_created",
)
updated_at = models.DateTimeField(verbose_name="date de modification", auto_now=True)
Expand All @@ -1447,7 +1448,7 @@ class CommonProlongation(models.Model):
verbose_name="modifié par",
null=True,
blank=True,
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="%(class)ss_updated",
)

Expand Down Expand Up @@ -1576,7 +1577,7 @@ class ProlongationRequest(CommonProlongation):
processed_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="traité par",
on_delete=models.SET_NULL,
on_delete=models.RESTRICT, # For traceability and accountability
related_name="%(class)s_processed",
null=True,
blank=True,
Expand Down
2 changes: 1 addition & 1 deletion itou/common_apps/address/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class AddressMixin(models.Model):
null=True,
)

insee_city = models.ForeignKey("cities.City", null=True, blank=True, on_delete=models.SET_NULL)
insee_city = models.ForeignKey("cities.City", null=True, blank=True, on_delete=models.RESTRICT)

class Meta:
abstract = True
Expand Down
2 changes: 1 addition & 1 deletion itou/common_apps/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class MembershipAbstract(models.Model):
settings.AUTH_USER_MODEL,
related_name="updated_membershipmodel_set",
null=True,
on_delete=models.CASCADE,
on_delete=models.RESTRICT, # For traceability and accountability
verbose_name="mis à jour par",
)
Expand Down
Loading

0 comments on commit 1156086

Please sign in to comment.