diff --git a/api/utils/serializer/serializer.py b/api/utils/serializer/serializer.py index 3820b302..2a7639bb 100644 --- a/api/utils/serializer/serializer.py +++ b/api/utils/serializer/serializer.py @@ -62,7 +62,12 @@ class AbstractModelAPISerializer: 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] def fetch_and_serialize(self): @@ -73,8 +78,10 @@ class AbstractModelAPISerializer: Returns: serialized_data (dict) """ - entry = self.model.objects.get(**self.lookup) - serialized_data = self.model_to_geo_json(entry) + entries = self.model.objects.filter(**self.lookup) + serialized_data = {} + for entry in entries: + serialized_data[str(entry.id)] = self.model_to_geo_json(entry) return serialized_data @abstractmethod diff --git a/api/utils/serializer/v1/compensation.py b/api/utils/serializer/v1/compensation.py index e2ec2609..eaf1694a 100644 --- a/api/utils/serializer/v1/compensation.py +++ b/api/utils/serializer/v1/compensation.py @@ -19,7 +19,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa model = Compensation def prepare_lookup(self, id, user): - self.lookup["id"] = id + super().prepare_lookup(id, user) del self.lookup["users__in"] self.lookup["intervention__users__in"] = [user] diff --git a/api/utils/serializer/v1/intervention.py b/api/utils/serializer/v1/intervention.py index 34fb75c5..b77d7e33 100644 --- a/api/utils/serializer/v1/intervention.py +++ b/api/utils/serializer/v1/intervention.py @@ -90,6 +90,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, Returns: obj """ + if legal_data is None: + return obj obj.legal.registration_date = legal_data["registration_date"] obj.legal.binding_date = legal_data["binding_date"] obj.legal.process_type = self.konova_code_from_json( @@ -110,6 +112,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, Returns: obj """ + if responsibility_data is None: + return obj obj.responsible.registration_office = self.konova_code_from_json( responsibility_data["registration_office"], CODELIST_REGISTRATION_OFFICE_ID @@ -134,6 +138,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, Returns: obj (intervention) """ + if payment_data is None: + return obj payments = [] for entry in payment_data: due_on = entry["due_on"] @@ -226,9 +232,9 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, properties = json_model["properties"] obj.title = properties["title"] obj.modified = update_action - self.set_responsibility(obj, properties["responsible"]) - self.set_legal(obj, properties["legal"]) - self.set_payments(obj, properties["payments"]) + self.set_responsibility(obj, properties.get("responsible", None)) + self.set_legal(obj, properties.get("legal", None)) + self.set_payments(obj, properties.get("payments", None)) obj.geometry.geom = self.create_geometry_from_json(json_model) obj.geometry.modified = update_action diff --git a/api/utils/serializer/v1/serializer.py b/api/utils/serializer/v1/serializer.py index af3fa96f..e97b1ee9 100644 --- a/api/utils/serializer/v1/serializer.py +++ b/api/utils/serializer/v1/serializer.py @@ -50,6 +50,8 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): Returns: serialized_json (dict) """ + if konova_code is None: + return None return { "atom_id": konova_code.atom_id, "long_name": konova_code.long_name, @@ -83,7 +85,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): Returns: 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): """ Serializes the modified_on into json @@ -95,7 +97,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): modified_on (timestamp) """ 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 diff --git a/api/views/v1/views.py b/api/views/v1/views.py index 7e271dd5..fa55458d 100644 --- a/api/views/v1/views.py +++ b/api/views/v1/views.py @@ -28,21 +28,29 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): Args: request (HttpRequest): The incoming request - id (str): The entries id + id (str): The entries id (optional) Returns: - + response (JsonResponse) """ try: - if id is None: - raise AttributeError("No id provided") self.serializer.prepare_lookup(id, self.user) data = self.serializer.fetch_and_serialize() except Exception as e: return self.return_error_response(e, 500) 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: body = request.body.decode("utf-8") body = json.loads(body) @@ -52,6 +60,17 @@ class AbstractModelAPIViewV1(AbstractModelAPIView): return JsonResponse({"id": created_id}) 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: body = request.body.decode("utf-8") body = json.loads(body)