#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:
2022-01-21 17:32:31 +01:00
parent 11de423d05
commit fd4729aa03
5 changed files with 102 additions and 10 deletions

View 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

View File

@@ -77,4 +77,31 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
}
}
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",
))

View File

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