From 4f6964b04a59784ddea35c35f1d71ec9f4851df0 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 21 Jan 2022 17:44:58 +0100 Subject: [PATCH] #31 API basic implementation EcoAccount fetch * outsources json creation of modified_on an created_on to superclass * adds API support for fetching ecoaccount data --- api/urls/v1/urls.py | 2 ++ api/views/v1/compensation.py | 6 ++-- api/views/v1/ecoaccount.py | 58 ++++++++++++++++++++++++++++++++++++ api/views/v1/general.py | 10 ++++++- api/views/v1/intervention.py | 6 ++-- 5 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 api/views/v1/ecoaccount.py diff --git a/api/urls/v1/urls.py b/api/urls/v1/urls.py index b11418fc..75720e87 100644 --- a/api/urls/v1/urls.py +++ b/api/urls/v1/urls.py @@ -8,10 +8,12 @@ Created on: 21.01.22 from django.urls import path from api.views.v1.compensation import APICompensationViewV1 +from api.views.v1.ecoaccount import APIEcoAccountViewV1 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"), + path("ecoaccount/", APIEcoAccountViewV1.as_view(), name="ecoaccount"), ] diff --git a/api/views/v1/compensation.py b/api/views/v1/compensation.py index 417e299c..19b6246b 100644 --- a/api/views/v1/compensation.py +++ b/api/views/v1/compensation.py @@ -32,8 +32,6 @@ class APICompensationViewV1(AbstractModelAPIViewV1): } 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, @@ -44,8 +42,8 @@ class APICompensationViewV1(AbstractModelAPIViewV1): "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, + "modified_on": self.modified_on_to_json(entry), + "created_on": self.created_on_to_json(entry), } geom = entry.geometry.geom.geojson geo_json = json.loads(geom) diff --git a/api/views/v1/ecoaccount.py b/api/views/v1/ecoaccount.py new file mode 100644 index 00000000..caa1f245 --- /dev/null +++ b/api/views/v1/ecoaccount.py @@ -0,0 +1,58 @@ +""" +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 JsonResponse, HttpRequest + +from api.views.v1.general import AbstractModelAPIViewV1 +from compensation.models import EcoAccount +from intervention.models import Legal, Responsibility + + +class APIEcoAccountViewV1(AbstractModelAPIViewV1): + model = EcoAccount + + def get(self, request: HttpRequest, id): + self.lookup["id"] = id + self.lookup["users__in"] = [self.user] + + data = self.fetch_and_serialize() + return JsonResponse(data) + + def model_to_json(self, entry): + entry_json = { + "identifier": entry.identifier, + "title": entry.title, + "deductable_surface": entry.deductable_surface, + "deductable_surface_available": entry.deductable_surface - entry.get_deductions_surface(), + "responsible": self.responsible_to_json(entry.responsible), + "legal": self.legal_to_json(entry.legal), + "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()), + "deductions": self.deductions_to_json(entry.deductions.all()), + "modified_on": self.modified_on_to_json(entry), + "created_on": self.created_on_to_json(entry), + } + geom = entry.geometry.geom.geojson + geo_json = json.loads(geom) + geo_json["properties"] = entry_json + return geo_json + + def legal_to_json(self, legal: Legal): + return { + "agreement_date": legal.registration_date, + } + + def responsible_to_json(self, responsible: Responsibility): + return { + "conservation_office": self.konova_code_to_json(responsible.conservation_office), + "conservation_file_number": responsible.conservation_file_number, + "handler": responsible.handler, + } \ No newline at end of file diff --git a/api/views/v1/general.py b/api/views/v1/general.py index 643475fb..b1343fea 100644 --- a/api/views/v1/general.py +++ b/api/views/v1/general.py @@ -104,4 +104,12 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): "type", "date", "comment", - )) \ No newline at end of file + )) + + def created_on_to_json(self, entry): + return entry.created.timestamp + + def modified_on_to_json(self, entry): + modified_on = entry.modified or entry.created + modified_on = modified_on.timestamp + return modified_on \ No newline at end of file diff --git a/api/views/v1/intervention.py b/api/views/v1/intervention.py index 59c06830..e8c329d7 100644 --- a/api/views/v1/intervention.py +++ b/api/views/v1/intervention.py @@ -32,8 +32,6 @@ 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,8 +40,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, + "modified_on": self.modified_on_to_json(entry), + "created_on": self.created_on_to_json(entry), } geom = entry.geometry.geom.geojson geo_json = json.loads(geom)