#31 API basic implementation Compensation
* adds compensation fetching for API v1 * refactors filter into predefined lookup dict of super class (needs to be customized on subclasses)
This commit is contained in:
@@ -24,10 +24,19 @@ class AbstractModelAPIView(View):
|
||||
"""
|
||||
model = None
|
||||
user = None
|
||||
lookup = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.lookup = {
|
||||
"id": None, # must be set in subclasses
|
||||
"deleted__isnull": True,
|
||||
"users__in": [], # must be set in subclasses
|
||||
}
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
@abstractmethod
|
||||
def model_to_json(self, entry):
|
||||
""" Defines the returned json values of the model
|
||||
@@ -40,16 +49,15 @@ class AbstractModelAPIView(View):
|
||||
"""
|
||||
raise NotImplementedError("Must be implemented in subclasses")
|
||||
|
||||
def fetch_and_serialize(self, _filter):
|
||||
def fetch_and_serialize(self):
|
||||
""" Serializes the model entry according to the given lookup data
|
||||
|
||||
Args:
|
||||
_filter (dict): Lookup declarations
|
||||
|
||||
Returns:
|
||||
serialized_data (dict)
|
||||
"""
|
||||
qs = self.model.objects.filter(**_filter)
|
||||
qs = self.model.objects.filter(**self.lookup)
|
||||
serialized_data = {}
|
||||
for entry in qs:
|
||||
serialized_data[str(entry.pk)] = self.model_to_json(entry)
|
||||
|
||||
Reference in New Issue
Block a user