Refactoring #43

Merged
mpeltriaux merged 13 commits from Refactoring into master 2021-11-16 14:24:11 +01:00
4 changed files with 79 additions and 44 deletions
Showing only changes of commit d8f0db6bd6 - Show all commits

View File

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

View File

@ -9,7 +9,7 @@ import shutil
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator 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.db.models import Sum, QuerySet
from django.utils.translation import gettext_lazy as _ 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, \ from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \
generate_document_file_upload_path generate_document_file_upload_path
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE 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): class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin):
@ -35,7 +35,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
) )
legal = models.OneToOneField( legal = models.OneToOneField(
Legal, "intervention.Legal",
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
null=True, null=True,
blank=True, blank=True,
@ -164,6 +164,40 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
) )
return docs 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): class EcoAccountDocument(AbstractDocument):
""" """
@ -234,7 +268,7 @@ class EcoAccountDeduction(BaseResource):
] ]
) )
intervention = models.ForeignKey( intervention = models.ForeignKey(
Intervention, "intervention.Intervention",
on_delete=models.CASCADE, on_delete=models.CASCADE,
null=True, null=True,
blank=True, blank=True,

View File

@ -290,14 +290,12 @@ class NewDeductionModalForm(BaseModalForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.form_title = _("New Deduction") self.form_title = _("New Deduction")
self.form_caption = _("Enter the information for a new deduction from a chosen eco-account") 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 # Check for Intervention or EcoAccount
if isinstance(self.instance, Intervention): if isinstance(self.instance, Intervention):
# Form has been called with a given intervention # Form has been called with a given intervention
self.initialize_form_field("intervention", self.instance) self.initialize_form_field("intervention", self.instance)
self.disable_form_field("intervention") self.disable_form_field("intervention")
self.is_intervention_initially = True
elif isinstance(self.instance, EcoAccount): elif isinstance(self.instance, EcoAccount):
# Form has been called with a given account --> make it initial in the form and read-only # Form has been called with a given account --> make it initial in the form and read-only
self.initialize_form_field("account", self.instance) self.initialize_form_field("account", self.instance)
@ -314,10 +312,7 @@ class NewDeductionModalForm(BaseModalForm):
is_valid (bool) is_valid (bool)
""" """
super_result = super().is_valid() super_result = super().is_valid()
if self.is_intervention_initially:
acc = self.cleaned_data["account"] acc = self.cleaned_data["account"]
else:
acc = self.instance
if not acc.recorded: if not acc.recorded:
self.add_error( self.add_error(
@ -331,7 +326,7 @@ class NewDeductionModalForm(BaseModalForm):
sum_surface_deductions = acc.get_deductions_surface() sum_surface_deductions = acc.get_deductions_surface()
rest_surface = deductable_surface - sum_surface_deductions rest_surface = deductable_surface - sum_surface_deductions
form_surface = float(self.cleaned_data["surface"]) 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: if not is_valid_surface:
self.add_error( self.add_error(
"surface", "surface",
@ -344,35 +339,7 @@ class NewDeductionModalForm(BaseModalForm):
return is_valid_surface and super_result return is_valid_surface and super_result
def save(self): def save(self):
with transaction.atomic(): deduction = self.instance.add_deduction(self)
# 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,
)
return deduction return deduction

View File

@ -12,6 +12,7 @@ from django.db import models, transaction
from django.db.models import QuerySet from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccountDeduction
from intervention.managers import InterventionManager from intervention.managers import InterventionManager
from intervention.models.legal import Legal from intervention.models.legal import Legal
from intervention.models.responsibility import Responsibility from intervention.models.responsibility import Responsibility
@ -213,7 +214,7 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
""" Adds a new revocation to the intervention """ Adds a new revocation to the intervention
Args: Args:
form (NewPaymentForm): The form holding the data form (NewRevocationModalForm): The form holding the data
Returns: Returns:
@ -249,6 +250,40 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
) )
return revocation 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): class InterventionDocument(AbstractDocument):
""" """