#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
This commit is contained in:
mpeltriaux 2022-01-28 09:44:16 +01:00
parent a7890948b7
commit df309c6124
10 changed files with 140 additions and 133 deletions

View File

@ -9,7 +9,7 @@ import json
from django.urls import reverse 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): class APIV1CreateTestCase(BaseAPIV1TestCase):

View File

@ -10,7 +10,7 @@ import json
from django.urls import reverse 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): class APIV1DeleteTestCase(BaseAPIV1TestCase):

View File

@ -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
"""

View File

@ -31,7 +31,7 @@ class AbstractModelAPISerializer:
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@abstractmethod @abstractmethod
def model_to_geo_json(self, entry): def _model_to_geo_json(self, entry):
""" Defines the model as geo json """ Defines the model as geo json
Args: Args:
@ -43,7 +43,7 @@ class AbstractModelAPISerializer:
raise NotImplementedError("Must be implemented in subclasses") raise NotImplementedError("Must be implemented in subclasses")
@abstractmethod @abstractmethod
def extend_properties_data(self, entry): def _extend_properties_data(self, entry):
""" Defines the 'properties' part of geo json """ Defines the 'properties' part of geo json
Args: Args:
@ -83,7 +83,7 @@ class AbstractModelAPISerializer:
entries = self.model.objects.filter(**self.lookup) entries = self.model.objects.filter(**self.lookup)
serialized_data = {} serialized_data = {}
for entry in entries: 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 return serialized_data
@abstractmethod @abstractmethod
@ -113,7 +113,7 @@ class AbstractModelAPISerializer:
""" """
raise NotImplementedError("Must be implemented in subclasses") 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 """ Creates a GEOSGeometry object based on the given geojson
Args: Args:
@ -129,7 +129,7 @@ class AbstractModelAPISerializer:
geometry = None geometry = None
return geometry return geometry
def get_obj_from_db(self, id, user): def _get_obj_from_db(self, id, user):
""" Returns the object from database """ Returns the object from database
Fails if id not found or user does not have shared access Fails if id not found or user does not have shared access
@ -151,7 +151,7 @@ class AbstractModelAPISerializer:
return obj return obj
@abstractmethod @abstractmethod
def initialize_objects(self, json_model, user): def _initialize_objects(self, json_model, user):
""" Initializes all needed objects from the json_model data """ Initializes all needed objects from the json_model data
Does not persist data to the DB! Does not persist data to the DB!

View File

@ -31,16 +31,16 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
"title": entry.title, "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_cef"] = entry.is_cef
self.properties_data["is_coherence_keeping"] = entry.is_coherence_keeping self.properties_data["is_coherence_keeping"] = entry.is_coherence_keeping
self.properties_data["intervention"] = self.intervention_to_json(entry.intervention) 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["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["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["actions"] = self._compensation_actions_to_json(entry.actions.all())
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.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 """ Initializes all needed objects from the json_model data
Does not persist data to the DB! 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_action = UserActionLogEntry.get_created_action(user, comment="API Import")
# Create geometry # Create geometry
json_geom = self.create_geometry_from_json(json_model) json_geom = self._create_geometry_from_json(json_model)
geometry = Geometry() geometry = Geometry()
geometry.geom = json_geom geometry.geom = json_geom
geometry.created = create_action geometry.created = create_action
@ -105,7 +105,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
created_id (str): The id of the newly created Compensation entry created_id (str): The id of the newly created Compensation entry
""" """
with transaction.atomic(): with transaction.atomic():
obj = self.initialize_objects(json_model, user) obj = self._initialize_objects(json_model, user)
# Fill in data to objects # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
@ -118,10 +118,10 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
obj.geometry.save() obj.geometry.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(obj.created) obj.log.add(obj.created)
@ -142,7 +142,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
""" """
with transaction.atomic(): with transaction.atomic():
update_action = UserActionLogEntry.get_edited_action(user, "API update") 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 # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
@ -150,17 +150,17 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
obj.is_cef = properties["is_cef"] obj.is_cef = properties["is_cef"]
obj.is_coherence_keeping = properties["is_coherence_keeping"] obj.is_coherence_keeping = properties["is_coherence_keeping"]
obj.modified = update_action 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.geometry.modified = update_action
obj = self.set_intervention(obj, properties["intervention"], user) obj = self.set_intervention(obj, properties["intervention"], user)
obj.geometry.save() obj.geometry.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(update_action) obj.log.add(update_action)

View File

@ -24,30 +24,30 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
DeductableAPISerializerV1Mixin): DeductableAPISerializerV1Mixin):
model = EcoAccount 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"] = entry.deductable_surface
self.properties_data["deductable_surface_available"] = entry.deductable_surface - entry.get_deductions_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["responsible"] = self._responsible_to_json(entry.responsible)
self.properties_data["legal"] = self.legal_to_json(entry.legal) 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["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["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["actions"] = self._compensation_actions_to_json(entry.actions.all())
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.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["deductions"] = self._deductions_to_json(entry.deductions.all())
def legal_to_json(self, legal: Legal): def _legal_to_json(self, legal: Legal):
return { return {
"agreement_date": legal.registration_date, "agreement_date": legal.registration_date,
} }
def responsible_to_json(self, responsible: Responsibility): def _responsible_to_json(self, responsible: Responsibility):
return { 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, "conservation_file_number": responsible.conservation_file_number,
"handler": responsible.handler, "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 """ Sets the responsible data contents to the provided responsibility_data dict
Args: Args:
@ -59,7 +59,7 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
""" """
if responsibility_data is None: if responsibility_data is None:
return obj return obj
obj.responsible.conservation_office = self.konova_code_from_json( obj.responsible.conservation_office = self._konova_code_from_json(
responsibility_data["conservation_office"], responsibility_data["conservation_office"],
CODELIST_CONSERVATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID,
) )
@ -67,11 +67,11 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
obj.responsible.handler = responsibility_data["handler"] obj.responsible.handler = responsibility_data["handler"]
return obj 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) obj.legal.registration_date = legal_data.get("agreement_date", None)
return obj 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 """ Initializes all needed objects from the json_model data
Does not persist data to the DB! 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_action = UserActionLogEntry.get_created_action(user, comment="API Import")
# Create geometry # Create geometry
json_geom = self.create_geometry_from_json(json_model) json_geom = self._create_geometry_from_json(json_model)
geometry = Geometry() geometry = Geometry()
geometry.geom = json_geom geometry.geom = json_geom
geometry.created = create_action geometry.created = create_action
@ -110,7 +110,7 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
created_id (str): The id of the newly created EcoAccount entry created_id (str): The id of the newly created EcoAccount entry
""" """
with transaction.atomic(): with transaction.atomic():
obj = self.initialize_objects(json_model, user) obj = self._initialize_objects(json_model, user)
# Fill in data to objects # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
@ -124,18 +124,18 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
if obj.deductable_surface < 0: if obj.deductable_surface < 0:
raise ValueError("Deductable surface (m²) must be greater or equal 0") raise ValueError("Deductable surface (m²) must be greater or equal 0")
obj = self.set_responsibility(obj, properties["responsible"]) obj = self._set_responsibility(obj, properties["responsible"])
obj = self.set_legal(obj, properties["legal"]) obj = self._set_legal(obj, properties["legal"])
obj.geometry.save() obj.geometry.save()
obj.responsible.save() obj.responsible.save()
obj.legal.save() obj.legal.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(obj.created) obj.log.add(obj.created)
obj.users.add(user) obj.users.add(user)
@ -157,27 +157,27 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
""" """
with transaction.atomic(): with transaction.atomic():
update_action = UserActionLogEntry.get_edited_action(user, "API update") 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 # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
obj.title = properties["title"] obj.title = properties["title"]
obj.deductable_surface = float(properties["deductable_surface"]) obj.deductable_surface = float(properties["deductable_surface"])
obj.modified = update_action 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.geometry.modified = update_action
obj = self.set_responsibility(obj, properties["responsible"]) obj = self._set_responsibility(obj, properties["responsible"])
obj = self.set_legal(obj, properties["legal"]) obj = self._set_legal(obj, properties["legal"])
obj.geometry.save() obj.geometry.save()
obj.responsible.save() obj.responsible.save()
obj.legal.save() obj.legal.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(update_action) obj.log.add(update_action)

View File

@ -20,21 +20,21 @@ from user.models import UserActionLogEntry
class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin): class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin):
model = Ema model = Ema
def extend_properties_data(self, entry): def _extend_properties_data(self, entry):
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible) 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["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["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["actions"] = self._compensation_actions_to_json(entry.actions.all())
self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.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 { 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, "conservation_file_number": responsible.conservation_file_number,
"handler": responsible.handler, "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 """ Sets the responsible data contents to the provided responsibility_data dict
Args: Args:
@ -46,7 +46,7 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe
""" """
if responsibility_data is None: if responsibility_data is None:
return obj return obj
obj.responsible.conservation_office = self.konova_code_from_json( obj.responsible.conservation_office = self._konova_code_from_json(
responsibility_data["conservation_office"], responsibility_data["conservation_office"],
CODELIST_CONSERVATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID,
) )
@ -54,7 +54,7 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe
obj.responsible.handler = responsibility_data["handler"] obj.responsible.handler = responsibility_data["handler"]
return obj 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 """ Initializes all needed objects from the json_model data
Does not persist data to the DB! 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_action = UserActionLogEntry.get_created_action(user, comment="API Import")
# Create geometry # Create geometry
json_geom = self.create_geometry_from_json(json_model) json_geom = self._create_geometry_from_json(json_model)
geometry = Geometry() geometry = Geometry()
geometry.geom = json_geom geometry.geom = json_geom
geometry.created = create_action geometry.created = create_action
@ -92,22 +92,22 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe
created_id (str): The id of the newly created Ema entry created_id (str): The id of the newly created Ema entry
""" """
with transaction.atomic(): with transaction.atomic():
obj = self.initialize_objects(json_model, user) obj = self._initialize_objects(json_model, user)
# Fill in data to objects # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
obj.identifier = obj.generate_new_identifier() obj.identifier = obj.generate_new_identifier()
obj.title = properties["title"] obj.title = properties["title"]
obj = self.set_responsibility(obj, properties["responsible"]) obj = self._set_responsibility(obj, properties["responsible"])
obj.geometry.save() obj.geometry.save()
obj.responsible.save() obj.responsible.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(obj.created) obj.log.add(obj.created)
obj.users.add(user) obj.users.add(user)
@ -129,24 +129,24 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe
""" """
with transaction.atomic(): with transaction.atomic():
update_action = UserActionLogEntry.get_edited_action(user, "API update") 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 # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
obj.title = properties["title"] obj.title = properties["title"]
obj.modified = update_action 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.geometry.modified = update_action
obj = self.set_responsibility(obj, properties["responsible"]) obj = self._set_responsibility(obj, properties["responsible"])
obj.geometry.save() obj.geometry.save()
obj.responsible.save() obj.responsible.save()
obj.save() obj.save()
obj = self.set_compensation_actions(obj, properties["actions"]) 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["before_states"], obj.before_states)
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states) obj = self._set_compensation_states(obj, properties["after_states"], obj.after_states)
obj = self.set_deadlines(obj, properties["deadlines"]) obj = self._set_deadlines(obj, properties["deadlines"])
obj.log.add(update_action) obj.log.add(update_action)

View File

@ -23,14 +23,14 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
DeductableAPISerializerV1Mixin): DeductableAPISerializerV1Mixin):
model = Intervention model = Intervention
def compensations_to_json(self, qs: QuerySet): def _compensations_to_json(self, qs: QuerySet):
return list( return list(
qs.values( qs.values(
"id", "identifier", "title" "id", "identifier", "title"
) )
) )
def payments_to_json(self, qs: QuerySet): def _payments_to_json(self, qs: QuerySet):
""" Serializes payments into json """ Serializes payments into json
Args: Args:
@ -41,14 +41,14 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
""" """
return list(qs.values("amount", "due_on", "comment")) return list(qs.values("amount", "due_on", "comment"))
def extend_properties_data(self, entry): def _extend_properties_data(self, entry):
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible) self.properties_data["responsible"] = self._responsible_to_json(entry.responsible)
self.properties_data["legal"] = self.legal_to_json(entry.legal) self.properties_data["legal"] = self._legal_to_json(entry.legal)
self.properties_data["compensations"] = self.compensations_to_json(entry.compensations.all()) 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["payments"] = self._payments_to_json(entry.payments.all())
self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.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 """ Initializes all needed objects from the json_model data
Does not persist data to the DB! 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_action = UserActionLogEntry.get_created_action(user, comment="API Import")
# Create geometry # Create geometry
json_geom = self.create_geometry_from_json(json_model) json_geom = self._create_geometry_from_json(json_model)
geometry = Geometry() geometry = Geometry()
geometry.geom = json_geom geometry.geom = json_geom
geometry.created = create_action geometry.created = create_action
@ -78,7 +78,7 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
obj.responsible = resp obj.responsible = resp
return obj 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 """ 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 created_id (str): The id of the newly created Intervention entry
""" """
with transaction.atomic(): with transaction.atomic():
obj = self.initialize_objects(json_model, user) obj = self._initialize_objects(json_model, user)
# Fill in data to objects # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
obj.identifier = obj.generate_new_identifier() obj.identifier = obj.generate_new_identifier()
obj.title = properties["title"] obj.title = properties["title"]
self.set_responsibility(obj, properties["responsible"]) self._set_responsibility(obj, properties["responsible"])
self.set_legal(obj, properties["legal"]) self._set_legal(obj, properties["legal"])
obj.responsible.save() obj.responsible.save()
obj.geometry.save() obj.geometry.save()
@ -177,15 +177,15 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
""" """
with transaction.atomic(): with transaction.atomic():
update_action = UserActionLogEntry.get_edited_action(user, "API update") 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 # Fill in data to objects
properties = json_model["properties"] properties = json_model["properties"]
obj.title = properties["title"] obj.title = properties["title"]
self.set_responsibility(obj, properties.get("responsible", None)) self._set_responsibility(obj, properties.get("responsible", None))
self.set_legal(obj, properties.get("legal", None)) self._set_legal(obj, properties.get("legal", None))
self.set_payments(obj, properties.get("payments", None)) 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
obj.responsible.save() obj.responsible.save()

View File

@ -21,7 +21,7 @@ from konova.utils.message_templates import DATA_UNSHARED
class AbstractModelAPISerializerV1(AbstractModelAPISerializer): 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 """ Adds the basic data, which all elements hold
Args: Args:
@ -36,14 +36,14 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
"id": entry.id, "id": entry.id,
"identifier": entry.identifier, "identifier": entry.identifier,
"title": entry.title, "title": entry.title,
"created_on": self.created_on_to_json(entry), "created_on": self._created_on_to_json(entry),
"modified_on": self.modified_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 geo_json["properties"] = self.properties_data
return geo_json 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 """ Serializes KonovaCode model into json
Args: Args:
@ -60,7 +60,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
"short_name": konova_code.short_name, "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 """ Returns a konova code instance
Args: Args:
@ -78,7 +78,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
) )
return code return code
def created_on_to_json(self, entry): def _created_on_to_json(self, entry):
""" Serializes the created_on into json """ Serializes the created_on into json
Args: Args:
@ -89,7 +89,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
""" """
return entry.created.timestamp if entry.created is not None else None 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
Args: Args:
@ -112,7 +112,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
Returns: Returns:
""" """
entry = self.get_obj_from_db(id, user) entry = self._get_obj_from_db(id, user)
is_shared = entry.is_shared_with(user) is_shared = entry.is_shared_with(user)
if not is_shared: if not is_shared:
raise PermissionError(DATA_UNSHARED) raise PermissionError(DATA_UNSHARED)
@ -128,7 +128,7 @@ class DeductableAPISerializerV1Mixin:
class Meta: class Meta:
abstract = True abstract = True
def deductions_to_json(self, qs: QuerySet): def _deductions_to_json(self, qs: QuerySet):
""" Serializes eco account deductions into json """ Serializes eco account deductions into json
Args: Args:
@ -160,7 +160,7 @@ class ResponsibilityAPISerializerV1Mixin:
class Meta: class Meta:
abstract = True abstract = True
def responsible_to_json(self, responsible: Responsibility): def _responsible_to_json(self, responsible: Responsibility):
""" Serializes Responsibility model into json """ Serializes Responsibility model into json
Args: Args:
@ -170,14 +170,14 @@ class ResponsibilityAPISerializerV1Mixin:
serialized_json (dict) serialized_json (dict)
""" """
return { 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, "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, "conservation_file_number": responsible.conservation_file_number,
"handler": responsible.handler, "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 """ Sets the responsible data contents to the provided responsibility_data dict
Args: Args:
@ -189,12 +189,12 @@ class ResponsibilityAPISerializerV1Mixin:
""" """
if responsibility_data is None: if responsibility_data is None:
return obj 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
) )
obj.responsible.registration_file_number = responsibility_data["registration_file_number"] 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"], responsibility_data["conservation_office"],
CODELIST_CONSERVATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID,
) )
@ -207,7 +207,7 @@ class LegalAPISerializerV1Mixin:
class Meta: class Meta:
abstract = True abstract = True
def legal_to_json(self, legal: Legal): def _legal_to_json(self, legal: Legal):
""" Serializes Legal model into json """ Serializes Legal model into json
Args: Args:
@ -219,11 +219,11 @@ class LegalAPISerializerV1Mixin:
return { return {
"registration_date": legal.registration_date, "registration_date": legal.registration_date,
"binding_date": legal.binding_date, "binding_date": legal.binding_date,
"process_type": self.konova_code_to_json(legal.process_type), "process_type": self._konova_code_to_json(legal.process_type),
"laws": [self.konova_code_to_json(law) for law in legal.laws.all()], "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 """ Sets the legal data contents to the provided legal_data dict
Args: Args:
@ -237,11 +237,11 @@ class LegalAPISerializerV1Mixin:
return obj return obj
obj.legal.registration_date = legal_data.get("registration_date", None) obj.legal.registration_date = legal_data.get("registration_date", None)
obj.legal.binding_date = legal_data.get("binding_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), legal_data.get("process_type", None),
CODELIST_PROCESS_TYPE_ID, 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) obj.legal.laws.set(laws)
return obj return obj
@ -250,7 +250,7 @@ class AbstractCompensationAPISerializerV1Mixin:
class Meta: class Meta:
abstract = True 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 """ Sets the linked deadline data according to the given deadline_data
@ -293,7 +293,7 @@ class AbstractCompensationAPISerializerV1Mixin:
obj.deadlines.set(deadlines) obj.deadlines.set(deadlines)
return obj 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 """ Sets the linked compensation state data according to the given states_data
@ -327,7 +327,7 @@ class AbstractCompensationAPISerializerV1Mixin:
else: else:
# Create and add id to list # Create and add id to list
new_state = CompensationState.objects.create( 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 surface=surface
) )
states.append(new_state.id) states.append(new_state.id)
@ -335,7 +335,7 @@ class AbstractCompensationAPISerializerV1Mixin:
states_manager.set(states) states_manager.set(states)
return obj 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 """ Sets the linked compensation action data according to the given actions_data
@ -374,7 +374,7 @@ class AbstractCompensationAPISerializerV1Mixin:
else: else:
# Create and add id to list # Create and add id to list
new_action = CompensationAction.objects.create( 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, amount=amount,
unit=unit, unit=unit,
comment=comment, comment=comment,
@ -383,7 +383,7 @@ class AbstractCompensationAPISerializerV1Mixin:
obj.actions.set(actions) obj.actions.set(actions)
return obj return obj
def compensation_state_to_json(self, qs: QuerySet): def _compensation_state_to_json(self, qs: QuerySet):
""" Serializes compensation states into json """ Serializes compensation states into json
Args: Args:
@ -394,13 +394,13 @@ class AbstractCompensationAPISerializerV1Mixin:
""" """
return [ return [
{ {
"biotope": self.konova_code_to_json(entry.biotope_type), "biotope": self._konova_code_to_json(entry.biotope_type),
"surface": entry.surface, "surface": entry.surface,
} }
for entry in qs for entry in qs
] ]
def compensation_actions_to_json(self, qs: QuerySet): def _compensation_actions_to_json(self, qs: QuerySet):
""" Serializes CompensationActions into json """ Serializes CompensationActions into json
Args: Args:
@ -411,7 +411,7 @@ class AbstractCompensationAPISerializerV1Mixin:
""" """
return [ return [
{ {
"action": self.konova_code_to_json(entry.action_type), "action": self._konova_code_to_json(entry.action_type),
"amount": entry.amount, "amount": entry.amount,
"unit": entry.unit, "unit": entry.unit,
"comment": entry.comment, "comment": entry.comment,
@ -419,7 +419,7 @@ class AbstractCompensationAPISerializerV1Mixin:
for entry in qs for entry in qs
] ]
def deadlines_to_json(self, qs: QuerySet): def _deadlines_to_json(self, qs: QuerySet):
""" Serializes deadlines into json """ Serializes deadlines into json
Args: Args: