#31 API PUT Compensation
* adds support for PUT compensation (Update) * improves updating of related objects * adds missing payment PUT support for intervention API
This commit is contained in:
@@ -78,16 +78,21 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
||||
Returns:
|
||||
obj (Compensation)
|
||||
"""
|
||||
if obj.intervention.id == intervention_id:
|
||||
# Nothing to do here
|
||||
return obj
|
||||
|
||||
intervention = Intervention.objects.get(
|
||||
id=intervention_id,
|
||||
)
|
||||
is_shared = intervention.is_shared_with(user)
|
||||
|
||||
if not is_shared:
|
||||
raise PermissionError("Intervention not shared with user")
|
||||
|
||||
obj.intervention = intervention
|
||||
return obj
|
||||
|
||||
|
||||
def create_model_from_json(self, json_model, user):
|
||||
""" Creates a new entry for the model based on the contents of json_model
|
||||
|
||||
@@ -123,6 +128,23 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
||||
|
||||
return obj.id
|
||||
|
||||
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
|
||||
|
||||
Args:
|
||||
id (str): The object's id
|
||||
user (User): The API user
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return self.model.objects.get(
|
||||
id=id,
|
||||
intervention__users__in=[user]
|
||||
)
|
||||
|
||||
def update_model_from_json(self, id, json_model, user):
|
||||
""" Updates an entry for the model based on the contents of json_model
|
||||
|
||||
@@ -135,21 +157,28 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
||||
created_id (str): The id of the newly created Compensation entry
|
||||
"""
|
||||
with transaction.atomic():
|
||||
update_action = UserActionLogEntry.get_edited_action(user, "API update")
|
||||
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["responsible"])
|
||||
self.set_legal(obj, properties["legal"])
|
||||
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.modified = update_action
|
||||
obj = self.set_intervention(obj, properties["intervention"], user)
|
||||
|
||||
obj.responsible.save()
|
||||
obj.geometry.save()
|
||||
obj.legal.save()
|
||||
obj.save()
|
||||
|
||||
obj.users.add(user)
|
||||
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)
|
||||
|
||||
celery_update_parcels.delay(obj.geometry.id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user