#31 API basic implementation Refactor
* reorganizes code into proper api/utils/serializer subclasses to keep serialization logic away from regular view logic
This commit is contained in:
@@ -5,7 +5,6 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.views import View
|
||||
@@ -22,10 +21,8 @@ class AbstractModelAPIView(View):
|
||||
https://datatracker.ietf.org/doc/html/rfc7946
|
||||
|
||||
"""
|
||||
model = None
|
||||
serializer = None
|
||||
user = None
|
||||
lookup = None
|
||||
properties_data = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
@@ -37,45 +34,11 @@ class AbstractModelAPIView(View):
|
||||
"users__in": [], # must be set in subclasses
|
||||
}
|
||||
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")
|
||||
|
||||
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
|
||||
self.serializer = self.serializer()
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
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))
|
||||
except PermissionError as e:
|
||||
return self.return_error_response(e, 403)
|
||||
|
||||
Reference in New Issue
Block a user