konova/api/utils/serializer.py

77 lines
1.8 KiB
Python
Raw Normal View History

"""
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