#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:
2022-01-28 09:44:16 +01:00
parent 3d446883c6
commit 27c1de2c53
10 changed files with 140 additions and 133 deletions

View File

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