Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empêcher de lier une fiche Zone a elle-même #440

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion sv/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ def __init__(self, *args, **kwargs):
self.fields.pop("visibilite")

qs_detection = FicheDetection.objects.all().get_fiches_user_can_view(self.user).select_related("numero")
qs_zone = FicheZoneDelimitee.objects.all().get_fiches_user_can_view(self.user).select_related("numero")
if self.instance:
qs_zone = (
FicheZoneDelimitee.objects.exclude(id=self.instance.id)
.get_fiches_user_can_view(self.user)
.select_related("numero")
)
else:
qs_zone = FicheZoneDelimitee.objects.all().get_fiches_user_can_view(self.user).select_related("numero")
Comment on lines +100 to +107
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pour éviter la répétition de la requête, je propose :

Suggested change
if self.instance:
qs_zone = (
FicheZoneDelimitee.objects.exclude(id=self.instance.id)
.get_fiches_user_can_view(self.user)
.select_related("numero")
)
else:
qs_zone = FicheZoneDelimitee.objects.all().get_fiches_user_can_view(self.user).select_related("numero")
qs_zone = FicheZoneDelimitee.objects.all().get_fiches_user_can_view(self.user).select_related("numero")
if self.instance:
qs_zone = qs_zone.exclude(id=self.instance.id)

self.fields["free_link"] = MultiModelChoiceField(
required=False,
label="Sélectionner un objet",
Expand All @@ -122,6 +129,11 @@ def clean_organisme_nuisible(self):
def clean_statut_reglementaire(self):
return StatutReglementaire.objects.get(libelle=self.cleaned_data["statut_reglementaire"])

def clean_free_link(self):
if self.instance and self.instance in self.cleaned_data["free_link"]:
raise ValidationError("Vous ne pouvez pas lier une fiche a elle-même.")
return self.cleaned_data["free_link"]

def clean(self):
if duplicate_fiches_detection := self._get_duplicate_detections():
fiches = ", ".join(str(fiche) for fiche in duplicate_fiches_detection)
Expand Down
25 changes: 24 additions & 1 deletion sv/tests/test_fichezonedelimitee_update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from playwright.sync_api import Page, expect
from model_bakery import baker

from core.models import LienLibre
from core.models import LienLibre, Visibilite
from sv.tests.test_utils import FicheZoneDelimiteeFormPage
from sv.models import ZoneInfestee, FicheZoneDelimitee, FicheDetection, Etat

Expand Down Expand Up @@ -214,3 +214,26 @@ def test_update_fiche_can_add_and_delete_free_links(

assert lien_libre.related_object_1 == fiche_zone_delimitee
assert lien_libre.related_object_2 == other_fiche_2


def test_update_fiche_zone_cant_add_self_links(
live_server,
page: Page,
fiche_detection: FicheDetection,
choice_js_fill,
):
fiche_zone_delimitee = baker.make(
FicheZoneDelimitee,
etat=Etat.objects.get(id=Etat.get_etat_initial()),
organisme_nuisible=fiche_detection.organisme_nuisible,
statut_reglementaire=fiche_detection.statut_reglementaire,
_fill_optional=True,
visibilite=Visibilite.NATIONAL,
)
page.goto(f"{live_server.url}{fiche_zone_delimitee.get_update_url()}")

fiche_input = "Fiche zone délimitée : " + str(fiche_zone_delimitee.numero)
page.query_selector("#liens-libre .choices").click()
page.wait_for_selector("input:focus", state="visible", timeout=2_000)
page.locator("*:focus").fill(fiche_input)
expect(page.get_by_role("option", name=fiche_input, exact=True)).not_to_be_visible()