#31 API basic implementation Refactor
* reorganizes code into proper api/utils/serializer subclasses to keep serialization logic away from regular view logicpull/90/head
parent
8d400b4ffe
commit
45ac5b68b9
@ -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
|
||||
|
||||
"""
|
@ -0,0 +1,76 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 24.01.22
|
||||
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class AbstractModelAPISerializer:
|
||||
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
|
||||
"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
|
@ -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
|
||||
|
||||
"""
|
@ -0,0 +1,200 @@
|
||||
"""
|
||||
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 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 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
|
Loading…
Reference in New Issue