Refactoring

* moves adding of deduction into Intervention and EcoAccount model
* hardens against circular import issues
pull/43/head
mpeltriaux 3 years ago
parent 9b531bc09e
commit d8f0db6bd6

@ -15,7 +15,6 @@ from django.utils.translation import gettext_lazy as _
from compensation.managers import CompensationManager
from compensation.models import CompensationState, CompensationAction
from compensation.utils.quality import CompensationQualityChecker
from intervention.models import Responsibility, Intervention
from konova.models import BaseObject, AbstractDocument, Geometry, Deadline, generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
from user.models import UserActionLogEntry, UserAction
@ -28,7 +27,7 @@ class AbstractCompensation(BaseObject):
"""
responsible = models.OneToOneField(
Responsibility,
"intervention.Responsibility",
on_delete=models.SET_NULL,
null=True,
blank=True,
@ -186,7 +185,7 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin):
Regular compensation, linked to an intervention
"""
intervention = models.ForeignKey(
Intervention,
"intervention.Intervention",
on_delete=models.CASCADE,
null=True,
blank=True,

@ -9,7 +9,7 @@ import shutil
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
from django.db import models, transaction
from django.db.models import Sum, QuerySet
from django.utils.translation import gettext_lazy as _
@ -19,7 +19,7 @@ from compensation.utils.quality import EcoAccountQualityChecker
from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \
generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
from intervention.models import Intervention, Legal
from user.models import UserActionLogEntry, UserAction
class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin):
@ -35,7 +35,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
)
legal = models.OneToOneField(
Legal,
"intervention.Legal",
on_delete=models.SET_NULL,
null=True,
blank=True,
@ -164,6 +164,40 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
)
return docs
def add_deduction(self, form):
""" Adds a new deduction to the intervention
Args:
form (NewDeductionModalForm): The form holding the data
Returns:
"""
form_data = form.cleaned_data
user = form.user
with transaction.atomic():
# Create log entry
user_action_edit = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED
)
user_action_create = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED
)
self.log.add(user_action_edit)
self.modified = user_action_edit
self.save()
deduction = EcoAccountDeduction.objects.create(
intervention=form_data["intervention"],
account=self,
surface=form_data["surface"],
created=user_action_create,
)
return deduction
class EcoAccountDocument(AbstractDocument):
"""
@ -234,7 +268,7 @@ class EcoAccountDeduction(BaseResource):
]
)
intervention = models.ForeignKey(
Intervention,
"intervention.Intervention",
on_delete=models.CASCADE,
null=True,
blank=True,

@ -290,14 +290,12 @@ class NewDeductionModalForm(BaseModalForm):
super().__init__(*args, **kwargs)
self.form_title = _("New Deduction")
self.form_caption = _("Enter the information for a new deduction from a chosen eco-account")
self.is_intervention_initially = False
# Check for Intervention or EcoAccount
if isinstance(self.instance, Intervention):
# Form has been called with a given intervention
self.initialize_form_field("intervention", self.instance)
self.disable_form_field("intervention")
self.is_intervention_initially = True
elif isinstance(self.instance, EcoAccount):
# Form has been called with a given account --> make it initial in the form and read-only
self.initialize_form_field("account", self.instance)
@ -314,10 +312,7 @@ class NewDeductionModalForm(BaseModalForm):
is_valid (bool)
"""
super_result = super().is_valid()
if self.is_intervention_initially:
acc = self.cleaned_data["account"]
else:
acc = self.instance
acc = self.cleaned_data["account"]
if not acc.recorded:
self.add_error(
@ -331,7 +326,7 @@ class NewDeductionModalForm(BaseModalForm):
sum_surface_deductions = acc.get_deductions_surface()
rest_surface = deductable_surface - sum_surface_deductions
form_surface = float(self.cleaned_data["surface"])
is_valid_surface = form_surface < rest_surface
is_valid_surface = form_surface <= rest_surface
if not is_valid_surface:
self.add_error(
"surface",
@ -344,35 +339,7 @@ class NewDeductionModalForm(BaseModalForm):
return is_valid_surface and super_result
def save(self):
with transaction.atomic():
# Create log entry
user_action_edit = UserActionLogEntry.objects.create(
user=self.user,
action=UserAction.EDITED
)
user_action_create = UserActionLogEntry.objects.create(
user=self.user,
action=UserAction.CREATED
)
self.instance.log.add(user_action_edit)
self.instance.modified = user_action_edit
self.instance.save()
# Create deductions depending on Intervention or EcoAccount as the initial instance
if self.is_intervention_initially:
deduction = EcoAccountDeduction.objects.create(
intervention=self.instance,
account=self.cleaned_data["account"],
surface=self.cleaned_data["surface"],
created=user_action_create,
)
else:
deduction = EcoAccountDeduction.objects.create(
intervention=self.cleaned_data["intervention"],
account=self.instance,
surface=self.cleaned_data["surface"],
created=user_action_create,
)
deduction = self.instance.add_deduction(self)
return deduction

@ -12,6 +12,7 @@ from django.db import models, transaction
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccountDeduction
from intervention.managers import InterventionManager
from intervention.models.legal import Legal
from intervention.models.responsibility import Responsibility
@ -213,7 +214,7 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
""" Adds a new revocation to the intervention
Args:
form (NewPaymentForm): The form holding the data
form (NewRevocationModalForm): The form holding the data
Returns:
@ -249,6 +250,40 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
)
return revocation
def add_deduction(self, form):
""" Adds a new deduction to the intervention
Args:
form (NewDeductionModalForm): The form holding the data
Returns:
"""
form_data = form.cleaned_data
user = form.user
with transaction.atomic():
# Create log entry
user_action_edit = UserActionLogEntry.objects.create(
user=user,
action=UserAction.EDITED
)
user_action_create = UserActionLogEntry.objects.create(
user=user,
action=UserAction.CREATED
)
self.log.add(user_action_edit)
self.modified = user_action_edit
self.save()
deduction = EcoAccountDeduction.objects.create(
intervention=self,
account=form_data["account"],
surface=form_data["surface"],
created=user_action_create,
)
return deduction
class InterventionDocument(AbstractDocument):
"""

Loading…
Cancel
Save