From 11de423d05167c016772aec5ca307b652a0f54d8 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 21 Jan 2022 16:29:59 +0100 Subject: [PATCH] #31 API basic implementation Intervention fetch * enhances intervention fetching and serialization --- api/urls/v1/urls.py | 2 +- api/views/v1/general.py | 21 ++++++++++++--------- api/views/v1/intervention.py | 14 +++++++++++--- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/api/urls/v1/urls.py b/api/urls/v1/urls.py index 0d2cd6d7..c5c090d7 100644 --- a/api/urls/v1/urls.py +++ b/api/urls/v1/urls.py @@ -11,5 +11,5 @@ from api.views.v1.intervention import APIInterventionViewV1 app_name = "v1" urlpatterns = [ - path("intervention/", APIInterventionViewV1.as_view(), name="intervention"), + path("intervention/", APIInterventionViewV1.as_view(), name="intervention"), ] diff --git a/api/views/v1/general.py b/api/views/v1/general.py index 10f801a3..c07dc2c0 100644 --- a/api/views/v1/general.py +++ b/api/views/v1/general.py @@ -50,14 +50,7 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): Returns: """ - return [ - { - "amount": entry.amount, - "due_on": entry.due_on, - "comment": entry.comment, - } - for entry in qs - ] + return list(qs.values("amount", "due_on", "comment")) def deductions_to_json(self, qs: QuerySet): """ Serializes eco account deductions into json @@ -70,8 +63,18 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): """ return [ { - "eco_account": entry.account.pk, + "id": entry.pk, + "eco_account": { + "id": entry.account.pk, + "identifier": entry.account.identifier, + "title": entry.account.title, + }, "surface": entry.surface, + "intervention": { + "id": entry.intervention.pk, + "identifier": entry.intervention.identifier, + "title": entry.intervention.title, + } } for entry in qs ] \ No newline at end of file diff --git a/api/views/v1/intervention.py b/api/views/v1/intervention.py index 613f608f..b823bb4b 100644 --- a/api/views/v1/intervention.py +++ b/api/views/v1/intervention.py @@ -7,6 +7,7 @@ Created on: 21.01.22 """ import json +from django.db.models import QuerySet from django.http import HttpRequest, JsonResponse from api.views.v1.general import AbstractModelAPIViewV1 @@ -16,22 +17,29 @@ from intervention.models import Intervention class APIInterventionViewV1(AbstractModelAPIViewV1): model = Intervention - def get(self, request: HttpRequest, identifier): + def get(self, request: HttpRequest, id): _filter = { - "identifier": identifier, + "id": id, "users__in": [self.user], "deleted__isnull": True, } data = self.fetch_and_serialize(_filter) return JsonResponse(data) + def compensations_to_json(self, qs: QuerySet): + return list( + qs.values( + "id", "identifier", "title" + ) + ) + def model_to_json(self, entry: Intervention): entry_json = { "identifier": entry.identifier, "title": entry.title, "responsible": self.responsible_to_json(entry.responsible), "legal": self.legal_to_json(entry.legal), - "compensations": list(entry.compensations.all().values_list("pk", flat=True)), + "compensations": self.compensations_to_json(entry.compensations.all()), "payments": self.payments_to_json(entry.payments.all()), "deductions": self.deductions_to_json(entry.deductions.all()), }