From e501512a6356168e6914bdd4939cef9ecf30c280 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Tue, 16 Nov 2021 13:45:05 +0100 Subject: [PATCH] Refactoring * moves add_state from compensation to AbstractCompensation --- compensation/models/compensation.py | 57 ++++++++++++++--------------- compensation/models/payment.py | 1 - 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/compensation/models/compensation.py b/compensation/models/compensation.py index 3f00418a..7808187d 100644 --- a/compensation/models/compensation.py +++ b/compensation/models/compensation.py @@ -101,6 +101,34 @@ class AbstractCompensation(BaseObject): self.actions.add(comp_action) return comp_action + def add_state(self, form, is_before_state: bool) -> CompensationState: + """ Adds a new compensation state to the compensation + + Args: + form (NewStateModalForm): The form, holding all relevant data + is_before_state (bool): Whether this is a new before_state or after_state + + Returns: + + """ + form_data = form.cleaned_data + user = form.user + with transaction.atomic(): + user_action = UserActionLogEntry.get_edited_action(user, _("Added state")) + self.log.add(user_action) + self.modified = user_action + self.save() + + state = CompensationState.objects.create( + biotope_type=form_data["biotope_type"], + surface=form_data["surface"], + ) + if is_before_state: + self.before_states.add(state) + else: + self.after_states.add(state) + return state + def get_surface_after_states(self) -> float: """ Calculates the compensation's/account's surface @@ -243,35 +271,6 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin): ) return docs - def add_state(self, form, is_before_state: bool) -> CompensationState: - """ Adds a new compensation state to the compensation - - Args: - form (NewStateModalForm): The form, holding all relevant data - is_before_state (bool): Whether this is a new before_state or after_state - - Returns: - - """ - form_data = form.cleaned_data - user = form.user - with transaction.atomic(): - user_action = UserActionLogEntry.get_edited_action(user, _("Added state")) - self.log.add(user_action) - self.modified = user_action - self.save() - - state = CompensationState.objects.create( - biotope_type=form_data["biotope_type"], - surface=form_data["surface"], - ) - if is_before_state: - self.before_states.add(state) - else: - self.after_states.add(state) - return state - - class CompensationDocument(AbstractDocument): """ Specializes document upload for revocations with certain path diff --git a/compensation/models/payment.py b/compensation/models/payment.py index a630e36d..ec56910d 100644 --- a/compensation/models/payment.py +++ b/compensation/models/payment.py @@ -35,4 +35,3 @@ class Payment(BaseResource): ordering = [ "-amount", ] -