""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 21.01.22 """ from abc import abstractmethod from django.views import View class AbstractModelAPIView(View): """ Base class for API views The API must follow the GeoJSON Specification RFC 7946 https://geojson.org/ https://datatracker.ietf.org/doc/html/rfc7946 """ model = None class Meta: abstract = True @abstractmethod def model_to_json(self, entry): """ Defines the returned json values of the model Args: entry (): The found entry from the database Returns: """ raise NotImplementedError("Must be implemented in subclasses") def fetch_and_serialize(self, lookup_field, lookup_val): """ Serializes the model entry according to the given lookup data Args: lookup_field (): Which field used for lookup lookup_val (): Value for lookup Returns: serialized_data (dict) """ _filters = { lookup_field: lookup_val } qs = self.model.objects.filter(**_filters) serialized_data = {} for entry in qs: serialized_data[str(entry.pk)] = self.model_to_json(entry) return serialized_data