#86 Edit deductions
* adds support for editing deductions * adds tests * improves major base test logic
This commit is contained in:
@@ -7,7 +7,7 @@ Created on: 27.09.21
|
||||
"""
|
||||
from dal import autocomplete
|
||||
|
||||
from konova.utils.message_templates import DEDUCTION_ADDED, REVOCATION_ADDED, DEDUCTION_REMOVED
|
||||
from konova.utils.message_templates import DEDUCTION_ADDED, REVOCATION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED
|
||||
from user.models import User, UserActionLogEntry
|
||||
from django.db import transaction
|
||||
from django import forms
|
||||
@@ -349,6 +349,21 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
def _get_available_surface(self, acc):
|
||||
""" Calculates how much available surface is left on the account
|
||||
|
||||
Args:
|
||||
acc (EcoAccount):
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Calculate valid surface
|
||||
deductable_surface = acc.deductable_surface
|
||||
sum_surface_deductions = acc.get_deductions_surface()
|
||||
rest_surface = deductable_surface - sum_surface_deductions
|
||||
return rest_surface
|
||||
|
||||
def is_valid(self):
|
||||
""" Custom validity check
|
||||
|
||||
@@ -367,10 +382,7 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
)
|
||||
return False
|
||||
|
||||
# Calculate valid surface
|
||||
deductable_surface = acc.deductable_surface
|
||||
sum_surface_deductions = acc.get_deductions_surface()
|
||||
rest_surface = deductable_surface - sum_surface_deductions
|
||||
rest_surface = self._get_available_surface(acc)
|
||||
form_surface = float(self.cleaned_data["surface"])
|
||||
is_valid_surface = form_surface <= rest_surface
|
||||
if not is_valid_surface:
|
||||
@@ -407,6 +419,57 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
return deduction
|
||||
|
||||
|
||||
class EditEcoAccountDeductionModalForm(NewDeductionModalForm):
|
||||
deduction = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.deduction = kwargs.pop("deduction", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
form_data = {
|
||||
"account": self.deduction.account,
|
||||
"intervention": self.deduction.intervention,
|
||||
"surface": self.deduction.surface,
|
||||
}
|
||||
self.load_initial_data(form_data)
|
||||
|
||||
def _get_available_surface(self, acc):
|
||||
rest_surface = super()._get_available_surface(acc)
|
||||
# Increase available surface by the currently deducted surface, so we can 'deduct' the same amount again or
|
||||
# increase the surface only a little, which will still be valid.
|
||||
# Example: 200 m² left, 500 m² deducted. Entering 700 m² would fail if we would not add the 500 m² to the available
|
||||
# surface again.
|
||||
rest_surface += self.deduction.surface
|
||||
return rest_surface
|
||||
|
||||
def save(self):
|
||||
deduction = self.deduction
|
||||
form_account = self.cleaned_data.get("account", None)
|
||||
form_intervention = self.cleaned_data.get("intervention", None)
|
||||
current_account = deduction.account
|
||||
current_intervention = deduction.intervention
|
||||
|
||||
|
||||
# If account or intervention has been changed, we put that change in the logs just as if the deduction has
|
||||
# been removed for this entry. Act as if the deduction is newly created for the new entries
|
||||
if current_account != form_account:
|
||||
current_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
|
||||
form_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_ADDED)
|
||||
else:
|
||||
current_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
|
||||
|
||||
if current_intervention != form_intervention:
|
||||
current_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
|
||||
form_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_ADDED)
|
||||
else:
|
||||
current_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
|
||||
|
||||
deduction.account = form_account
|
||||
deduction.intervention = self.cleaned_data.get("intervention", None)
|
||||
deduction.surface = self.cleaned_data.get("surface", None)
|
||||
deduction.save()
|
||||
return deduction
|
||||
|
||||
|
||||
class RemoveEcoAccountDeductionModalForm(RemoveModalForm):
|
||||
""" Removing modal form for EcoAccountDeduction
|
||||
|
||||
|
||||
Reference in New Issue
Block a user