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

feat(api) Pouvoir chercher les structures par SIREN #1074

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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
60 changes: 60 additions & 0 deletions lemarche/api/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,66 @@ def test_should_return_detailed_siae_object_to_authenticated_user(self):
self.assertTrue("sectors" in response.data)


class SiaeRetrieveBySirenApiTest(TestCase):
@classmethod
def setUpTestData(cls):
SiaeFactory(name="Une structure", siret="12312312312345", department="38")
SiaeFactory(name="Une autre structure", siret="22222222222222", department="69")
Copy link
Contributor

Choose a reason for hiding this comment

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

J'aurai peut-être changé les 5 derniers chiffres pour avoir des SIRET différents sur le même SIREN. Je pense que c'est plus réaliste.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

je vais faire ca dans une petite PR séparée 👌
et améliorer un peu cet endpoint par siret

SiaeFactory(name="Une autre structure avec le meme siret", siret="22222222222222", department="69")
UserFactory(api_key="admin")

def test_should_return_400_if_siren_malformed(self):
# anonymous user
for siren in ["123", "12312312312345"]:
url = reverse("api:siae-retrieve-by-siren", args=[siren])
response = self.client.get(url)
self.assertEqual(response.status_code, 400)

def test_should_return_empty_list_if_siren_unknown(self):
# anonymous user
url = reverse("api:siae-retrieve-by-siren", args=["444444444"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
Copy link
Contributor

Choose a reason for hiding this comment

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

Tu as laissé des lignes commentées 😉

self.assertEqual(len(response.data), 0)

def test_should_return_siae_list_if_siren_known(self):
# anonymous user
url = reverse("api:siae-retrieve-by-siren", args=["123123123"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["siret"], "12312312312345")
self.assertEqual(response.data[0]["slug"], "une-structure-38")
self.assertTrue("sectors" not in response.data)
url = reverse("api:siae-retrieve-by-siren", args=["222222222"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 2)
self.assertEqual(response.data[0]["siret"], "22222222222222")
self.assertEqual(response.data[1]["siret"], "22222222222222")
self.assertTrue("sectors" not in response.data[0])
# authenticated user
url = reverse("api:siae-retrieve-by-siren", args=["123123123"]) + "?token=admin"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["siret"], "12312312312345")
self.assertEqual(response.data[0]["slug"], "une-structure-38")
self.assertTrue("sectors" in response.data[0])
url = reverse("api:siae-retrieve-by-siren", args=["222222222"]) + "?token=admin"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 2)
self.assertEqual(response.data[0]["siret"], "22222222222222")
self.assertEqual(response.data[1]["siret"], "22222222222222")
self.assertTrue("sectors" in response.data[0])


class SiaeRetrieveBySiretApiTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down
20 changes: 19 additions & 1 deletion lemarche/api/siaes/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.http import Http404
from django.http import Http404, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import mixins, viewsets
Expand Down Expand Up @@ -78,6 +78,24 @@ def retrieve_by_slug(self, request, slug=None, format=None):
queryset_or_404 = get_object_or_404(queryset, slug=slug)
return self._retrieve_return(request, queryset_or_404, format)

@extend_schema(
summary="Détail d'une structure (par son siren)",
tags=[Siae._meta.verbose_name_plural],
parameters=[
OpenApiParameter(name="token", description="Token Utilisateur", required=False, type=str),
],
responses=SiaeDetailSerializer,
)
def retrieve_by_siren(self, request, siren=None, format=None):
"""
Note : le siren n'est pas nécessairement unique, il peut y avoir plusieurs structures retournées.<br /><br />
<i>Un <strong>token</strong> est nécessaire pour l'accès complet à cette ressource.</i>
"""
if len(siren) != 9:
return HttpResponseBadRequest("siren must be 9 caracters long")
queryset = self.get_queryset().prefetch_many_to_one().filter(siret__startswith=siren)
return self._list_return(request, queryset, format)

@extend_schema(
summary="Détail d'une structure (par son siret)",
tags=[Siae._meta.verbose_name_plural],
Expand Down
1 change: 1 addition & 0 deletions lemarche/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
path("", TemplateView.as_view(template_name="api/home.html"), name="home"),
# Additional API endpoints
path("siae/slug/<str:slug>/", SiaeViewSet.as_view({"get": "retrieve_by_slug"}), name="siae-retrieve-by-slug"),
path("siae/siren/<str:siren>/", SiaeViewSet.as_view({"get": "retrieve_by_siren"}), name="siae-retrieve-by-siren"),
path("siae/siret/<str:siret>/", SiaeViewSet.as_view({"get": "retrieve_by_siret"}), name="siae-retrieve-by-siret"),
path(
"siaes/siret/<str:siret>/",
Expand Down
Loading