#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)
This commit is contained in:
parent
0c35e79d04
commit
897520f906
@ -7,9 +7,11 @@ Created on: 21.01.22
|
|||||||
"""
|
"""
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
|
from api.views.v1.compensation import APICompensationViewV1
|
||||||
from api.views.v1.intervention import APIInterventionViewV1
|
from api.views.v1.intervention import APIInterventionViewV1
|
||||||
|
|
||||||
app_name = "v1"
|
app_name = "v1"
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("intervention/<id>", APIInterventionViewV1.as_view(), name="intervention"),
|
path("intervention/<id>", APIInterventionViewV1.as_view(), name="intervention"),
|
||||||
|
path("compensation/<id>", APICompensationViewV1.as_view(), name="compensation"),
|
||||||
]
|
]
|
||||||
|
53
api/views/v1/compensation.py
Normal file
53
api/views/v1/compensation.py
Normal file
@ -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
|
@ -78,3 +78,30 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
|||||||
}
|
}
|
||||||
for entry in qs
|
for entry in qs
|
||||||
]
|
]
|
||||||
|
|
||||||
|
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",
|
||||||
|
))
|
@ -18,12 +18,10 @@ class APIInterventionViewV1(AbstractModelAPIViewV1):
|
|||||||
model = Intervention
|
model = Intervention
|
||||||
|
|
||||||
def get(self, request: HttpRequest, id):
|
def get(self, request: HttpRequest, id):
|
||||||
_filter = {
|
self.lookup["id"] = id
|
||||||
"id": id,
|
self.lookup["users__in"] = [self.user]
|
||||||
"users__in": [self.user],
|
|
||||||
"deleted__isnull": True,
|
data = self.fetch_and_serialize()
|
||||||
}
|
|
||||||
data = self.fetch_and_serialize(_filter)
|
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
def compensations_to_json(self, qs: QuerySet):
|
def compensations_to_json(self, qs: QuerySet):
|
||||||
@ -34,6 +32,8 @@ class APIInterventionViewV1(AbstractModelAPIViewV1):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def model_to_json(self, entry: Intervention):
|
def model_to_json(self, entry: Intervention):
|
||||||
|
modified_on = entry.modified or entry.created
|
||||||
|
modified_on = modified_on.timestamp
|
||||||
entry_json = {
|
entry_json = {
|
||||||
"identifier": entry.identifier,
|
"identifier": entry.identifier,
|
||||||
"title": entry.title,
|
"title": entry.title,
|
||||||
@ -42,6 +42,8 @@ class APIInterventionViewV1(AbstractModelAPIViewV1):
|
|||||||
"compensations": self.compensations_to_json(entry.compensations.all()),
|
"compensations": self.compensations_to_json(entry.compensations.all()),
|
||||||
"payments": self.payments_to_json(entry.payments.all()),
|
"payments": self.payments_to_json(entry.payments.all()),
|
||||||
"deductions": self.deductions_to_json(entry.deductions.all()),
|
"deductions": self.deductions_to_json(entry.deductions.all()),
|
||||||
|
"modified_on": modified_on,
|
||||||
|
"created_on": entry.created.timestamp,
|
||||||
}
|
}
|
||||||
geom = entry.geometry.geom.geojson
|
geom = entry.geometry.geom.geojson
|
||||||
geo_json = json.loads(geom)
|
geo_json = json.loads(geom)
|
||||||
|
@ -24,10 +24,19 @@ class AbstractModelAPIView(View):
|
|||||||
"""
|
"""
|
||||||
model = None
|
model = None
|
||||||
user = None
|
user = None
|
||||||
|
lookup = None
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
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
|
@abstractmethod
|
||||||
def model_to_json(self, entry):
|
def model_to_json(self, entry):
|
||||||
""" Defines the returned json values of the model
|
""" Defines the returned json values of the model
|
||||||
@ -40,16 +49,15 @@ class AbstractModelAPIView(View):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError("Must be implemented in subclasses")
|
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
|
""" Serializes the model entry according to the given lookup data
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
_filter (dict): Lookup declarations
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
serialized_data (dict)
|
serialized_data (dict)
|
||||||
"""
|
"""
|
||||||
qs = self.model.objects.filter(**_filter)
|
qs = self.model.objects.filter(**self.lookup)
|
||||||
serialized_data = {}
|
serialized_data = {}
|
||||||
for entry in qs:
|
for entry in qs:
|
||||||
serialized_data[str(entry.pk)] = self.model_to_json(entry)
|
serialized_data[str(entry.pk)] = self.model_to_json(entry)
|
||||||
|
Loading…
Reference in New Issue
Block a user