#31 API basic implementation EcoAccount fetch

* outsources json creation of modified_on an created_on to superclass
* adds API support for fetching ecoaccount data
This commit is contained in:
mpeltriaux 2022-01-21 17:44:58 +01:00
parent 897520f906
commit 4f6964b04a
5 changed files with 73 additions and 9 deletions

View File

@ -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/<id>", APIInterventionViewV1.as_view(), name="intervention"),
path("compensation/<id>", APICompensationViewV1.as_view(), name="compensation"),
path("ecoaccount/<id>", APIEcoAccountViewV1.as_view(), name="ecoaccount"),
]

View File

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

View File

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

View File

@ -105,3 +105,11 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
"date",
"comment",
))
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

View File

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