From fd4729aa035cf7bacfe7cd7075f29dfe219185ec Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 21 Jan 2022 17:32:31 +0100 Subject: [PATCH] #31 API basic implementation Compensation * adds compensation fetching for API v1 * refactors filter into predefined lookup dict of super class (needs to be customized on subclasses) --- api/urls/v1/urls.py | 2 ++ api/views/v1/compensation.py | 53 ++++++++++++++++++++++++++++++++++++ api/views/v1/general.py | 29 +++++++++++++++++++- api/views/v1/intervention.py | 14 ++++++---- api/views/views.py | 14 ++++++++-- 5 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 api/views/v1/compensation.py diff --git a/api/urls/v1/urls.py b/api/urls/v1/urls.py index c5c090d7..b11418fc 100644 --- a/api/urls/v1/urls.py +++ b/api/urls/v1/urls.py @@ -7,9 +7,11 @@ Created on: 21.01.22 """ from django.urls import path +from api.views.v1.compensation import APICompensationViewV1 from api.views.v1.intervention import APIInterventionViewV1 app_name = "v1" urlpatterns = [ path("intervention/", APIInterventionViewV1.as_view(), name="intervention"), + path("compensation/", APICompensationViewV1.as_view(), name="compensation"), ] diff --git a/api/views/v1/compensation.py b/api/views/v1/compensation.py new file mode 100644 index 00000000..417e299c --- /dev/null +++ b/api/views/v1/compensation.py @@ -0,0 +1,53 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 21.01.22 + +""" +import json + +from django.http import HttpRequest, JsonResponse + +from api.views.v1.general import AbstractModelAPIViewV1 +from compensation.models import Compensation + + +class APICompensationViewV1(AbstractModelAPIViewV1): + model = Compensation + + def get(self, request: HttpRequest, id): + self.lookup["id"] = id + del self.lookup["users__in"] + self.lookup["intervention__users__in"] = [self.user] + + data = self.fetch_and_serialize() + return JsonResponse(data) + + def intervention_to_json(self, entry): + return { + "id": entry.pk, + "identifier": entry.identifier, + "title": entry.title, + } + + def model_to_json(self, entry): + modified_on = entry.modified or entry.created + modified_on = modified_on.timestamp + entry_json = { + "identifier": entry.identifier, + "title": entry.title, + "is_cef": entry.is_cef, + "is_coherence_keeping": entry.is_coherence_keeping, + "intervention": self.intervention_to_json(entry.intervention), + "before_states": self.compensation_state_to_json(entry.before_states.all()), + "after_states": self.compensation_state_to_json(entry.after_states.all()), + "actions": self.compensation_actions_to_json(entry.actions.all()), + "deadlines": self.deadlines_to_json(entry.deadlines.all()), + "modified_on": modified_on, + "created_on": entry.created.timestamp, + } + geom = entry.geometry.geom.geojson + geo_json = json.loads(geom) + geo_json["properties"] = entry_json + return geo_json \ No newline at end of file diff --git a/api/views/v1/general.py b/api/views/v1/general.py index c07dc2c0..643475fb 100644 --- a/api/views/v1/general.py +++ b/api/views/v1/general.py @@ -77,4 +77,31 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): } } for entry in qs - ] \ No newline at end of file + ] + + def compensation_state_to_json(self, qs: QuerySet): + return [ + { + "biotope": self.konova_code_to_json(entry.biotope_type), + "surface": entry.surface, + } + for entry in qs + ] + + def compensation_actions_to_json(self, qs: QuerySet): + return [ + { + "action": self.konova_code_to_json(entry.action_type), + "amount": entry.amount, + "unit": entry.unit, + "comment": entry.comment, + } + for entry in qs + ] + + def deadlines_to_json(self, qs: QuerySet): + return list(qs.values( + "type", + "date", + "comment", + )) \ No newline at end of file diff --git a/api/views/v1/intervention.py b/api/views/v1/intervention.py index b823bb4b..59c06830 100644 --- a/api/views/v1/intervention.py +++ b/api/views/v1/intervention.py @@ -18,12 +18,10 @@ class APIInterventionViewV1(AbstractModelAPIViewV1): model = Intervention def get(self, request: HttpRequest, id): - _filter = { - "id": id, - "users__in": [self.user], - "deleted__isnull": True, - } - data = self.fetch_and_serialize(_filter) + self.lookup["id"] = id + self.lookup["users__in"] = [self.user] + + data = self.fetch_and_serialize() return JsonResponse(data) def compensations_to_json(self, qs: QuerySet): @@ -34,6 +32,8 @@ class APIInterventionViewV1(AbstractModelAPIViewV1): ) def model_to_json(self, entry: Intervention): + modified_on = entry.modified or entry.created + modified_on = modified_on.timestamp entry_json = { "identifier": entry.identifier, "title": entry.title, @@ -42,6 +42,8 @@ class APIInterventionViewV1(AbstractModelAPIViewV1): "compensations": self.compensations_to_json(entry.compensations.all()), "payments": self.payments_to_json(entry.payments.all()), "deductions": self.deductions_to_json(entry.deductions.all()), + "modified_on": modified_on, + "created_on": entry.created.timestamp, } geom = entry.geometry.geom.geojson geo_json = json.loads(geom) diff --git a/api/views/views.py b/api/views/views.py index 40f42210..f68a68ab 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -24,10 +24,19 @@ class AbstractModelAPIView(View): """ model = None user = None + lookup = None class Meta: abstract = True + def __init__(self, *args, **kwargs): + self.lookup = { + "id": None, # must be set in subclasses + "deleted__isnull": True, + "users__in": [], # must be set in subclasses + } + super().__init__(*args, **kwargs) + @abstractmethod def model_to_json(self, entry): """ Defines the returned json values of the model @@ -40,16 +49,15 @@ class AbstractModelAPIView(View): """ raise NotImplementedError("Must be implemented in subclasses") - def fetch_and_serialize(self, _filter): + def fetch_and_serialize(self): """ Serializes the model entry according to the given lookup data Args: - _filter (dict): Lookup declarations Returns: serialized_data (dict) """ - qs = self.model.objects.filter(**_filter) + qs = self.model.objects.filter(**self.lookup) serialized_data = {} for entry in qs: serialized_data[str(entry.pk)] = self.model_to_json(entry)