Compare commits
5 Commits
3938db1893
...
8d400b4ffe
Author | SHA1 | Date | |
---|---|---|---|
8d400b4ffe | |||
870cc96a1a | |||
4f6964b04a | |||
897520f906 | |||
0c35e79d04 |
@ -7,9 +7,15 @@ 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.ema import APIEmaViewV1
|
||||
from api.views.v1.intervention import APIInterventionViewV1
|
||||
|
||||
app_name = "v1"
|
||||
urlpatterns = [
|
||||
path("intervention/<identifier>", APIInterventionViewV1.as_view(), name="intervention"),
|
||||
path("intervention/<id>", APIInterventionViewV1.as_view(), name="intervention"),
|
||||
path("compensation/<id>", APICompensationViewV1.as_view(), name="compensation"),
|
||||
path("ecoaccount/<id>", APIEcoAccountViewV1.as_view(), name="ecoaccount"),
|
||||
path("ema/<id>", APIEmaViewV1.as_view(), name="ema"),
|
||||
]
|
||||
|
36
api/views/v1/compensation.py
Normal file
36
api/views/v1/compensation.py
Normal file
@ -0,0 +1,36 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
|
||||
from api.views.v1.views import AbstractModelAPIViewV1
|
||||
from compensation.models import Compensation
|
||||
|
||||
|
||||
class APICompensationViewV1(AbstractModelAPIViewV1):
|
||||
model = Compensation
|
||||
|
||||
def prepare_lookup(self, id):
|
||||
self.lookup["id"] = id
|
||||
del self.lookup["users__in"]
|
||||
self.lookup["intervention__users__in"] = [self.user]
|
||||
|
||||
def intervention_to_json(self, entry):
|
||||
return {
|
||||
"id": entry.pk,
|
||||
"identifier": entry.identifier,
|
||||
"title": entry.title,
|
||||
}
|
||||
|
||||
def extend_properties_data(self, entry):
|
||||
self.properties_data["is_cef"] = entry.is_cef
|
||||
self.properties_data["is_coherence_keeping"] = entry.is_coherence_keeping
|
||||
self.properties_data["intervention"] = self.intervention_to_json(entry.intervention)
|
||||
self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all())
|
||||
self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all())
|
||||
self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all())
|
||||
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all())
|
||||
|
37
api/views/v1/ecoaccount.py
Normal file
37
api/views/v1/ecoaccount.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from api.views.v1.views import AbstractModelAPIViewV1
|
||||
from compensation.models import EcoAccount
|
||||
from intervention.models import Legal, Responsibility
|
||||
|
||||
|
||||
class APIEcoAccountViewV1(AbstractModelAPIViewV1):
|
||||
model = EcoAccount
|
||||
|
||||
def extend_properties_data(self, entry):
|
||||
self.properties_data["deductable_surface"] = entry.deductable_surface
|
||||
self.properties_data["deductable_surface_available"] = entry.deductable_surface - entry.get_deductions_surface()
|
||||
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible)
|
||||
self.properties_data["legal"] = self.legal_to_json(entry.legal)
|
||||
self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all())
|
||||
self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all())
|
||||
self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all())
|
||||
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all())
|
||||
self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.all())
|
||||
|
||||
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,
|
||||
}
|
28
api/views/v1/ema.py
Normal file
28
api/views/v1/ema.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from api.views.v1.views import AbstractModelAPIViewV1
|
||||
from ema.models import Ema
|
||||
from intervention.models import Responsibility
|
||||
|
||||
|
||||
class APIEmaViewV1(AbstractModelAPIViewV1):
|
||||
model = Ema
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
def extend_properties_data(self, entry):
|
||||
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible)
|
||||
self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all())
|
||||
self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all())
|
||||
self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all())
|
||||
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all())
|
@ -1,77 +0,0 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from api.views.views import AbstractModelAPIView
|
||||
from codelist.models import KonovaCode
|
||||
from intervention.models import Responsibility, Legal
|
||||
|
||||
|
||||
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
""" Holds general serialization functions for API v1
|
||||
|
||||
"""
|
||||
|
||||
def konova_code_to_json(self, konova_code: KonovaCode):
|
||||
return {
|
||||
"atom_id": konova_code.atom_id,
|
||||
"long_name": konova_code.long_name,
|
||||
"short_name": konova_code.short_name,
|
||||
}
|
||||
|
||||
def responsible_to_json(self, responsible: Responsibility):
|
||||
return {
|
||||
"registration_office": self.konova_code_to_json(responsible.registration_office),
|
||||
"registration_file_number": responsible.registration_file_number,
|
||||
"conservation_office": self.konova_code_to_json(responsible.conservation_office),
|
||||
"conservation_file_number": responsible.conservation_file_number,
|
||||
"handler": responsible.handler,
|
||||
}
|
||||
|
||||
def legal_to_json(self, legal: Legal):
|
||||
return {
|
||||
"registration_date": legal.registration_date,
|
||||
"binding_date": legal.binding_date,
|
||||
"process_type": self.konova_code_to_json(legal.process_type),
|
||||
"laws": [self.konova_code_to_json(law) for law in legal.laws.all()],
|
||||
}
|
||||
|
||||
def payments_to_json(self, qs: QuerySet):
|
||||
""" Serializes payments into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of Payment entries
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"amount": entry.amount,
|
||||
"due_on": entry.due_on,
|
||||
"comment": entry.comment,
|
||||
}
|
||||
for entry in qs
|
||||
]
|
||||
|
||||
def deductions_to_json(self, qs: QuerySet):
|
||||
""" Serializes eco account deductions into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of EcoAccountDeduction entries
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"eco_account": entry.account.pk,
|
||||
"surface": entry.surface,
|
||||
}
|
||||
for entry in qs
|
||||
]
|
@ -5,37 +5,25 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
import json
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
|
||||
from api.views.v1.general import AbstractModelAPIViewV1
|
||||
from api.views.v1.views import AbstractModelAPIViewV1
|
||||
from intervention.models import Intervention
|
||||
|
||||
|
||||
class APIInterventionViewV1(AbstractModelAPIViewV1):
|
||||
model = Intervention
|
||||
|
||||
def get(self, request: HttpRequest, identifier):
|
||||
_filter = {
|
||||
"identifier": identifier,
|
||||
"users__in": [self.user],
|
||||
"deleted__isnull": True,
|
||||
}
|
||||
data = self.fetch_and_serialize(_filter)
|
||||
return JsonResponse(data)
|
||||
def compensations_to_json(self, qs: QuerySet):
|
||||
return list(
|
||||
qs.values(
|
||||
"id", "identifier", "title"
|
||||
)
|
||||
)
|
||||
|
||||
def model_to_json(self, entry: Intervention):
|
||||
entry_json = {
|
||||
"identifier": entry.identifier,
|
||||
"title": entry.title,
|
||||
"responsible": self.responsible_to_json(entry.responsible),
|
||||
"legal": self.legal_to_json(entry.legal),
|
||||
"compensations": list(entry.compensations.all().values_list("pk", flat=True)),
|
||||
"payments": self.payments_to_json(entry.payments.all()),
|
||||
"deductions": self.deductions_to_json(entry.deductions.all()),
|
||||
}
|
||||
geom = entry.geometry.geom.geojson
|
||||
geo_json = json.loads(geom)
|
||||
geo_json["properties"] = entry_json
|
||||
return geo_json
|
||||
def extend_properties_data(self, entry):
|
||||
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible)
|
||||
self.properties_data["legal"] = self.legal_to_json(entry.legal)
|
||||
self.properties_data["compensations"] = self.compensations_to_json(entry.compensations.all())
|
||||
self.properties_data["payments"] = self.payments_to_json(entry.payments.all())
|
||||
self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.all())
|
||||
|
236
api/views/v1/views.py
Normal file
236
api/views/v1/views.py
Normal file
@ -0,0 +1,236 @@
|
||||
"""
|
||||
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.db.models import QuerySet
|
||||
from django.http import JsonResponse, HttpRequest
|
||||
|
||||
from api.views.views import AbstractModelAPIView
|
||||
from codelist.models import KonovaCode
|
||||
from intervention.models import Responsibility, Legal
|
||||
|
||||
|
||||
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
""" Holds general serialization functions for API v1
|
||||
|
||||
"""
|
||||
|
||||
def get(self, request: HttpRequest, id):
|
||||
""" Handles the GET request
|
||||
|
||||
Performs the fetching and serialization of the data
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The entries id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.prepare_lookup(id)
|
||||
|
||||
try:
|
||||
data = self.fetch_and_serialize()
|
||||
except Exception as e:
|
||||
return self.return_error_response(e, 500)
|
||||
return JsonResponse(data)
|
||||
|
||||
def model_to_geo_json(self, entry):
|
||||
""" Adds the basic data, which all elements hold
|
||||
|
||||
Args:
|
||||
entry (): The data entry
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
geom = entry.geometry.geom.geojson
|
||||
geo_json = json.loads(geom)
|
||||
self.properties_data = {
|
||||
"id": entry.id,
|
||||
"identifier": entry.identifier,
|
||||
"title": entry.title,
|
||||
"created_on": self.created_on_to_json(entry),
|
||||
"modified_on": self.modified_on_to_json(entry),
|
||||
}
|
||||
self.extend_properties_data(entry)
|
||||
geo_json["properties"] = self.properties_data
|
||||
return geo_json
|
||||
|
||||
def prepare_lookup(self, id):
|
||||
""" Customizes lookup values for db filtering
|
||||
|
||||
Args:
|
||||
id (str): The entries id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.lookup["id"] = id
|
||||
self.lookup["users__in"] = [self.user]
|
||||
|
||||
def konova_code_to_json(self, konova_code: KonovaCode):
|
||||
""" Serializes KonovaCode model into json
|
||||
|
||||
Args:
|
||||
konova_code (KonovaCode): The KonovaCode entry
|
||||
|
||||
Returns:
|
||||
serialized_json (dict)
|
||||
"""
|
||||
return {
|
||||
"atom_id": konova_code.atom_id,
|
||||
"long_name": konova_code.long_name,
|
||||
"short_name": konova_code.short_name,
|
||||
}
|
||||
|
||||
def responsible_to_json(self, responsible: Responsibility):
|
||||
""" Serializes Responsibility model into json
|
||||
|
||||
Args:
|
||||
responsible (Responsibility): The Responsibility entry
|
||||
|
||||
Returns:
|
||||
serialized_json (dict)
|
||||
"""
|
||||
return {
|
||||
"registration_office": self.konova_code_to_json(responsible.registration_office),
|
||||
"registration_file_number": responsible.registration_file_number,
|
||||
"conservation_office": self.konova_code_to_json(responsible.conservation_office),
|
||||
"conservation_file_number": responsible.conservation_file_number,
|
||||
"handler": responsible.handler,
|
||||
}
|
||||
|
||||
def legal_to_json(self, legal: Legal):
|
||||
""" Serializes Legal model into json
|
||||
|
||||
Args:
|
||||
legal (Legal): The Legal entry
|
||||
|
||||
Returns:
|
||||
serialized_json (dict)
|
||||
"""
|
||||
return {
|
||||
"registration_date": legal.registration_date,
|
||||
"binding_date": legal.binding_date,
|
||||
"process_type": self.konova_code_to_json(legal.process_type),
|
||||
"laws": [self.konova_code_to_json(law) for law in legal.laws.all()],
|
||||
}
|
||||
|
||||
def payments_to_json(self, qs: QuerySet):
|
||||
""" Serializes payments into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of Payment entries
|
||||
|
||||
Returns:
|
||||
serialized_json (list)
|
||||
"""
|
||||
return list(qs.values("amount", "due_on", "comment"))
|
||||
|
||||
def deductions_to_json(self, qs: QuerySet):
|
||||
""" Serializes eco account deductions into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of EcoAccountDeduction entries
|
||||
|
||||
Returns:
|
||||
serialized_json (list)
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"id": entry.pk,
|
||||
"eco_account": {
|
||||
"id": entry.account.pk,
|
||||
"identifier": entry.account.identifier,
|
||||
"title": entry.account.title,
|
||||
},
|
||||
"surface": entry.surface,
|
||||
"intervention": {
|
||||
"id": entry.intervention.pk,
|
||||
"identifier": entry.intervention.identifier,
|
||||
"title": entry.intervention.title,
|
||||
}
|
||||
}
|
||||
for entry in qs
|
||||
]
|
||||
|
||||
def compensation_state_to_json(self, qs: QuerySet):
|
||||
""" Serializes compensation states into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of CompensationState entries
|
||||
|
||||
Returns:
|
||||
serialized_json (list)
|
||||
"""
|
||||
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):
|
||||
""" Serializes CompensationActions into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of CompensationAction entries
|
||||
|
||||
Returns:
|
||||
serialized_json (list)
|
||||
"""
|
||||
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):
|
||||
""" Serializes deadlines into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of Deadline entries
|
||||
|
||||
Returns:
|
||||
serialized_json (list)
|
||||
"""
|
||||
return list(qs.values(
|
||||
"type",
|
||||
"date",
|
||||
"comment",
|
||||
))
|
||||
|
||||
def created_on_to_json(self, entry):
|
||||
""" Serializes the created_on into json
|
||||
|
||||
Args:
|
||||
entry (BaseObject): The entry
|
||||
|
||||
Returns:
|
||||
created_on (timestamp)
|
||||
"""
|
||||
return entry.created.timestamp
|
||||
|
||||
def modified_on_to_json(self, entry):
|
||||
""" Serializes the modified_on into json
|
||||
|
||||
Args:
|
||||
entry (BaseObject): The entry
|
||||
|
||||
Returns:
|
||||
modified_on (timestamp)
|
||||
"""
|
||||
modified_on = entry.modified or entry.created
|
||||
modified_on = modified_on.timestamp
|
||||
return modified_on
|
@ -24,13 +24,23 @@ class AbstractModelAPIView(View):
|
||||
"""
|
||||
model = None
|
||||
user = None
|
||||
lookup = None
|
||||
properties_data = 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
|
||||
def model_to_geo_json(self, entry):
|
||||
""" Defines the model as geo json
|
||||
|
||||
Args:
|
||||
entry (): The found entry from the database
|
||||
@ -40,29 +50,53 @@ class AbstractModelAPIView(View):
|
||||
"""
|
||||
raise NotImplementedError("Must be implemented in subclasses")
|
||||
|
||||
def fetch_and_serialize(self, _filter):
|
||||
@abstractmethod
|
||||
def extend_properties_data(self, entry):
|
||||
""" Defines the 'properties' part of geo json
|
||||
|
||||
Args:
|
||||
entry (): The found entry from the database
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
raise NotImplementedError("Must be implemented in subclasses")
|
||||
|
||||
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)
|
||||
serialized_data = {}
|
||||
for entry in qs:
|
||||
serialized_data[str(entry.pk)] = self.model_to_json(entry)
|
||||
entry = self.model.objects.get(**self.lookup)
|
||||
serialized_data = self.model_to_geo_json(entry)
|
||||
return serialized_data
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
try:
|
||||
self.user = APIUserToken.get_user_from_token(request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None))
|
||||
except PermissionError as e:
|
||||
return JsonResponse(
|
||||
{
|
||||
"error": e.__str__()
|
||||
},
|
||||
status=403
|
||||
)
|
||||
return self.return_error_response(e, 403)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def return_error_response(self, error, status_code=500):
|
||||
""" Returns an error as JsonReponse
|
||||
|
||||
Args:
|
||||
error (): The error/exception
|
||||
status_code (): The desired status code
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
content = [error.__str__()]
|
||||
if hasattr(error, "messages"):
|
||||
content = error.messages
|
||||
return JsonResponse(
|
||||
{
|
||||
"errors": content
|
||||
},
|
||||
status=status_code
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user