Skip to content

Commit

Permalink
Corrections suite review PR
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzirek committed Oct 29, 2024
1 parent 21ef917 commit 1cdd230
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 37 deletions.
28 changes: 15 additions & 13 deletions sv/templates/sv/fichezonedelimitee_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h2>Détections hors zone infestée</h2>
<ul class="fr-tags-group">
{% for fichedetection in detections_hors_zone_infestee %}
<li>
<a href="{% url 'fiche-detection-vue-detaillee' fichedetection.pk %}" class="fr-tag">{{ fichedetection.numero }}</a>
<a href="{{ fichedetection.get_absolute_url }}" class="fr-tag">{{ fichedetection.numero }}</a>
</li>
{% endfor %}
</ul>
Expand Down Expand Up @@ -158,18 +158,20 @@ <h2>Zones infestées {% if zones_infestees|length %}({{ zones_infestees|length }
{% endif %}
</div>
</div>
<span class="fichezonedelimitee__field">Détections rattachées {% if detections.count %}({{detections.count}}){% endif %}</span>
{% if detections.count %}
<ul class="fr-tags-group fr-mt-3v">
{% for detection in detections %}
<li>
<a href="{% url 'fiche-detection-vue-detaillee' detection.pk %}" class="fr-tag">{{ detection.numero }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<span class="fr-ml-22v">nc.</span>
{% endif %}
{% with detections_count=detections.count %}
<span class="fichezonedelimitee__field">Détections rattachées {% if detections_count %}({{ detections_count }}){% endif %}</span>
{% if detections_count %}
<ul class="fr-tags-group fr-mt-3v">
{% for detection in detections %}
<li>
<a href="{{ detection.get_absolute_url }}" class="fr-tag">{{ detection.numero }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<span class="fr-ml-22v">nc.</span>
{% endif %}
{% endwith %}
</div>
</div>
{% endfor %}
Expand Down
44 changes: 23 additions & 21 deletions sv/tests/test_fichezonedelimitee_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@
from sv.models import FicheZoneDelimitee, ZoneInfestee, FicheDetection


def test_fichezonedelimitee_with_zoneinfestee_detail(live_server, page: Page, mocked_authentification_user) -> None:
fichezonedelimitee = baker.make(
def test_fichezonedelimitee_with_zoneinfestee_detail(live_server, page: Page, mocked_authentification_user):
fiche_zone_delimitee = baker.make(
FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True
)
zoneinfestee1 = baker.make(ZoneInfestee, fiche_zone_delimitee=fichezonedelimitee, _fill_optional=True)
fd_fzd = baker.make(FicheDetection, hors_zone_infestee=fichezonedelimitee)
fd_zi1 = baker.make(FicheDetection, zone_infestee=zoneinfestee1)
page.goto(f"{live_server.url}{fichezonedelimitee.get_absolute_url()}")
expect(page.get_by_role("heading", name=f"Fiche zone délimitée n° {fichezonedelimitee.numero}")).to_be_visible()
expect(page.get_by_text(str(fichezonedelimitee.organisme_nuisible))).to_be_visible()
expect(page.get_by_text(str(fichezonedelimitee.statut_reglementaire))).to_be_visible()
zone_infestee_1 = baker.make(ZoneInfestee, fiche_zone_delimitee=fiche_zone_delimitee, _fill_optional=True)
fiche_detection_fiche_zone_delimitee = baker.make(FicheDetection, hors_zone_infestee=fiche_zone_delimitee)
fiche_detection_zone_infestee_1 = baker.make(FicheDetection, zone_infestee=zone_infestee_1)
page.goto(f"{live_server.url}{fiche_zone_delimitee.get_absolute_url()}")
expect(page.get_by_role("heading", name=f"Fiche zone délimitée n° {fiche_zone_delimitee.numero}")).to_be_visible()
expect(page.get_by_text(str(fiche_zone_delimitee.organisme_nuisible))).to_be_visible()
expect(page.get_by_text(str(fiche_zone_delimitee.statut_reglementaire))).to_be_visible()
expect(
page.get_by_text(fichezonedelimitee.get_caracteristiques_principales_zone_delimitee_display())
page.get_by_text(fiche_zone_delimitee.get_caracteristiques_principales_zone_delimitee_display())
).to_be_visible()
expect(page.get_by_text(fichezonedelimitee.commentaire)).to_be_visible()
expect(page.get_by_text(fiche_zone_delimitee.commentaire)).to_be_visible()
expect(
page.get_by_text(f"{fichezonedelimitee.rayon_zone_tampon} {fichezonedelimitee.unite_rayon_zone_tampon}")
page.get_by_text(f"{fiche_zone_delimitee.rayon_zone_tampon} {fiche_zone_delimitee.unite_rayon_zone_tampon}")
).to_be_visible()
expect(
page.get_by_text(f"{fichezonedelimitee.surface_tampon_totale} {fichezonedelimitee.unite_surface_tampon_totale}")
page.get_by_text(
f"{fiche_zone_delimitee.surface_tampon_totale} {fiche_zone_delimitee.unite_surface_tampon_totale}"
)
).to_be_visible()
if fichezonedelimitee.is_zone_tampon_toute_commune:
if fiche_zone_delimitee.is_zone_tampon_toute_commune:
expect(page.get_by_text("La zone tampon s'étend à toute la ou les communes")).to_be_visible()
expect(page.get_by_text(f"{str(fichezonedelimitee.createur)}")).to_be_visible()
expect(page.get_by_text(fichezonedelimitee.date_creation.strftime("%d/%m/%Y"))).to_be_visible()
expect(page.get_by_role("link", name=f"{str(fd_fzd.numero)}")).to_be_visible()
expect(page.get_by_text(f"{zoneinfestee1.nom}")).to_be_visible()
expect(page.get_by_text(f"{zoneinfestee1.rayon} {zoneinfestee1.unite_rayon}")).to_be_visible()
expect(page.get_by_text(f"{str(fiche_zone_delimitee.createur)}")).to_be_visible()
expect(page.get_by_text(fiche_zone_delimitee.date_creation.strftime("%d/%m/%Y"))).to_be_visible()
expect(page.get_by_role("link", name=f"{str(fiche_detection_fiche_zone_delimitee.numero)}")).to_be_visible()
expect(page.get_by_text(f"{zone_infestee_1.nom}")).to_be_visible()
expect(page.get_by_text(f"{zone_infestee_1.rayon} {zone_infestee_1.unite_rayon}")).to_be_visible()
expect(
page.get_by_text(f"{zoneinfestee1.surface_infestee_totale} {zoneinfestee1.unite_surface_infestee_totale}")
page.get_by_text(f"{zone_infestee_1.surface_infestee_totale} {zone_infestee_1.unite_surface_infestee_totale}")
).to_be_visible()
expect(page.get_by_role("link", name=f"{str(fd_zi1.numero)}")).to_be_visible()
expect(page.get_by_role("link", name=f"{str(fiche_detection_zone_infestee_1.numero)}")).to_be_visible()
52 changes: 52 additions & 0 deletions sv/tests/test_fichezonedelimitee_performances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from model_bakery import baker
from sv.models import FicheZoneDelimitee, FicheDetection, ZoneInfestee

BASE_NUM_QUERIES = 6


def test_empty_fiche_zone_delimitee_performances(client, django_assert_num_queries, mocked_authentification_user):
fiche = baker.make(FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True)

with django_assert_num_queries(BASE_NUM_QUERIES):
client.get(fiche.get_absolute_url())


def test_fiche_zone_delimitee_with_one_detection_in_hors_zone_infestee(
client, django_assert_num_queries, mocked_authentification_user
):
fiche = baker.make(FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True)
baker.make(FicheDetection, hors_zone_infestee=fiche)

with django_assert_num_queries(BASE_NUM_QUERIES):
client.get(fiche.get_absolute_url())


def test_fiche_zone_delimitee_with_multiple_detection_in_hors_zone_infestee(
client, django_assert_num_queries, mocked_authentification_user
):
fiche = baker.make(FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True)
baker.make(FicheDetection, hors_zone_infestee=fiche, _quantity=3)

with django_assert_num_queries(BASE_NUM_QUERIES):
client.get(fiche.get_absolute_url())


def test_fiche_zone_delimitee_with_one_zone_infestee(client, django_assert_num_queries, mocked_authentification_user):
fiche = baker.make(FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True)
zone_infestee = baker.make(ZoneInfestee, fiche_zone_delimitee=fiche)
baker.make(FicheDetection, zone_infestee=zone_infestee)

with django_assert_num_queries(BASE_NUM_QUERIES + 2):
client.get(fiche.get_absolute_url())


def test_fiche_zone_delimitee_with_multiple_zone_infestee(
client, django_assert_num_queries, mocked_authentification_user
):
fiche = baker.make(FicheZoneDelimitee, createur=mocked_authentification_user.agent.structure, _fill_optional=True)
zones_infestees = [baker.make(ZoneInfestee, fiche_zone_delimitee=fiche) for _ in range(4)]
for zone_infestee in zones_infestees:
baker.make(FicheDetection, zone_infestee=zone_infestee)

with django_assert_num_queries(BASE_NUM_QUERIES + 2):
client.get(fiche.get_absolute_url())
21 changes: 18 additions & 3 deletions sv/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FormView,
)
from django.urls import reverse
from django.db.models import F
from django.db.models import F, Prefetch
from django.db import transaction, IntegrityError
from django.http import HttpResponseBadRequest, HttpResponseRedirect, HttpResponse
from django.core.exceptions import ValidationError, PermissionDenied
Expand Down Expand Up @@ -57,6 +57,7 @@
TypeExploitant,
PositionChaineDistribution,
FicheZoneDelimitee,
ZoneInfestee,
)
from core.models import Visibilite

Expand Down Expand Up @@ -744,12 +745,26 @@ class FicheZoneDelimiteeDetailView(DetailView):
model = FicheZoneDelimitee
context_object_name = "fiche"

def get_queryset(self):
# Préchargement optimisé des relations
return FicheZoneDelimitee.objects.select_related(
"numero", "createur", "organisme_nuisible", "statut_reglementaire"
).prefetch_related(
# Préchargement des zones infestées et leurs détections
Prefetch(
"zoneinfestee_set",
queryset=ZoneInfestee.objects.prefetch_related(
Prefetch("fichedetection_set", queryset=FicheDetection.objects.select_related("numero"))
),
)
)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
fichezonedelimitee = self.get_object()
context["detections_hors_zone_infestee"] = fichezonedelimitee.fichedetection_set.all()
context["detections_hors_zone_infestee"] = fichezonedelimitee.fichedetection_set.select_related("numero").all()
context["zones_infestees"] = [
(zone_infestee, FicheDetection.objects.filter(zone_infestee=zone_infestee))
(zone_infestee, zone_infestee.fichedetection_set.all())
for zone_infestee in fichezonedelimitee.zoneinfestee_set.all()
]
return context

0 comments on commit 1cdd230

Please sign in to comment.