From 27c1de2c537e4836324b6e25d4459f0df20f5a84 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 28 Jan 2022 09:44:16 +0100 Subject: [PATCH] #31 API protected visibility * reworks most internal API methods for de/serializing from public to protected visibility * moves test_api_sharing.py into /share subfolder of tests --- api/tests/v1/create/test_api_create.py | 2 +- api/tests/v1/delete/test_api_delete.py | 2 +- api/tests/v1/share/__init__.py | 7 +++ api/tests/v1/{ => share}/test_api_sharing.py | 0 api/utils/serializer/serializer.py | 12 ++-- api/utils/serializer/v1/compensation.py | 36 +++++------ api/utils/serializer/v1/ecoaccount.py | 62 +++++++++---------- api/utils/serializer/v1/ema.py | 50 +++++++-------- api/utils/serializer/v1/intervention.py | 38 ++++++------ api/utils/serializer/v1/serializer.py | 64 ++++++++++---------- 10 files changed, 140 insertions(+), 133 deletions(-) create mode 100644 api/tests/v1/share/__init__.py rename api/tests/v1/{ => share}/test_api_sharing.py (100%) diff --git a/api/tests/v1/create/test_api_create.py b/api/tests/v1/create/test_api_create.py index 5e7134a1..c52db868 100644 --- a/api/tests/v1/create/test_api_create.py +++ b/api/tests/v1/create/test_api_create.py @@ -9,7 +9,7 @@ import json from django.urls import reverse -from api.tests.v1.test_api_sharing import BaseAPIV1TestCase +from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase class APIV1CreateTestCase(BaseAPIV1TestCase): diff --git a/api/tests/v1/delete/test_api_delete.py b/api/tests/v1/delete/test_api_delete.py index 1c5503b1..a898d105 100644 --- a/api/tests/v1/delete/test_api_delete.py +++ b/api/tests/v1/delete/test_api_delete.py @@ -10,7 +10,7 @@ import json from django.urls import reverse -from api.tests.v1.test_api_sharing import BaseAPIV1TestCase +from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase class APIV1DeleteTestCase(BaseAPIV1TestCase): diff --git a/api/tests/v1/share/__init__.py b/api/tests/v1/share/__init__.py new file mode 100644 index 00000000..3659b8cb --- /dev/null +++ b/api/tests/v1/share/__init__.py @@ -0,0 +1,7 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 28.01.22 + +""" diff --git a/api/tests/v1/test_api_sharing.py b/api/tests/v1/share/test_api_sharing.py similarity index 100% rename from api/tests/v1/test_api_sharing.py rename to api/tests/v1/share/test_api_sharing.py diff --git a/api/utils/serializer/serializer.py b/api/utils/serializer/serializer.py index ac4bf227..443e10c5 100644 --- a/api/utils/serializer/serializer.py +++ b/api/utils/serializer/serializer.py @@ -31,7 +31,7 @@ class AbstractModelAPISerializer: super().__init__(*args, **kwargs) @abstractmethod - def model_to_geo_json(self, entry): + def _model_to_geo_json(self, entry): """ Defines the model as geo json Args: @@ -43,7 +43,7 @@ class AbstractModelAPISerializer: raise NotImplementedError("Must be implemented in subclasses") @abstractmethod - def extend_properties_data(self, entry): + def _extend_properties_data(self, entry): """ Defines the 'properties' part of geo json Args: @@ -83,7 +83,7 @@ class AbstractModelAPISerializer: entries = self.model.objects.filter(**self.lookup) serialized_data = {} for entry in entries: - serialized_data[str(entry.id)] = self.model_to_geo_json(entry) + serialized_data[str(entry.id)] = self._model_to_geo_json(entry) return serialized_data @abstractmethod @@ -113,7 +113,7 @@ class AbstractModelAPISerializer: """ raise NotImplementedError("Must be implemented in subclasses") - def create_geometry_from_json(self, geojson) -> GEOSGeometry: + def _create_geometry_from_json(self, geojson) -> GEOSGeometry: """ Creates a GEOSGeometry object based on the given geojson Args: @@ -129,7 +129,7 @@ class AbstractModelAPISerializer: geometry = None return geometry - def get_obj_from_db(self, id, user): + def _get_obj_from_db(self, id, user): """ Returns the object from database Fails if id not found or user does not have shared access @@ -151,7 +151,7 @@ class AbstractModelAPISerializer: return obj @abstractmethod - def initialize_objects(self, json_model, user): + def _initialize_objects(self, json_model, user): """ Initializes all needed objects from the json_model data Does not persist data to the DB! diff --git a/api/utils/serializer/v1/compensation.py b/api/utils/serializer/v1/compensation.py index a40df14f..24e499ef 100644 --- a/api/utils/serializer/v1/compensation.py +++ b/api/utils/serializer/v1/compensation.py @@ -31,16 +31,16 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa "title": entry.title, } - def extend_properties_data(self, entry): + def _extend_properties_data(self, entry): self.properties_data["is_cef"] = entry.is_cef self.properties_data["is_coherence_keeping"] = entry.is_coherence_keeping self.properties_data["intervention"] = self.intervention_to_json(entry.intervention) - self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all()) - self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all()) - self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all()) - self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all()) + self.properties_data["before_states"] = self._compensation_state_to_json(entry.before_states.all()) + self.properties_data["after_states"] = self._compensation_state_to_json(entry.after_states.all()) + self.properties_data["actions"] = self._compensation_actions_to_json(entry.actions.all()) + self.properties_data["deadlines"] = self._deadlines_to_json(entry.deadlines.all()) - def initialize_objects(self, json_model, user): + def _initialize_objects(self, json_model, user): """ Initializes all needed objects from the json_model data Does not persist data to the DB! @@ -54,7 +54,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa """ create_action = UserActionLogEntry.get_created_action(user, comment="API Import") # Create geometry - json_geom = self.create_geometry_from_json(json_model) + json_geom = self._create_geometry_from_json(json_model) geometry = Geometry() geometry.geom = json_geom geometry.created = create_action @@ -105,7 +105,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa created_id (str): The id of the newly created Compensation entry """ with transaction.atomic(): - obj = self.initialize_objects(json_model, user) + obj = self._initialize_objects(json_model, user) # Fill in data to objects properties = json_model["properties"] @@ -118,10 +118,10 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa obj.geometry.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(obj.created) @@ -142,7 +142,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa """ with transaction.atomic(): update_action = UserActionLogEntry.get_edited_action(user, "API update") - obj = self.get_obj_from_db(id, user) + obj = self._get_obj_from_db(id, user) # Fill in data to objects properties = json_model["properties"] @@ -150,17 +150,17 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa obj.is_cef = properties["is_cef"] obj.is_coherence_keeping = properties["is_coherence_keeping"] obj.modified = update_action - 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 = self.set_intervention(obj, properties["intervention"], user) obj.geometry.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(update_action) diff --git a/api/utils/serializer/v1/ecoaccount.py b/api/utils/serializer/v1/ecoaccount.py index 59aa5f76..0c3e1399 100644 --- a/api/utils/serializer/v1/ecoaccount.py +++ b/api/utils/serializer/v1/ecoaccount.py @@ -24,30 +24,30 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, DeductableAPISerializerV1Mixin): model = EcoAccount - def extend_properties_data(self, entry): + def _extend_properties_data(self, entry): self.properties_data["deductable_surface"] = entry.deductable_surface self.properties_data["deductable_surface_available"] = entry.deductable_surface - entry.get_deductions_surface() - self.properties_data["responsible"] = self.responsible_to_json(entry.responsible) - self.properties_data["legal"] = self.legal_to_json(entry.legal) - self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all()) - self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all()) - self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all()) - self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all()) - self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.all()) + self.properties_data["responsible"] = self._responsible_to_json(entry.responsible) + self.properties_data["legal"] = self._legal_to_json(entry.legal) + self.properties_data["before_states"] = self._compensation_state_to_json(entry.before_states.all()) + self.properties_data["after_states"] = self._compensation_state_to_json(entry.after_states.all()) + self.properties_data["actions"] = self._compensation_actions_to_json(entry.actions.all()) + self.properties_data["deadlines"] = self._deadlines_to_json(entry.deadlines.all()) + self.properties_data["deductions"] = self._deductions_to_json(entry.deductions.all()) - def legal_to_json(self, legal: Legal): + def _legal_to_json(self, legal: Legal): return { "agreement_date": legal.registration_date, } - def responsible_to_json(self, responsible: Responsibility): + def _responsible_to_json(self, responsible: Responsibility): return { - "conservation_office": self.konova_code_to_json(responsible.conservation_office), + "conservation_office": self._konova_code_to_json(responsible.conservation_office), "conservation_file_number": responsible.conservation_file_number, "handler": responsible.handler, } - def set_responsibility(self, obj, responsibility_data: dict): + def _set_responsibility(self, obj, responsibility_data: dict): """ Sets the responsible data contents to the provided responsibility_data dict Args: @@ -59,7 +59,7 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, """ if responsibility_data is None: return obj - obj.responsible.conservation_office = self.konova_code_from_json( + obj.responsible.conservation_office = self._konova_code_from_json( responsibility_data["conservation_office"], CODELIST_CONSERVATION_OFFICE_ID, ) @@ -67,11 +67,11 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, obj.responsible.handler = responsibility_data["handler"] return obj - def set_legal(self, obj, legal_data): + def _set_legal(self, obj, legal_data): obj.legal.registration_date = legal_data.get("agreement_date", None) return obj - def initialize_objects(self, json_model, user): + def _initialize_objects(self, json_model, user): """ Initializes all needed objects from the json_model data Does not persist data to the DB! @@ -85,7 +85,7 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, """ create_action = UserActionLogEntry.get_created_action(user, comment="API Import") # Create geometry - json_geom = self.create_geometry_from_json(json_model) + json_geom = self._create_geometry_from_json(json_model) geometry = Geometry() geometry.geom = json_geom geometry.created = create_action @@ -110,7 +110,7 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, created_id (str): The id of the newly created EcoAccount entry """ with transaction.atomic(): - obj = self.initialize_objects(json_model, user) + obj = self._initialize_objects(json_model, user) # Fill in data to objects properties = json_model["properties"] @@ -124,18 +124,18 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, if obj.deductable_surface < 0: raise ValueError("Deductable surface (m²) must be greater or equal 0") - obj = self.set_responsibility(obj, properties["responsible"]) - obj = self.set_legal(obj, properties["legal"]) + obj = self._set_responsibility(obj, properties["responsible"]) + obj = self._set_legal(obj, properties["legal"]) obj.geometry.save() obj.responsible.save() obj.legal.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(obj.created) obj.users.add(user) @@ -157,27 +157,27 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1, """ with transaction.atomic(): update_action = UserActionLogEntry.get_edited_action(user, "API update") - obj = self.get_obj_from_db(id, user) + obj = self._get_obj_from_db(id, user) # Fill in data to objects properties = json_model["properties"] obj.title = properties["title"] obj.deductable_surface = float(properties["deductable_surface"]) obj.modified = update_action - 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 = self.set_responsibility(obj, properties["responsible"]) - obj = self.set_legal(obj, properties["legal"]) + obj = self._set_responsibility(obj, properties["responsible"]) + obj = self._set_legal(obj, properties["legal"]) obj.geometry.save() obj.responsible.save() obj.legal.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(update_action) diff --git a/api/utils/serializer/v1/ema.py b/api/utils/serializer/v1/ema.py index 728faa84..547f223f 100644 --- a/api/utils/serializer/v1/ema.py +++ b/api/utils/serializer/v1/ema.py @@ -20,21 +20,21 @@ from user.models import UserActionLogEntry class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin): model = Ema - def extend_properties_data(self, entry): - self.properties_data["responsible"] = self.responsible_to_json(entry.responsible) - self.properties_data["before_states"] = self.compensation_state_to_json(entry.before_states.all()) - self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all()) - self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all()) - self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all()) + def _extend_properties_data(self, entry): + self.properties_data["responsible"] = self._responsible_to_json(entry.responsible) + self.properties_data["before_states"] = self._compensation_state_to_json(entry.before_states.all()) + self.properties_data["after_states"] = self._compensation_state_to_json(entry.after_states.all()) + self.properties_data["actions"] = self._compensation_actions_to_json(entry.actions.all()) + self.properties_data["deadlines"] = self._deadlines_to_json(entry.deadlines.all()) - def responsible_to_json(self, responsible: Responsibility): + def _responsible_to_json(self, responsible: Responsibility): return { - "conservation_office": self.konova_code_to_json(responsible.conservation_office), + "conservation_office": self._konova_code_to_json(responsible.conservation_office), "conservation_file_number": responsible.conservation_file_number, "handler": responsible.handler, } - def set_responsibility(self, obj, responsibility_data: dict): + def _set_responsibility(self, obj, responsibility_data: dict): """ Sets the responsible data contents to the provided responsibility_data dict Args: @@ -46,7 +46,7 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe """ if responsibility_data is None: return obj - obj.responsible.conservation_office = self.konova_code_from_json( + obj.responsible.conservation_office = self._konova_code_from_json( responsibility_data["conservation_office"], CODELIST_CONSERVATION_OFFICE_ID, ) @@ -54,7 +54,7 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe obj.responsible.handler = responsibility_data["handler"] return obj - def initialize_objects(self, json_model, user): + def _initialize_objects(self, json_model, user): """ Initializes all needed objects from the json_model data Does not persist data to the DB! @@ -68,7 +68,7 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe """ create_action = UserActionLogEntry.get_created_action(user, comment="API Import") # Create geometry - json_geom = self.create_geometry_from_json(json_model) + json_geom = self._create_geometry_from_json(json_model) geometry = Geometry() geometry.geom = json_geom geometry.created = create_action @@ -92,22 +92,22 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe created_id (str): The id of the newly created Ema entry """ with transaction.atomic(): - obj = self.initialize_objects(json_model, user) + obj = self._initialize_objects(json_model, user) # Fill in data to objects properties = json_model["properties"] obj.identifier = obj.generate_new_identifier() obj.title = properties["title"] - obj = self.set_responsibility(obj, properties["responsible"]) + obj = self._set_responsibility(obj, properties["responsible"]) obj.geometry.save() obj.responsible.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(obj.created) obj.users.add(user) @@ -129,24 +129,24 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe """ with transaction.atomic(): update_action = UserActionLogEntry.get_edited_action(user, "API update") - obj = self.get_obj_from_db(id, user) + obj = self._get_obj_from_db(id, user) # Fill in data to objects properties = json_model["properties"] obj.title = properties["title"] obj.modified = update_action - 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 = self.set_responsibility(obj, properties["responsible"]) + obj = self._set_responsibility(obj, properties["responsible"]) obj.geometry.save() obj.responsible.save() obj.save() - obj = self.set_compensation_actions(obj, properties["actions"]) - obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states) - obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) - obj = self.set_deadlines(obj, properties["deadlines"]) + obj = self._set_compensation_actions(obj, properties["actions"]) + obj = self._set_compensation_states(obj, properties["before_states"], obj.before_states) + obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states) + obj = self._set_deadlines(obj, properties["deadlines"]) obj.log.add(update_action) diff --git a/api/utils/serializer/v1/intervention.py b/api/utils/serializer/v1/intervention.py index a36b547b..9fa7b71c 100644 --- a/api/utils/serializer/v1/intervention.py +++ b/api/utils/serializer/v1/intervention.py @@ -23,14 +23,14 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, DeductableAPISerializerV1Mixin): model = Intervention - def compensations_to_json(self, qs: QuerySet): + def _compensations_to_json(self, qs: QuerySet): return list( qs.values( "id", "identifier", "title" ) ) - def payments_to_json(self, qs: QuerySet): + def _payments_to_json(self, qs: QuerySet): """ Serializes payments into json Args: @@ -41,14 +41,14 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, """ return list(qs.values("amount", "due_on", "comment")) - def extend_properties_data(self, entry): - self.properties_data["responsible"] = self.responsible_to_json(entry.responsible) - self.properties_data["legal"] = self.legal_to_json(entry.legal) - self.properties_data["compensations"] = self.compensations_to_json(entry.compensations.all()) - self.properties_data["payments"] = self.payments_to_json(entry.payments.all()) - self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.all()) + def _extend_properties_data(self, entry): + self.properties_data["responsible"] = self._responsible_to_json(entry.responsible) + self.properties_data["legal"] = self._legal_to_json(entry.legal) + self.properties_data["compensations"] = self._compensations_to_json(entry.compensations.all()) + self.properties_data["payments"] = self._payments_to_json(entry.payments.all()) + self.properties_data["deductions"] = self._deductions_to_json(entry.deductions.all()) - def initialize_objects(self, json_model, user): + def _initialize_objects(self, json_model, user): """ Initializes all needed objects from the json_model data Does not persist data to the DB! @@ -62,7 +62,7 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, """ create_action = UserActionLogEntry.get_created_action(user, comment="API Import") # Create geometry - json_geom = self.create_geometry_from_json(json_model) + json_geom = self._create_geometry_from_json(json_model) geometry = Geometry() geometry.geom = json_geom geometry.created = create_action @@ -78,7 +78,7 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, obj.responsible = resp return obj - def set_payments(self, obj, payment_data): + def _set_payments(self, obj, payment_data): """ Sets the linked Payment data according to the given payment_data @@ -143,14 +143,14 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, created_id (str): The id of the newly created Intervention entry """ with transaction.atomic(): - obj = self.initialize_objects(json_model, user) + obj = self._initialize_objects(json_model, user) # Fill in data to objects properties = json_model["properties"] obj.identifier = obj.generate_new_identifier() obj.title = properties["title"] - self.set_responsibility(obj, properties["responsible"]) - self.set_legal(obj, properties["legal"]) + self._set_responsibility(obj, properties["responsible"]) + self._set_legal(obj, properties["legal"]) obj.responsible.save() obj.geometry.save() @@ -177,15 +177,15 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, """ with transaction.atomic(): update_action = UserActionLogEntry.get_edited_action(user, "API update") - obj = self.get_obj_from_db(id, user) + obj = self._get_obj_from_db(id, user) # Fill in data to objects properties = json_model["properties"] obj.title = properties["title"] - 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) + 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 obj.responsible.save() diff --git a/api/utils/serializer/v1/serializer.py b/api/utils/serializer/v1/serializer.py index 7d6910d5..baff113b 100644 --- a/api/utils/serializer/v1/serializer.py +++ b/api/utils/serializer/v1/serializer.py @@ -21,7 +21,7 @@ from konova.utils.message_templates import DATA_UNSHARED class AbstractModelAPISerializerV1(AbstractModelAPISerializer): - def model_to_geo_json(self, entry): + def _model_to_geo_json(self, entry): """ Adds the basic data, which all elements hold Args: @@ -36,14 +36,14 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): "id": entry.id, "identifier": entry.identifier, "title": entry.title, - "created_on": self.created_on_to_json(entry), - "modified_on": self.modified_on_to_json(entry), + "created_on": self._created_on_to_json(entry), + "modified_on": self._modified_on_to_json(entry), } - self.extend_properties_data(entry) + self._extend_properties_data(entry) geo_json["properties"] = self.properties_data return geo_json - def konova_code_to_json(self, konova_code: KonovaCode): + def _konova_code_to_json(self, konova_code: KonovaCode): """ Serializes KonovaCode model into json Args: @@ -60,7 +60,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): "short_name": konova_code.short_name, } - def konova_code_from_json(self, json_str, code_list_identifier): + def _konova_code_from_json(self, json_str, code_list_identifier): """ Returns a konova code instance Args: @@ -78,7 +78,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): ) return code - def created_on_to_json(self, entry): + def _created_on_to_json(self, entry): """ Serializes the created_on into json Args: @@ -89,7 +89,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): """ 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 Args: @@ -112,7 +112,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): Returns: """ - entry = self.get_obj_from_db(id, user) + entry = self._get_obj_from_db(id, user) is_shared = entry.is_shared_with(user) if not is_shared: raise PermissionError(DATA_UNSHARED) @@ -128,7 +128,7 @@ class DeductableAPISerializerV1Mixin: class Meta: abstract = True - def deductions_to_json(self, qs: QuerySet): + def _deductions_to_json(self, qs: QuerySet): """ Serializes eco account deductions into json Args: @@ -160,7 +160,7 @@ class ResponsibilityAPISerializerV1Mixin: class Meta: abstract = True - def responsible_to_json(self, responsible: Responsibility): + def _responsible_to_json(self, responsible: Responsibility): """ Serializes Responsibility model into json Args: @@ -170,14 +170,14 @@ class ResponsibilityAPISerializerV1Mixin: serialized_json (dict) """ return { - "registration_office": self.konova_code_to_json(responsible.registration_office), + "registration_office": self._konova_code_to_json(responsible.registration_office), "registration_file_number": responsible.registration_file_number, - "conservation_office": self.konova_code_to_json(responsible.conservation_office), + "conservation_office": self._konova_code_to_json(responsible.conservation_office), "conservation_file_number": responsible.conservation_file_number, "handler": responsible.handler, } - def set_responsibility(self, obj, responsibility_data: dict): + def _set_responsibility(self, obj, responsibility_data: dict): """ Sets the responsible data contents to the provided responsibility_data dict Args: @@ -189,12 +189,12 @@ class ResponsibilityAPISerializerV1Mixin: """ 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"], CODELIST_REGISTRATION_OFFICE_ID ) obj.responsible.registration_file_number = responsibility_data["registration_file_number"] - obj.responsible.conservation_office = self.konova_code_from_json( + obj.responsible.conservation_office = self._konova_code_from_json( responsibility_data["conservation_office"], CODELIST_CONSERVATION_OFFICE_ID, ) @@ -207,7 +207,7 @@ class LegalAPISerializerV1Mixin: class Meta: abstract = True - def legal_to_json(self, legal: Legal): + def _legal_to_json(self, legal: Legal): """ Serializes Legal model into json Args: @@ -219,11 +219,11 @@ class LegalAPISerializerV1Mixin: return { "registration_date": legal.registration_date, "binding_date": legal.binding_date, - "process_type": self.konova_code_to_json(legal.process_type), - "laws": [self.konova_code_to_json(law) for law in legal.laws.all()], + "process_type": self._konova_code_to_json(legal.process_type), + "laws": [self._konova_code_to_json(law) for law in legal.laws.all()], } - def set_legal(self, obj, legal_data): + def _set_legal(self, obj, legal_data): """ Sets the legal data contents to the provided legal_data dict Args: @@ -237,11 +237,11 @@ class LegalAPISerializerV1Mixin: return obj obj.legal.registration_date = legal_data.get("registration_date", None) obj.legal.binding_date = legal_data.get("binding_date", None) - obj.legal.process_type = self.konova_code_from_json( + obj.legal.process_type = self._konova_code_from_json( legal_data.get("process_type", None), CODELIST_PROCESS_TYPE_ID, ) - laws = [self.konova_code_from_json(law, CODELIST_LAW_ID) for law in legal_data.get("laws", [])] + laws = [self._konova_code_from_json(law, CODELIST_LAW_ID) for law in legal_data.get("laws", [])] obj.legal.laws.set(laws) return obj @@ -250,7 +250,7 @@ class AbstractCompensationAPISerializerV1Mixin: class Meta: abstract = True - def set_deadlines(self, obj, deadline_data): + def _set_deadlines(self, obj, deadline_data): """ Sets the linked deadline data according to the given deadline_data @@ -293,7 +293,7 @@ class AbstractCompensationAPISerializerV1Mixin: obj.deadlines.set(deadlines) return obj - def set_compensation_states(self, obj, states_data, states_manager): + def _set_compensation_states(self, obj, states_data, states_manager): """ Sets the linked compensation state data according to the given states_data @@ -327,7 +327,7 @@ class AbstractCompensationAPISerializerV1Mixin: else: # Create and add id to list new_state = CompensationState.objects.create( - biotope_type=self.konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID), + biotope_type=self._konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID), surface=surface ) states.append(new_state.id) @@ -335,7 +335,7 @@ class AbstractCompensationAPISerializerV1Mixin: states_manager.set(states) return obj - def set_compensation_actions(self, obj, actions_data): + def _set_compensation_actions(self, obj, actions_data): """ Sets the linked compensation action data according to the given actions_data @@ -374,7 +374,7 @@ class AbstractCompensationAPISerializerV1Mixin: else: # Create and add id to list new_action = CompensationAction.objects.create( - action_type=self.konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID), + action_type=self._konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID), amount=amount, unit=unit, comment=comment, @@ -383,7 +383,7 @@ class AbstractCompensationAPISerializerV1Mixin: obj.actions.set(actions) return obj - def compensation_state_to_json(self, qs: QuerySet): + def _compensation_state_to_json(self, qs: QuerySet): """ Serializes compensation states into json Args: @@ -394,13 +394,13 @@ class AbstractCompensationAPISerializerV1Mixin: """ return [ { - "biotope": self.konova_code_to_json(entry.biotope_type), + "biotope": self._konova_code_to_json(entry.biotope_type), "surface": entry.surface, } for entry in qs ] - def compensation_actions_to_json(self, qs: QuerySet): + def _compensation_actions_to_json(self, qs: QuerySet): """ Serializes CompensationActions into json Args: @@ -411,7 +411,7 @@ class AbstractCompensationAPISerializerV1Mixin: """ return [ { - "action": self.konova_code_to_json(entry.action_type), + "action": self._konova_code_to_json(entry.action_type), "amount": entry.amount, "unit": entry.unit, "comment": entry.comment, @@ -419,7 +419,7 @@ class AbstractCompensationAPISerializerV1Mixin: for entry in qs ] - def deadlines_to_json(self, qs: QuerySet): + def _deadlines_to_json(self, qs: QuerySet): """ Serializes deadlines into json Args: