Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Les services portés par une structure orpheline sont exclus des résul…
Browse files Browse the repository at this point in the history
…tats de recherche (#322)
  • Loading branch information
ikarius authored Jul 15, 2024
1 parent ba89671 commit e0a89e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dora/services/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from dora import data_inclusion
from dora.admin_express.models import City
from dora.core.constants import WGS84
from dora.structures.models import Structure

from .serializers import SearchResultSerializer
from .utils import filter_services_by_city_code
Expand Down Expand Up @@ -234,6 +235,11 @@ def _get_dora_results(
)
)

# Par souci de qualité des données,
# les services DORA rattachés à une structure orpheline
# sont filtrés lors de la recherche.
services = services.exclude(structure__in=Structure.objects.orphans())

if kinds:
services = services.filter(kinds__value__in=kinds)

Expand Down
51 changes: 51 additions & 0 deletions dora/services/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from model_bakery import baker

from dora.admin_express.models import AdminDivisionType
from dora.core.test_utils import make_service, make_structure, make_user
from dora.services.enums import ServiceStatus


@pytest.fixture
def service():
# service devant sortir lors d'une recherche globale
service = make_service(
status=ServiceStatus.PUBLISHED,
diffusion_zone_type=AdminDivisionType.COUNTRY,
)
return service


@pytest.fixture
def structure_with_user():
return make_structure(user=make_user())


def test_search_services_with_orphan_structure(
api_client, service, structure_with_user
):
# les services rattachés à une structure orpheline
# doivent être filtrés lors de la recherche

# le paramètre `city` est nécessaire a minima
city = baker.make("City")
response = api_client.get(f"/search/?city={city.code}")

assert response.status_code == 200
assert not response.data[
"services"
], "aucun service ne devrait être trouvé (structure orpheline)"

# on ajoute une structure au service
service.structure = structure_with_user
service.save()
response = api_client.get(f"/search/?city={city.code}")

assert response.status_code == 200
assert response.data[
"services"
], "un service ne devrait être trouvé (structure avec utilisateur)"

[found] = response.data["services"]

assert found["slug"] == service.slug
2 changes: 2 additions & 0 deletions dora/structures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def create_from_establishment(
return structure

def orphans(self):
# structures "orphelines" :
# pas de membres enregistrés, ni en attente d'enregistrement
return self.filter(membership=None, putative_membership=None)


Expand Down

0 comments on commit e0a89e2

Please sign in to comment.