Refactoring
* renames model ResponsibilityData into Responsibility * renames model LegalData into Legal * moves form->object saving logic into model * refactors NewDocumentForm into special types for intervention, compensation, eco account and ema *
This commit is contained in:
+107
-5
@@ -11,19 +11,20 @@ from django.contrib.auth.models import User
|
||||
from django.contrib.gis.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db import transaction
|
||||
from django.db.models import Sum, QuerySet
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from codelist.models import KonovaCode
|
||||
from intervention.models import Intervention, Responsibility
|
||||
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID
|
||||
from compensation.managers import CompensationStateManager, EcoAccountDeductionManager, CompensationActionManager, \
|
||||
EcoAccountManager, CompensationManager
|
||||
from compensation.utils.quality import CompensationQualityChecker, EcoAccountQualityChecker
|
||||
from intervention.models import Intervention, ResponsibilityData, LegalData
|
||||
from konova.models import BaseObject, BaseResource, Geometry, UuidModel, AbstractDocument, \
|
||||
generate_document_file_upload_path, RecordableObject, ShareableObject
|
||||
generate_document_file_upload_path, RecordableObject, ShareableObject, Deadline
|
||||
from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE
|
||||
from user.models import UserActionLogEntry
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
class Payment(BaseResource):
|
||||
@@ -133,7 +134,7 @@ class AbstractCompensation(BaseObject):
|
||||
|
||||
"""
|
||||
responsible = models.OneToOneField(
|
||||
ResponsibilityData,
|
||||
Responsibility,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
@@ -151,6 +152,73 @@ class AbstractCompensation(BaseObject):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def add_new_deadline(self, form) -> Deadline:
|
||||
""" Adds a new deadline to the abstract compensation
|
||||
|
||||
Args:
|
||||
form (NewDeadlineModalForm): The form holding all relevant data
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
form_data = form.cleaned_data
|
||||
user = form.user
|
||||
with transaction.atomic():
|
||||
created_action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CREATED
|
||||
)
|
||||
deadline = Deadline.objects.create(
|
||||
type=form_data["type"],
|
||||
date=form_data["date"],
|
||||
comment=form_data["comment"],
|
||||
created=created_action,
|
||||
)
|
||||
edited_action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added deadline")
|
||||
)
|
||||
self.modified = edited_action
|
||||
self.save()
|
||||
self.log.add(edited_action)
|
||||
self.deadlines.add(deadline)
|
||||
return deadline
|
||||
|
||||
def add_new_action(self, form) -> CompensationAction:
|
||||
""" Adds a new action to the compensation
|
||||
|
||||
Args:
|
||||
form (NewActionModalForm): The form holding all relevant data
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
form_data = form.cleaned_data
|
||||
user = form.user
|
||||
with transaction.atomic():
|
||||
user_action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
comp_action = CompensationAction.objects.create(
|
||||
action_type=form_data["action_type"],
|
||||
amount=form_data["amount"],
|
||||
unit=form_data["unit"],
|
||||
comment=form_data["comment"],
|
||||
created=user_action,
|
||||
)
|
||||
edited_action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added action"),
|
||||
)
|
||||
self.modified = edited_action
|
||||
self.save()
|
||||
self.log.add(edited_action)
|
||||
self.actions.add(comp_action)
|
||||
return comp_action
|
||||
|
||||
def get_surface_after_states(self) -> float:
|
||||
""" Calculates the compensation's/account's surface
|
||||
|
||||
@@ -293,6 +361,38 @@ 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.objects.create(
|
||||
user=user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("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):
|
||||
"""
|
||||
@@ -347,6 +447,7 @@ class EcoAccount(AbstractCompensation, ShareableObject, RecordableObject):
|
||||
An eco account is a kind of 'prepaid' compensation. It can be compared to an account that already has been filled
|
||||
with some kind of currency. From this account one is able to deduct currency for current projects.
|
||||
"""
|
||||
from intervention.models import Legal
|
||||
deductable_surface = models.FloatField(
|
||||
blank=True,
|
||||
null=True,
|
||||
@@ -355,7 +456,7 @@ class EcoAccount(AbstractCompensation, ShareableObject, RecordableObject):
|
||||
)
|
||||
|
||||
legal = models.OneToOneField(
|
||||
LegalData,
|
||||
Legal,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
@@ -537,6 +638,7 @@ class EcoAccountDeduction(BaseResource):
|
||||
"""
|
||||
A deduction object for eco accounts
|
||||
"""
|
||||
from intervention.models import Intervention
|
||||
account = models.ForeignKey(
|
||||
EcoAccount,
|
||||
on_delete=models.SET_NULL,
|
||||
|
||||
Reference in New Issue
Block a user