-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pas de numéro de fiche zone délimitée si visibilité brouillon
- model FicheZoneDelimitee: le champ numero devient nullable - model FicheZoneDelimitee: ajout d'une contrainte pour s'assurer que numero est null quand la visibilite est brouillon - model FicheZoneDelimitee: ajout d'une méthode save pour attribuer automatiquement un numéro quand la visibilité est local - modification du template de détail pour affichage conditionnel du champ numero - ajout de tests du modèle pour valider la contrainte de base ajoutée (check_fiche_zone_delimitee_numero_fiche_is_null_when_visibilite_is_brouillon) - ajout d'un test e2e - refactoring de certains tests pour respecter la contrainte
- Loading branch information
Showing
12 changed files
with
213 additions
and
51 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
sv/migrations/0041_alter_fichezonedelimitee_numero_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Generated by Django 5.0.8 on 2024-11-22 20:45 | ||
import datetime | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models, transaction | ||
|
||
|
||
def set_numero_to_none_for_existing_fiche_zone_delimitee_in_brouillon(apps, schema_editor): | ||
FicheZoneDelimitee = apps.get_model("sv", "FicheZoneDelimitee") | ||
fiches_brouillon = FicheZoneDelimitee.objects.filter(visibilite="brouillon") | ||
for fiche in fiches_brouillon: | ||
if fiche.numero: | ||
numero = fiche.numero | ||
fiche.numero = None | ||
fiche.save() | ||
numero.delete() | ||
|
||
|
||
def reverse_set_numero_to_none_for_existing_fiche_zone_delimitee_in_brouillon(apps, schema_editor): | ||
FicheZoneDelimitee = apps.get_model("sv", "FicheZoneDelimitee") | ||
NumeroFiche = apps.get_model("sv", "NumeroFiche") | ||
|
||
with transaction.atomic(): | ||
annee_courante = datetime.datetime.now().year | ||
dernier_numero = NumeroFiche.objects.filter(annee=annee_courante).order_by("-numero").first() | ||
prochain_numero = 1 if not dernier_numero else dernier_numero.numero + 1 | ||
|
||
for fiche in FicheZoneDelimitee.objects.filter(visibilite="brouillon"): | ||
nouveau_numero = NumeroFiche.objects.create(annee=annee_courante, numero=prochain_numero) | ||
FicheZoneDelimitee.objects.filter(id=fiche.id).update(numero=nouveau_numero) | ||
prochain_numero += 1 | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0020_create_access_admin_group"), | ||
("sv", "0040_rename_OE_prefix_20241119_1541"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="fichezonedelimitee", | ||
name="numero", | ||
field=models.OneToOneField( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="sv.numerofiche", | ||
verbose_name="Numéro de fiche", | ||
), | ||
), | ||
migrations.RunPython( | ||
set_numero_to_none_for_existing_fiche_zone_delimitee_in_brouillon, | ||
reverse_set_numero_to_none_for_existing_fiche_zone_delimitee_in_brouillon, | ||
), | ||
migrations.AddConstraint( | ||
model_name="fichezonedelimitee", | ||
constraint=models.CheckConstraint( | ||
check=models.Q(("visibilite", "brouillon"), ("numero__isnull", False), _negated=True), | ||
name="check_fiche_zone_delimitee_numero_fiche_is_null_when_visibilite_is_brouillon", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.