#31 API basic implementation Intervention fetch

* enhances intervention fetching and serialization
This commit is contained in:
mpeltriaux 2022-01-21 16:29:59 +01:00
parent cf82f4b223
commit 11de423d05
3 changed files with 24 additions and 13 deletions

View File

@ -11,5 +11,5 @@ from api.views.v1.intervention import APIInterventionViewV1
app_name = "v1"
urlpatterns = [
path("intervention/<identifier>", APIInterventionViewV1.as_view(), name="intervention"),
path("intervention/<id>", APIInterventionViewV1.as_view(), name="intervention"),
]

View File

@ -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
]

View File

@ -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()),
}