#31 API Improvement

* adds support for returning all shared data
* adds documentation
This commit is contained in:
mpeltriaux 2022-01-24 16:23:38 +01:00
parent d58ca3f324
commit f461a8e38d
5 changed files with 48 additions and 14 deletions

View File

@ -62,7 +62,12 @@ class AbstractModelAPISerializer:
Returns: Returns:
""" """
self.lookup["id"] = _id if _id is None:
# Return all objects
del self.lookup["id"]
else:
# Return certain objects
self.lookup["id"] = _id
self.lookup["users__in"] = [user] self.lookup["users__in"] = [user]
def fetch_and_serialize(self): def fetch_and_serialize(self):
@ -73,8 +78,10 @@ class AbstractModelAPISerializer:
Returns: Returns:
serialized_data (dict) serialized_data (dict)
""" """
entry = self.model.objects.get(**self.lookup) entries = self.model.objects.filter(**self.lookup)
serialized_data = self.model_to_geo_json(entry) serialized_data = {}
for entry in entries:
serialized_data[str(entry.id)] = self.model_to_geo_json(entry)
return serialized_data return serialized_data
@abstractmethod @abstractmethod

View File

@ -19,7 +19,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
model = Compensation model = Compensation
def prepare_lookup(self, id, user): def prepare_lookup(self, id, user):
self.lookup["id"] = id super().prepare_lookup(id, user)
del self.lookup["users__in"] del self.lookup["users__in"]
self.lookup["intervention__users__in"] = [user] self.lookup["intervention__users__in"] = [user]

View File

@ -90,6 +90,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
Returns: Returns:
obj obj
""" """
if legal_data is None:
return obj
obj.legal.registration_date = legal_data["registration_date"] obj.legal.registration_date = legal_data["registration_date"]
obj.legal.binding_date = legal_data["binding_date"] obj.legal.binding_date = legal_data["binding_date"]
obj.legal.process_type = self.konova_code_from_json( obj.legal.process_type = self.konova_code_from_json(
@ -110,6 +112,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
Returns: Returns:
obj obj
""" """
if responsibility_data is None:
return obj
obj.responsible.registration_office = self.konova_code_from_json( obj.responsible.registration_office = self.konova_code_from_json(
responsibility_data["registration_office"], responsibility_data["registration_office"],
CODELIST_REGISTRATION_OFFICE_ID CODELIST_REGISTRATION_OFFICE_ID
@ -134,6 +138,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
Returns: Returns:
obj (intervention) obj (intervention)
""" """
if payment_data is None:
return obj
payments = [] payments = []
for entry in payment_data: for entry in payment_data:
due_on = entry["due_on"] due_on = entry["due_on"]
@ -226,9 +232,9 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
properties = json_model["properties"] properties = json_model["properties"]
obj.title = properties["title"] obj.title = properties["title"]
obj.modified = update_action obj.modified = update_action
self.set_responsibility(obj, properties["responsible"]) self.set_responsibility(obj, properties.get("responsible", None))
self.set_legal(obj, properties["legal"]) self.set_legal(obj, properties.get("legal", None))
self.set_payments(obj, properties["payments"]) self.set_payments(obj, properties.get("payments", None))
obj.geometry.geom = self.create_geometry_from_json(json_model) obj.geometry.geom = self.create_geometry_from_json(json_model)
obj.geometry.modified = update_action obj.geometry.modified = update_action

View File

@ -50,6 +50,8 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
Returns: Returns:
serialized_json (dict) serialized_json (dict)
""" """
if konova_code is None:
return None
return { return {
"atom_id": konova_code.atom_id, "atom_id": konova_code.atom_id,
"long_name": konova_code.long_name, "long_name": konova_code.long_name,
@ -83,7 +85,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
Returns: Returns:
created_on (timestamp) created_on (timestamp)
""" """
return entry.created.timestamp return entry.created.timestamp if entry.created is not None else None
def modified_on_to_json(self, entry): def modified_on_to_json(self, entry):
""" Serializes the modified_on into json """ Serializes the modified_on into json
@ -95,7 +97,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
modified_on (timestamp) modified_on (timestamp)
""" """
modified_on = entry.modified or entry.created modified_on = entry.modified or entry.created
modified_on = modified_on.timestamp modified_on = modified_on.timestamp if modified_on is not None else None
return modified_on return modified_on

View File

@ -28,21 +28,29 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
Args: Args:
request (HttpRequest): The incoming request request (HttpRequest): The incoming request
id (str): The entries id id (str): The entries id (optional)
Returns: Returns:
response (JsonResponse)
""" """
try: try:
if id is None:
raise AttributeError("No id provided")
self.serializer.prepare_lookup(id, self.user) self.serializer.prepare_lookup(id, self.user)
data = self.serializer.fetch_and_serialize() data = self.serializer.fetch_and_serialize()
except Exception as e: except Exception as e:
return self.return_error_response(e, 500) return self.return_error_response(e, 500)
return JsonResponse(data) return JsonResponse(data)
def post(self, request: HttpRequest, id=None): def post(self, request: HttpRequest):
""" Handles the POST request
Performs creation of new data
Args:
request (HttpRequest): The incoming request
Returns:
response (JsonResponse)
"""
try: try:
body = request.body.decode("utf-8") body = request.body.decode("utf-8")
body = json.loads(body) body = json.loads(body)
@ -52,6 +60,17 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
return JsonResponse({"id": created_id}) return JsonResponse({"id": created_id})
def put(self, request: HttpRequest, id=None): def put(self, request: HttpRequest, id=None):
""" Handles the PUT request
Performs updating
Args:
request (HttpRequest): The incoming request
id (str): The entries id
Returns:
"""
try: try:
body = request.body.decode("utf-8") body = request.body.decode("utf-8")
body = json.loads(body) body = json.loads(body)