#31 API basic implementation Intervention fetch

* enhances intervention fetching and serialization
This commit is contained in:
2022-01-21 16:29:59 +01:00
parent 3938db1893
commit 0c35e79d04
3 changed files with 24 additions and 13 deletions
+11 -3
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()),
}