Refactoring

* moves adding of deduction into Intervention and EcoAccount model
* hardens against circular import issues
This commit is contained in:
2021-11-16 12:43:13 +01:00
parent dafa53451f
commit 648bfba1bf
4 changed files with 79 additions and 44 deletions

View File

@@ -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,

View File

@@ -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,