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:
@@ -16,7 +16,7 @@ from codelist.models import KonovaCode
|
||||
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID
|
||||
from compensation.models import Compensation, EcoAccount
|
||||
from intervention.inputs import GenerateInput
|
||||
from intervention.models import Intervention, ResponsibilityData, LegalData
|
||||
from intervention.models import Intervention, Responsibility, Legal
|
||||
from konova.forms import BaseForm, SimpleGeomForm
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
@@ -371,13 +371,13 @@ class NewEcoAccountForm(AbstractCompensationForm, CompensationResponsibleFormMix
|
||||
# Process the geometry form
|
||||
geometry = geom_form.save(action)
|
||||
|
||||
responsible = ResponsibilityData.objects.create(
|
||||
responsible = Responsibility.objects.create(
|
||||
handler=handler,
|
||||
conservation_file_number=conservation_file_number,
|
||||
conservation_office=conservation_office,
|
||||
)
|
||||
|
||||
legal = LegalData.objects.create(
|
||||
legal = Legal.objects.create(
|
||||
registration_date=registration_date
|
||||
)
|
||||
|
||||
|
||||
@@ -9,19 +9,17 @@ from bootstrap_modal_forms.utils import is_ajax
|
||||
from dal import autocomplete
|
||||
from django import forms
|
||||
from django.contrib import messages
|
||||
from django.db import transaction
|
||||
from django.http import HttpRequest, HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.utils.translation import pgettext_lazy as _con, gettext_lazy as _
|
||||
|
||||
from codelist.models import KonovaCode
|
||||
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_COMPENSATION_ACTION_ID
|
||||
from compensation.models import Payment, CompensationState, UnitChoices, CompensationAction
|
||||
from compensation.models import CompensationDocument, EcoAccountDocument
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import BaseModalForm
|
||||
from konova.models import DeadlineType, Deadline
|
||||
from konova.forms import BaseModalForm, NewDocumentForm
|
||||
from konova.models import DeadlineType
|
||||
from konova.utils.message_templates import FORM_INVALID
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
class NewPaymentForm(BaseModalForm):
|
||||
@@ -99,27 +97,8 @@ class NewPaymentForm(BaseModalForm):
|
||||
return super_valid
|
||||
|
||||
def save(self):
|
||||
with transaction.atomic():
|
||||
created_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
edited_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added payment"),
|
||||
)
|
||||
pay = Payment.objects.create(
|
||||
created=created_action,
|
||||
amount=self.cleaned_data.get("amount", -1),
|
||||
due_on=self.cleaned_data.get("due", None),
|
||||
comment=self.cleaned_data.get("comment", None),
|
||||
intervention=self.intervention,
|
||||
)
|
||||
self.intervention.log.add(edited_action)
|
||||
self.intervention.modified = edited_action
|
||||
self.intervention.save()
|
||||
return pay
|
||||
pay = self.instance.add_payment(self)
|
||||
return pay
|
||||
|
||||
|
||||
class NewStateModalForm(BaseModalForm):
|
||||
@@ -167,24 +146,7 @@ class NewStateModalForm(BaseModalForm):
|
||||
self.form_caption = _("Insert data for the new state")
|
||||
|
||||
def save(self, is_before_state: bool = False):
|
||||
with transaction.atomic():
|
||||
user_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added state")
|
||||
)
|
||||
self.instance.log.add(user_action)
|
||||
self.instance.modified = user_action
|
||||
self.instance.save()
|
||||
|
||||
state = CompensationState.objects.create(
|
||||
biotope_type=self.cleaned_data["biotope_type"],
|
||||
surface=self.cleaned_data["surface"],
|
||||
)
|
||||
if is_before_state:
|
||||
self.instance.before_states.add(state)
|
||||
else:
|
||||
self.instance.after_states.add(state)
|
||||
state = self.instance.add_state(self, is_before_state)
|
||||
return state
|
||||
|
||||
def process_request(self, request: HttpRequest, msg_success: str = _("Object removed"), msg_error: str = FORM_INVALID, redirect_url: str = None):
|
||||
@@ -287,26 +249,7 @@ class NewDeadlineModalForm(BaseModalForm):
|
||||
self.form_caption = _("Insert data for the new deadline")
|
||||
|
||||
def save(self):
|
||||
with transaction.atomic():
|
||||
created_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.CREATED
|
||||
)
|
||||
deadline = Deadline.objects.create(
|
||||
type=self.cleaned_data["type"],
|
||||
date=self.cleaned_data["date"],
|
||||
comment=self.cleaned_data["comment"],
|
||||
created=created_action,
|
||||
)
|
||||
edited_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added deadline")
|
||||
)
|
||||
self.instance.modified = edited_action
|
||||
self.instance.save()
|
||||
self.instance.log.add(edited_action)
|
||||
self.instance.deadlines.add(deadline)
|
||||
deadline = self.instance.add_new_deadline(self)
|
||||
return deadline
|
||||
|
||||
|
||||
@@ -318,6 +261,7 @@ class NewActionModalForm(BaseModalForm):
|
||||
(not in the process logic in Konova, but in the real world).
|
||||
|
||||
"""
|
||||
from compensation.models import UnitChoices
|
||||
action_type = forms.ModelChoiceField(
|
||||
label=_("Action Type"),
|
||||
label_suffix="",
|
||||
@@ -381,25 +325,13 @@ class NewActionModalForm(BaseModalForm):
|
||||
self.form_caption = _("Insert data for the new action")
|
||||
|
||||
def save(self):
|
||||
with transaction.atomic():
|
||||
user_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
comp_action = CompensationAction.objects.create(
|
||||
action_type=self.cleaned_data["action_type"],
|
||||
amount=self.cleaned_data["amount"],
|
||||
unit=self.cleaned_data["unit"],
|
||||
comment=self.cleaned_data["comment"],
|
||||
created=user_action,
|
||||
)
|
||||
edited_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED,
|
||||
comment=_("Added action"),
|
||||
)
|
||||
self.instance.modified = edited_action
|
||||
self.instance.save()
|
||||
self.instance.log.add(edited_action)
|
||||
self.instance.actions.add(comp_action)
|
||||
return comp_action
|
||||
action = self.instance.add_new_action(self)
|
||||
return action
|
||||
|
||||
|
||||
class NewCompensationDocumentForm(NewDocumentForm):
|
||||
document_model = CompensationDocument
|
||||
|
||||
|
||||
class NewEcoAccountDocumentForm(NewDocumentForm):
|
||||
document_model = EcoAccountDocument
|
||||
@@ -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,
|
||||
|
||||
@@ -61,7 +61,7 @@ class EcoAccountQualityChecker(CompensationQualityChecker):
|
||||
super().run_check()
|
||||
|
||||
def _check_legal_data(self):
|
||||
""" Checks the data quality for LegalData
|
||||
""" Checks the data quality for Legal
|
||||
|
||||
Returns:
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ from django.shortcuts import render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.forms import NewCompensationForm, EditCompensationForm
|
||||
from compensation.forms.modalForms import NewStateModalForm, NewDeadlineModalForm, NewActionModalForm
|
||||
from compensation.forms.modalForms import NewStateModalForm, NewDeadlineModalForm, NewActionModalForm, \
|
||||
NewCompensationDocumentForm
|
||||
from compensation.models import Compensation, CompensationState, CompensationAction, CompensationDocument
|
||||
from compensation.tables import CompensationTable
|
||||
from intervention.models import Intervention
|
||||
@@ -258,7 +259,7 @@ def new_document_view(request: HttpRequest, id: str):
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewDocumentForm(request.POST or None, request.FILES or None, instance=comp, user=request.user)
|
||||
form = NewCompensationDocumentForm(request.POST or None, request.FILES or None, instance=comp, user=request.user)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Document added")
|
||||
|
||||
@@ -15,7 +15,8 @@ from django.http import HttpRequest, Http404, JsonResponse
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
|
||||
from compensation.forms.forms import NewEcoAccountForm, EditEcoAccountForm
|
||||
from compensation.forms.modalForms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm
|
||||
from compensation.forms.modalForms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm, \
|
||||
NewEcoAccountDocumentForm
|
||||
from compensation.models import EcoAccount, EcoAccountDocument, CompensationState, CompensationAction
|
||||
from compensation.tables import EcoAccountTable
|
||||
from intervention.forms.modalForms import NewDeductionModalForm, ShareInterventionModalForm
|
||||
@@ -453,7 +454,7 @@ def new_document_view(request: HttpRequest, id: str):
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewDocumentForm(request.POST or None, request.FILES or None, instance=acc, user=request.user)
|
||||
form = NewEcoAccountDocumentForm(request.POST or None, request.FILES or None, instance=acc, user=request.user)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Document added")
|
||||
|
||||
Reference in New Issue
Block a user