Compare commits
5 Commits
8d400b4ffe
...
314879a1fe
Author | SHA1 | Date | |
---|---|---|---|
314879a1fe | |||
b87389e07b | |||
c8dfa7e21f | |||
d0f3fb9f61 | |||
45ac5b68b9 |
@ -7,15 +7,16 @@ 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.views import EmaAPIViewV1, EcoAccountAPIViewV1, CompensationAPIViewV1, InterventionAPIViewV1
|
||||||
from api.views.v1.ecoaccount import APIEcoAccountViewV1
|
|
||||||
from api.views.v1.ema import APIEmaViewV1
|
|
||||||
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>", InterventionAPIViewV1.as_view(), name="intervention"),
|
||||||
path("compensation/<id>", APICompensationViewV1.as_view(), name="compensation"),
|
path("intervention/", InterventionAPIViewV1.as_view(), name="intervention"),
|
||||||
path("ecoaccount/<id>", APIEcoAccountViewV1.as_view(), name="ecoaccount"),
|
path("compensation/<id>", CompensationAPIViewV1.as_view(), name="compensation"),
|
||||||
path("ema/<id>", APIEmaViewV1.as_view(), name="ema"),
|
path("compensation/", CompensationAPIViewV1.as_view(), name="compensation"),
|
||||||
|
path("ecoaccount/<id>", EcoAccountAPIViewV1.as_view(), name="ecoaccount"),
|
||||||
|
path("ecoaccount/", EcoAccountAPIViewV1.as_view(), name="ecoaccount"),
|
||||||
|
path("ema/<id>", EmaAPIViewV1.as_view(), name="ema"),
|
||||||
|
path("ema/", EmaAPIViewV1.as_view(), name="ema"),
|
||||||
]
|
]
|
||||||
|
7
api/utils/__init__.py
Normal file
7
api/utils/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
7
api/utils/serializer/__init__.py
Normal file
7
api/utils/serializer/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
137
api/utils/serializer/serializer.py
Normal file
137
api/utils/serializer/serializer.py
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
from abc import abstractmethod
|
||||||
|
|
||||||
|
from django.contrib.gis import geos
|
||||||
|
from django.contrib.gis.geos import GEOSGeometry
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractModelAPISerializer:
|
||||||
|
model = None
|
||||||
|
lookup = None
|
||||||
|
properties_data = None
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.lookup = {
|
||||||
|
"id": None, # must be set
|
||||||
|
"deleted__isnull": True,
|
||||||
|
"users__in": [], # must be set
|
||||||
|
}
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def model_to_geo_json(self, entry):
|
||||||
|
""" Defines the model as geo json
|
||||||
|
|
||||||
|
Args:
|
||||||
|
entry (): The found entry from the database
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError("Must be implemented in subclasses")
|
||||||
|
|
||||||
|
@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")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def prepare_lookup(self, _id, user):
|
||||||
|
""" Updates lookup dict for db fetching
|
||||||
|
|
||||||
|
Args:
|
||||||
|
_id (str): The object's id
|
||||||
|
user (User): The user requesting for
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.lookup["id"] = _id
|
||||||
|
self.lookup["users__in"] = [user]
|
||||||
|
|
||||||
|
def fetch_and_serialize(self):
|
||||||
|
""" Serializes the model entry according to the given lookup data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
serialized_data (dict)
|
||||||
|
"""
|
||||||
|
entry = self.model.objects.get(**self.lookup)
|
||||||
|
serialized_data = self.model_to_geo_json(entry)
|
||||||
|
return serialized_data
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_model_from_json(self, id, json_model, user):
|
||||||
|
""" Updates an instance from given json data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
id (str): The instance's to be updated
|
||||||
|
json_model (dict): JSON data
|
||||||
|
user (User): The performing user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError("Must be implemented in subclasses")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def create_model_from_json(self, json_model, user):
|
||||||
|
""" Creates a new instance from given json data
|
||||||
|
|
||||||
|
Args:
|
||||||
|
json_model (dict): JSON data
|
||||||
|
user (User): The performing user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError("Must be implemented in subclasses")
|
||||||
|
|
||||||
|
def create_geometry_from_json(self, geojson) -> GEOSGeometry:
|
||||||
|
""" Creates a GEOSGeometry object based on the given geojson
|
||||||
|
|
||||||
|
Args:
|
||||||
|
geojson (str|dict): The geojson as str or dict
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
geometry (GEOSGeometry)
|
||||||
|
"""
|
||||||
|
if isinstance(geojson, dict):
|
||||||
|
geojson = json.dumps(geojson)
|
||||||
|
geometry = geos.fromstr(geojson)
|
||||||
|
return geometry
|
||||||
|
|
||||||
|
def get_obj_from_db(self, id, user):
|
||||||
|
""" Returns the object from database
|
||||||
|
|
||||||
|
Fails if id not found or user does not have shared access
|
||||||
|
|
||||||
|
Args:
|
||||||
|
id (str): The object's id
|
||||||
|
user (User): The API user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.model.objects.get(
|
||||||
|
id=id,
|
||||||
|
users__in=[user]
|
||||||
|
)
|
7
api/utils/serializer/v1/__init__.py
Normal file
7
api/utils/serializer/v1/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
@ -2,21 +2,20 @@
|
|||||||
Author: Michel Peltriaux
|
Author: Michel Peltriaux
|
||||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
Created on: 21.01.22
|
Created on: 24.01.22
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
|
||||||
from api.views.v1.views import AbstractModelAPIViewV1
|
|
||||||
from compensation.models import Compensation
|
from compensation.models import Compensation
|
||||||
|
|
||||||
|
|
||||||
class APICompensationViewV1(AbstractModelAPIViewV1):
|
class CompensationAPISerializerV1(AbstractModelAPISerializerV1):
|
||||||
model = Compensation
|
model = Compensation
|
||||||
|
|
||||||
def prepare_lookup(self, id):
|
def prepare_lookup(self, id, user):
|
||||||
self.lookup["id"] = id
|
self.lookup["id"] = id
|
||||||
del self.lookup["users__in"]
|
del self.lookup["users__in"]
|
||||||
self.lookup["intervention__users__in"] = [self.user]
|
self.lookup["intervention__users__in"] = [user]
|
||||||
|
|
||||||
def intervention_to_json(self, entry):
|
def intervention_to_json(self, entry):
|
||||||
return {
|
return {
|
||||||
@ -32,5 +31,4 @@ class APICompensationViewV1(AbstractModelAPIViewV1):
|
|||||||
self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all())
|
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["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["actions"] = self.compensation_actions_to_json(entry.actions.all())
|
||||||
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all())
|
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all())
|
||||||
|
|
@ -2,15 +2,15 @@
|
|||||||
Author: Michel Peltriaux
|
Author: Michel Peltriaux
|
||||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
Created on: 21.01.22
|
Created on: 24.01.22
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from api.views.v1.views import AbstractModelAPIViewV1
|
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
|
||||||
from compensation.models import EcoAccount
|
from compensation.models import EcoAccount
|
||||||
from intervention.models import Legal, Responsibility
|
from intervention.models import Legal, Responsibility
|
||||||
|
|
||||||
|
|
||||||
class APIEcoAccountViewV1(AbstractModelAPIViewV1):
|
class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1):
|
||||||
model = EcoAccount
|
model = EcoAccount
|
||||||
|
|
||||||
def extend_properties_data(self, entry):
|
def extend_properties_data(self, entry):
|
@ -2,15 +2,15 @@
|
|||||||
Author: Michel Peltriaux
|
Author: Michel Peltriaux
|
||||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
Created on: 21.01.22
|
Created on: 24.01.22
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from api.views.v1.views import AbstractModelAPIViewV1
|
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
|
||||||
from ema.models import Ema
|
from ema.models import Ema
|
||||||
from intervention.models import Responsibility
|
from intervention.models import Responsibility
|
||||||
|
|
||||||
|
|
||||||
class APIEmaViewV1(AbstractModelAPIViewV1):
|
class EmaAPISerializerV1(AbstractModelAPISerializerV1):
|
||||||
model = Ema
|
model = Ema
|
||||||
|
|
||||||
def responsible_to_json(self, responsible: Responsibility):
|
def responsible_to_json(self, responsible: Responsibility):
|
169
api/utils/serializer/v1/intervention.py
Normal file
169
api/utils/serializer/v1/intervention.py
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
||||||
|
from django.db import transaction
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
|
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
|
||||||
|
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID, \
|
||||||
|
CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID
|
||||||
|
from intervention.models import Intervention, Responsibility, Legal
|
||||||
|
from konova.models import Geometry
|
||||||
|
from konova.tasks import celery_update_parcels
|
||||||
|
from user.models import UserActionLogEntry
|
||||||
|
|
||||||
|
|
||||||
|
class InterventionAPISerializerV1(AbstractModelAPISerializerV1):
|
||||||
|
model = Intervention
|
||||||
|
|
||||||
|
def compensations_to_json(self, qs: QuerySet):
|
||||||
|
return list(
|
||||||
|
qs.values(
|
||||||
|
"id", "identifier", "title"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
|
def initialize_objects(self, json_model, user):
|
||||||
|
""" Initializes all needed objects from the json_model data
|
||||||
|
|
||||||
|
Does not persist data to the DB!
|
||||||
|
|
||||||
|
Args:
|
||||||
|
json_model (dict): The json data
|
||||||
|
user (User): The API user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
obj (Intervention)
|
||||||
|
"""
|
||||||
|
# Create geometry
|
||||||
|
json_geom = self.create_geometry_from_json(json_model)
|
||||||
|
geometry = Geometry()
|
||||||
|
geometry.geom = json_geom
|
||||||
|
|
||||||
|
# Create linked objects
|
||||||
|
obj = Intervention()
|
||||||
|
resp = Responsibility()
|
||||||
|
legal = Legal()
|
||||||
|
created = UserActionLogEntry.get_created_action(user, comment="API Import")
|
||||||
|
obj.legal = legal
|
||||||
|
obj.created = created
|
||||||
|
obj.geometry = geometry
|
||||||
|
obj.responsible = resp
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def set_legal(self, obj, legal_data):
|
||||||
|
""" Sets the legal data contents to the provided legal_data dict
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Intervention): The intervention object
|
||||||
|
legal_data (dict): The new data
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
obj
|
||||||
|
"""
|
||||||
|
obj.legal.registration_date = legal_data["registration_date"]
|
||||||
|
obj.legal.binding_date = legal_data["binding_date"]
|
||||||
|
obj.legal.process_type = self.konova_code_from_json(
|
||||||
|
legal_data["process_type"],
|
||||||
|
CODELIST_PROCESS_TYPE_ID,
|
||||||
|
)
|
||||||
|
laws = [self.konova_code_from_json(law, CODELIST_LAW_ID) for law in legal_data["laws"]]
|
||||||
|
obj.legal.laws.set(laws)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def set_responsibility(self, obj, responsibility_data: dict):
|
||||||
|
""" Sets the responsible data contents to the provided responsibility_data dict
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Intervention): The intervention object
|
||||||
|
responsibility_data (dict): The new data
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
obj
|
||||||
|
"""
|
||||||
|
obj.responsible.registration_office = self.konova_code_from_json(
|
||||||
|
responsibility_data["registration_office"],
|
||||||
|
CODELIST_REGISTRATION_OFFICE_ID
|
||||||
|
)
|
||||||
|
obj.responsible.registration_file_number = responsibility_data["registration_file_number"]
|
||||||
|
obj.responsible.conservation_office = self.konova_code_from_json(
|
||||||
|
responsibility_data["conservation_office"],
|
||||||
|
CODELIST_CONSERVATION_OFFICE_ID,
|
||||||
|
)
|
||||||
|
obj.responsible.conservation_file_number = responsibility_data["conservation_file_number"]
|
||||||
|
obj.responsible.handler = responsibility_data["handler"]
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def create_model_from_json(self, json_model, user):
|
||||||
|
""" Creates a new entry for the model based on the contents of json_model
|
||||||
|
|
||||||
|
Args:
|
||||||
|
json_model (dict): The json containing data
|
||||||
|
user (User): The API user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
created_id (str): The id of the newly created Intervention entry
|
||||||
|
"""
|
||||||
|
with transaction.atomic():
|
||||||
|
obj = self.initialize_objects(json_model, user)
|
||||||
|
|
||||||
|
# Fill in data to objects
|
||||||
|
properties = json_model["properties"]
|
||||||
|
obj.identifier = obj.generate_new_identifier()
|
||||||
|
obj.title = properties["title"]
|
||||||
|
self.set_responsibility(obj, properties["responsible"])
|
||||||
|
self.set_legal(obj, properties["legal"])
|
||||||
|
|
||||||
|
obj.responsible.save()
|
||||||
|
obj.geometry.save()
|
||||||
|
obj.legal.save()
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
obj.users.add(user)
|
||||||
|
|
||||||
|
celery_update_parcels.delay(obj.geometry.id)
|
||||||
|
|
||||||
|
return obj.id
|
||||||
|
|
||||||
|
def update_model_from_json(self, id, json_model, user):
|
||||||
|
""" Updates an entry for the model based on the contents of json_model
|
||||||
|
|
||||||
|
Args:
|
||||||
|
id (str): The object's id
|
||||||
|
json_model (dict): The json containing data
|
||||||
|
user (User): The API user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
created_id (str): The id of the newly created Intervention entry
|
||||||
|
"""
|
||||||
|
with transaction.atomic():
|
||||||
|
obj = self.get_obj_from_db(id, user)
|
||||||
|
|
||||||
|
# Fill in data to objects
|
||||||
|
properties = json_model["properties"]
|
||||||
|
obj.title = properties["title"]
|
||||||
|
self.set_responsibility(obj, properties["responsible"])
|
||||||
|
self.set_legal(obj, properties["legal"])
|
||||||
|
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
||||||
|
|
||||||
|
obj.responsible.save()
|
||||||
|
obj.geometry.save()
|
||||||
|
obj.legal.save()
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
obj.users.add(user)
|
||||||
|
|
||||||
|
celery_update_parcels.delay(obj.geometry.id)
|
||||||
|
|
||||||
|
return obj.id
|
218
api/utils/serializer/v1/serializer.py
Normal file
218
api/utils/serializer/v1/serializer.py
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 24.01.22
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
|
from api.utils.serializer.serializer import AbstractModelAPISerializer
|
||||||
|
from codelist.models import KonovaCode
|
||||||
|
from intervention.models import Responsibility, Legal
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
|
||||||
|
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 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 konova_code_from_json(self, json_str, code_list_identifier):
|
||||||
|
""" Returns a konova code instance
|
||||||
|
|
||||||
|
Args:
|
||||||
|
json_str (str): The value for the code (atom id)
|
||||||
|
code_list_identifier (str): From which konova code list this code is supposed to be from
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
if json_str is None or len(json_str) == 0:
|
||||||
|
return None
|
||||||
|
code = KonovaCode.objects.get(
|
||||||
|
atom_id=json_str,
|
||||||
|
code_lists__in=[code_list_identifier]
|
||||||
|
)
|
||||||
|
return code
|
||||||
|
|
||||||
|
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
|
@ -1,29 +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.v1.views import AbstractModelAPIViewV1
|
|
||||||
from intervention.models import Intervention
|
|
||||||
|
|
||||||
|
|
||||||
class APIInterventionViewV1(AbstractModelAPIViewV1):
|
|
||||||
model = Intervention
|
|
||||||
|
|
||||||
def compensations_to_json(self, qs: QuerySet):
|
|
||||||
return list(
|
|
||||||
qs.values(
|
|
||||||
"id", "identifier", "title"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
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())
|
|
@ -7,12 +7,13 @@ Created on: 21.01.22
|
|||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from django.db.models import QuerySet
|
|
||||||
from django.http import JsonResponse, HttpRequest
|
from django.http import JsonResponse, HttpRequest
|
||||||
|
|
||||||
|
from api.utils.serializer.v1.compensation import CompensationAPISerializerV1
|
||||||
|
from api.utils.serializer.v1.ecoaccount import EcoAccountAPISerializerV1
|
||||||
|
from api.utils.serializer.v1.ema import EmaAPISerializerV1
|
||||||
|
from api.utils.serializer.v1.intervention import InterventionAPISerializerV1
|
||||||
from api.views.views import AbstractModelAPIView
|
from api.views.views import AbstractModelAPIView
|
||||||
from codelist.models import KonovaCode
|
|
||||||
from intervention.models import Responsibility, Legal
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||||
@ -20,7 +21,7 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, request: HttpRequest, id):
|
def get(self, request: HttpRequest, id=None):
|
||||||
""" Handles the GET request
|
""" Handles the GET request
|
||||||
|
|
||||||
Performs the fetching and serialization of the data
|
Performs the fetching and serialization of the data
|
||||||
@ -32,205 +33,45 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
|||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.prepare_lookup(id)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = self.fetch_and_serialize()
|
if id is None:
|
||||||
|
raise AttributeError("No id provided")
|
||||||
|
self.serializer.prepare_lookup(id, self.user)
|
||||||
|
data = self.serializer.fetch_and_serialize()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self.return_error_response(e, 500)
|
return self.return_error_response(e, 500)
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
def model_to_geo_json(self, entry):
|
def post(self, request: HttpRequest, id=None):
|
||||||
""" Adds the basic data, which all elements hold
|
try:
|
||||||
|
body = request.body.decode("utf-8")
|
||||||
|
body = json.loads(body)
|
||||||
|
created_id = self.serializer.create_model_from_json(body, self.user)
|
||||||
|
except Exception as e:
|
||||||
|
return self.return_error_response(e, 500)
|
||||||
|
return JsonResponse({"id": created_id})
|
||||||
|
|
||||||
Args:
|
def put(self, request: HttpRequest, id=None):
|
||||||
entry (): The data entry
|
try:
|
||||||
|
body = request.body.decode("utf-8")
|
||||||
|
body = json.loads(body)
|
||||||
|
updated_id = self.serializer.update_model_from_json(id, body, self.user)
|
||||||
|
except Exception as e:
|
||||||
|
return self.return_error_response(e, 500)
|
||||||
|
return JsonResponse({"id": updated_id})
|
||||||
|
|
||||||
Returns:
|
|
||||||
|
|
||||||
"""
|
class InterventionAPIViewV1(AbstractModelAPIViewV1):
|
||||||
geom = entry.geometry.geom.geojson
|
serializer = InterventionAPISerializerV1
|
||||||
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:
|
class CompensationAPIViewV1(AbstractModelAPIViewV1):
|
||||||
id (str): The entries id
|
serializer = CompensationAPISerializerV1
|
||||||
|
|
||||||
Returns:
|
|
||||||
|
|
||||||
"""
|
class EcoAccountAPIViewV1(AbstractModelAPIViewV1):
|
||||||
self.lookup["id"] = id
|
serializer = EcoAccountAPISerializerV1
|
||||||
self.lookup["users__in"] = [self.user]
|
|
||||||
|
|
||||||
def konova_code_to_json(self, konova_code: KonovaCode):
|
|
||||||
""" Serializes KonovaCode model into json
|
|
||||||
|
|
||||||
Args:
|
class EmaAPIViewV1(AbstractModelAPIViewV1):
|
||||||
konova_code (KonovaCode): The KonovaCode entry
|
serializer = EmaAPISerializerV1
|
||||||
|
|
||||||
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
|
|
||||||
|
@ -5,10 +5,10 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
|||||||
Created on: 21.01.22
|
Created on: 21.01.22
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from abc import abstractmethod
|
|
||||||
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.views import View
|
from django.views import View
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
from api.models import APIUserToken
|
from api.models import APIUserToken
|
||||||
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
|
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
|
||||||
@ -22,10 +22,8 @@ class AbstractModelAPIView(View):
|
|||||||
https://datatracker.ietf.org/doc/html/rfc7946
|
https://datatracker.ietf.org/doc/html/rfc7946
|
||||||
|
|
||||||
"""
|
"""
|
||||||
model = None
|
serializer = None
|
||||||
user = None
|
user = None
|
||||||
lookup = None
|
|
||||||
properties_data = None
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
@ -37,45 +35,12 @@ class AbstractModelAPIView(View):
|
|||||||
"users__in": [], # must be set in subclasses
|
"users__in": [], # must be set in subclasses
|
||||||
}
|
}
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
self.serializer = self.serializer()
|
||||||
|
|
||||||
@abstractmethod
|
@csrf_exempt
|
||||||
def model_to_geo_json(self, entry):
|
|
||||||
""" Defines the model as geo json
|
|
||||||
|
|
||||||
Args:
|
|
||||||
entry (): The found entry from the database
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
|
|
||||||
"""
|
|
||||||
raise NotImplementedError("Must be implemented in subclasses")
|
|
||||||
|
|
||||||
@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:
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
serialized_data (dict)
|
|
||||||
"""
|
|
||||||
entry = self.model.objects.get(**self.lookup)
|
|
||||||
serialized_data = self.model_to_geo_json(entry)
|
|
||||||
return serialized_data
|
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
# Fetch the proper user from the given request header token
|
||||||
self.user = APIUserToken.get_user_from_token(request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None))
|
self.user = APIUserToken.get_user_from_token(request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None))
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
return self.return_error_response(e, 403)
|
return self.return_error_response(e, 403)
|
||||||
|
Loading…
Reference in New Issue
Block a user