From bcffae4a95a524702c586d0855db8c0459adf9b9 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 15 Nov 2021 17:09:17 +0100 Subject: [PATCH] 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 * --- compensation/forms/forms.py | 6 +- compensation/forms/modalForms.py | 104 ++++----------------- compensation/models.py | 112 ++++++++++++++++++++++- compensation/utils/quality.py | 2 +- compensation/views/compensation_views.py | 5 +- compensation/views/eco_account_views.py | 5 +- ema/forms.py | 12 ++- ema/tests/test_views.py | 4 +- ema/views.py | 4 +- intervention/admin.py | 6 +- intervention/forms/forms.py | 6 +- intervention/forms/modalForms.py | 10 +- intervention/models.py | 48 ++++++++-- intervention/utils/quality.py | 4 +- intervention/views.py | 4 +- konova/forms.py | 33 ++----- konova/tests/test_views.py | 10 +- 17 files changed, 220 insertions(+), 155 deletions(-) diff --git a/compensation/forms/forms.py b/compensation/forms/forms.py index 212efa63..0616f4ff 100644 --- a/compensation/forms/forms.py +++ b/compensation/forms/forms.py @@ -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 ) diff --git a/compensation/forms/modalForms.py b/compensation/forms/modalForms.py index 2cc10643..999fc7ab 100644 --- a/compensation/forms/modalForms.py +++ b/compensation/forms/modalForms.py @@ -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 \ No newline at end of file + action = self.instance.add_new_action(self) + return action + + +class NewCompensationDocumentForm(NewDocumentForm): + document_model = CompensationDocument + + +class NewEcoAccountDocumentForm(NewDocumentForm): + document_model = EcoAccountDocument \ No newline at end of file diff --git a/compensation/models.py b/compensation/models.py index a0cf80de..9763fa7b 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -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, diff --git a/compensation/utils/quality.py b/compensation/utils/quality.py index 28002db4..b622fcdd 100644 --- a/compensation/utils/quality.py +++ b/compensation/utils/quality.py @@ -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: diff --git a/compensation/views/compensation_views.py b/compensation/views/compensation_views.py index 02bdde01..a2a57b67 100644 --- a/compensation/views/compensation_views.py +++ b/compensation/views/compensation_views.py @@ -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") diff --git a/compensation/views/eco_account_views.py b/compensation/views/eco_account_views.py index 521963fc..2c5340ab 100644 --- a/compensation/views/eco_account_views.py +++ b/compensation/views/eco_account_views.py @@ -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") diff --git a/ema/forms.py b/ema/forms.py index 85a9094b..f0f15163 100644 --- a/ema/forms.py +++ b/ema/forms.py @@ -13,9 +13,9 @@ from django.urls import reverse, reverse_lazy from django.utils.translation import gettext_lazy as _ from compensation.forms.forms import AbstractCompensationForm, CompensationResponsibleFormMixin -from ema.models import Ema -from intervention.models import ResponsibilityData -from konova.forms import SimpleGeomForm +from ema.models import Ema, EmaDocument +from intervention.models import Responsibility +from konova.forms import SimpleGeomForm, NewDocumentForm from user.models import UserActionLogEntry, UserAction @@ -66,7 +66,7 @@ class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin): # 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, @@ -154,3 +154,7 @@ class EditEmaForm(NewEmaForm): # Add the log entry to the main objects log list self.instance.log.add(action) return self.instance + + +class NewEmaDocumentForm(NewDocumentForm): + document_model = EmaDocument \ No newline at end of file diff --git a/ema/tests/test_views.py b/ema/tests/test_views.py index 07d761c8..45092883 100644 --- a/ema/tests/test_views.py +++ b/ema/tests/test_views.py @@ -11,7 +11,7 @@ from django.test.client import Client from compensation.tests.test_views import CompensationViewTestCase from ema.models import Ema -from intervention.models import ResponsibilityData +from intervention.models import Responsibility from konova.models import Geometry from konova.settings import DEFAULT_GROUP, ETS_GROUP from user.models import UserActionLogEntry, UserAction @@ -66,7 +66,7 @@ class EmaViewTestCase(CompensationViewTestCase): action=UserAction.CREATED, ) # Create responsible data object - responsibility_data = ResponsibilityData.objects.create() + responsibility_data = Responsibility.objects.create() geometry = Geometry.objects.create() cls.ema = Ema.objects.create( identifier="TEST", diff --git a/ema/views.py b/ema/views.py index 496c55b1..783dc7c7 100644 --- a/ema/views.py +++ b/ema/views.py @@ -8,7 +8,7 @@ from django.utils.translation import gettext_lazy as _ from compensation.forms.modalForms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm from compensation.models import CompensationAction, CompensationState -from ema.forms import NewEmaForm, EditEmaForm +from ema.forms import NewEmaForm, EditEmaForm, NewEmaDocumentForm from ema.tables import EmaTable from intervention.forms.modalForms import ShareInterventionModalForm from konova.contexts import BaseContext @@ -343,7 +343,7 @@ def document_new_view(request: HttpRequest, id: str): """ ema = get_object_or_404(Ema, id=id) - form = NewDocumentForm(request.POST or None, request.FILES or None, instance=ema, user=request.user) + form = NewEmaDocumentForm(request.POST or None, request.FILES or None, instance=ema, user=request.user) return form.process_request( request, msg_success=_("Document added") diff --git a/intervention/admin.py b/intervention/admin.py index de11ee27..f65cb332 100644 --- a/intervention/admin.py +++ b/intervention/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from intervention.models import Intervention, ResponsibilityData, LegalData, Revocation, InterventionDocument +from intervention.models import Intervention, Responsibility, Legal, Revocation, InterventionDocument from konova.admin import AbstractDocumentAdmin @@ -46,7 +46,7 @@ class RevocationAdmin(admin.ModelAdmin): admin.site.register(Intervention, InterventionAdmin) -admin.site.register(ResponsibilityData, ResponsibilityAdmin) -admin.site.register(LegalData, LegalAdmin) +admin.site.register(Responsibility, ResponsibilityAdmin) +admin.site.register(Legal, LegalAdmin) admin.site.register(Revocation, RevocationAdmin) admin.site.register(InterventionDocument, InterventionDocumentAdmin) diff --git a/intervention/forms/forms.py b/intervention/forms/forms.py index f7453ccd..d8f88979 100644 --- a/intervention/forms/forms.py +++ b/intervention/forms/forms.py @@ -17,7 +17,7 @@ from codelist.models import KonovaCode from codelist.settings import CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID, \ CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID from intervention.inputs import GenerateInput -from intervention.models import Intervention, LegalData, ResponsibilityData +from intervention.models import Intervention, Legal, Responsibility from konova.forms import BaseForm, SimpleGeomForm from user.models import UserActionLogEntry, UserAction @@ -220,7 +220,7 @@ class NewInterventionForm(BaseForm): ) # Create legal data object (without M2M laws first) - legal_data = LegalData.objects.create( + legal_data = Legal.objects.create( registration_date=registration_date, binding_date=binding_date, process_type=_type, @@ -229,7 +229,7 @@ class NewInterventionForm(BaseForm): legal_data.laws.set(laws) # Create responsible data object - responsibility_data = ResponsibilityData.objects.create( + responsibility_data = Responsibility.objects.create( registration_office=registration_office, conservation_office=conservation_office, registration_file_number=registration_file_number, diff --git a/intervention/forms/modalForms.py b/intervention/forms/modalForms.py index fb6ee083..ef6fdf9e 100644 --- a/intervention/forms/modalForms.py +++ b/intervention/forms/modalForms.py @@ -14,8 +14,8 @@ from django.utils.translation import gettext_lazy as _ from compensation.models import EcoAccount, EcoAccountDeduction from intervention.inputs import TextToClipboardInput -from intervention.models import Revocation, RevocationDocument, Intervention -from konova.forms import BaseModalForm +from intervention.models import Revocation, RevocationDocument, Intervention, InterventionDocument +from konova.forms import BaseModalForm, NewDocumentForm from konova.utils.general import format_german_float from konova.utils.messenger import Messenger from konova.utils.user_checks import is_default_group_only @@ -409,4 +409,8 @@ class NewDeductionModalForm(BaseModalForm): surface=self.cleaned_data["surface"], created=user_action_create, ) - return deduction \ No newline at end of file + return deduction + + +class NewInterventionDocumentForm(NewDocumentForm): + document_model = InterventionDocument diff --git a/intervention/models.py b/intervention/models.py index 6043e422..d025cddd 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -9,7 +9,9 @@ import shutil from django.contrib.auth.models import User from django.contrib.gis.db import models +from django.db import transaction from django.db.models import QuerySet +from django.utils.translation import gettext_lazy as _ from codelist.models import KonovaCode from codelist.settings import CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID, CODELIST_LAW_ID, \ @@ -19,10 +21,10 @@ from intervention.utils.quality import InterventionQualityChecker from konova.models import BaseObject, Geometry, UuidModel, BaseResource, AbstractDocument, \ generate_document_file_upload_path, RecordableObject, CheckableObject, ShareableObject from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT -from user.models import UserActionLogEntry +from user.models import UserActionLogEntry, UserAction -class ResponsibilityData(UuidModel): +class Responsibility(UuidModel): """ Holds intervention data about responsible organizations and their file numbers for this case @@ -68,7 +70,7 @@ class Revocation(BaseResource): Holds revocation data e.g. for intervention objects """ date = models.DateField(null=True, blank=True, help_text="Revocation from") - legal = models.ForeignKey("LegalData", null=False, blank=False, on_delete=models.CASCADE, help_text="Refers to 'Widerspruch am'", related_name="revocations") + legal = models.ForeignKey("Legal", null=False, blank=False, on_delete=models.CASCADE, help_text="Refers to 'Widerspruch am'", related_name="revocations") comment = models.TextField(null=True, blank=True) def delete(self, *args, **kwargs): @@ -137,7 +139,7 @@ class RevocationDocument(AbstractDocument): pass -class LegalData(UuidModel): +class Legal(UuidModel): """ Holds intervention legal data such as important dates, laws or responsible handler """ @@ -175,14 +177,14 @@ class Intervention(BaseObject, ShareableObject, RecordableObject, CheckableObjec Interventions are e.g. construction sites where nature used to be. """ responsible = models.OneToOneField( - ResponsibilityData, + Responsibility, on_delete=models.SET_NULL, null=True, blank=True, help_text="Holds data on responsible organizations ('Zulassungsbehörde', 'Eintragungsstelle')" ) legal = models.OneToOneField( - LegalData, + Legal, on_delete=models.SET_NULL, null=True, blank=True, @@ -322,6 +324,40 @@ class Intervention(BaseObject, ShareableObject, RecordableObject, CheckableObjec for comp in comps: comp.log.add(log_entry) + def add_payment(self, form): + """ Adds a new payment to the intervention + + Args: + form (NewPaymentForm): The form holding the data + + Returns: + + """ + from compensation.models import Payment + form_data = form.cleaned_data + user = form.user + with transaction.atomic(): + created_action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.CREATED, + ) + edited_action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.EDITED, + comment=_("Added payment"), + ) + pay = Payment.objects.create( + created=created_action, + amount=form_data.get("amount", -1), + due_on=form_data.get("due", None), + comment=form_data.get("comment", None), + intervention=self, + ) + self.log.add(edited_action) + self.modified = edited_action + self.save() + return pay + class InterventionDocument(AbstractDocument): """ diff --git a/intervention/utils/quality.py b/intervention/utils/quality.py index b4717b18..e15b4ca4 100644 --- a/intervention/utils/quality.py +++ b/intervention/utils/quality.py @@ -24,7 +24,7 @@ class InterventionQualityChecker(AbstractQualityChecker): self.valid = len(self.messages) == 0 def _check_responsible_data(self): - """ Checks data quality of related ResponsibilityData + """ Checks data quality of related Responsibility Args: self.messages (dict): Holds error messages @@ -55,7 +55,7 @@ class InterventionQualityChecker(AbstractQualityChecker): self._add_missing_attr_name(_("Responsible data")) def _check_legal_data(self): - """ Checks data quality of related LegalData + """ Checks data quality of related Legal Args: self.messages (dict): Holds error messages diff --git a/intervention/views.py b/intervention/views.py index c2a87d6e..f79ae5f4 100644 --- a/intervention/views.py +++ b/intervention/views.py @@ -5,7 +5,7 @@ from django.shortcuts import render from intervention.forms.forms import NewInterventionForm, EditInterventionForm from intervention.forms.modalForms import ShareInterventionModalForm, NewRevocationModalForm, \ - CheckModalForm, NewDeductionModalForm + CheckModalForm, NewDeductionModalForm, NewInterventionDocumentForm from intervention.models import Intervention, Revocation, InterventionDocument, RevocationDocument from intervention.tables import InterventionTable from konova.contexts import BaseContext @@ -123,7 +123,7 @@ def new_document_view(request: HttpRequest, id: str): """ intervention = get_object_or_404(Intervention, id=id) - form = NewDocumentForm(request.POST or None, request.FILES or None, instance=intervention, user=request.user) + form = NewInterventionDocumentForm(request.POST or None, request.FILES or None, instance=intervention, user=request.user) return form.process_request( request, msg_success=_("Document added") diff --git a/konova/forms.py b/konova/forms.py index 82d46c8a..3087c5fc 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -21,11 +21,8 @@ from django.shortcuts import render from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from compensation.models import EcoAccount, Compensation, EcoAccountDocument, CompensationDocument -from ema.models import Ema, EmaDocument -from intervention.models import Intervention, Revocation, RevocationDocument, InterventionDocument from konova.contexts import BaseContext -from konova.models import BaseObject, Geometry +from konova.models import BaseObject, Geometry, RecordableObject from konova.settings import DEFAULT_SRID from konova.utils.message_templates import FORM_INVALID from user.models import UserActionLogEntry, UserAction @@ -382,13 +379,10 @@ class NewDocumentForm(BaseModalForm): } ) ) - document_instance_map = { - Intervention: InterventionDocument, - Compensation: CompensationDocument, - EcoAccount: EcoAccountDocument, - Revocation: RevocationDocument, - Ema: EmaDocument, - } + document_model = None + + class Meta: + abstract = True def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -398,11 +392,7 @@ class NewDocumentForm(BaseModalForm): self.form_attrs = { "enctype": "multipart/form-data", # important for file upload } - self.document_type = self.document_instance_map.get( - self.instance.__class__, - None - ) - if not self.document_type: + if not self.document_model: raise NotImplementedError("Unsupported document type for {}".format(self.instance.__class__)) def save(self): @@ -411,7 +401,7 @@ class NewDocumentForm(BaseModalForm): user=self.user, action=UserAction.CREATED, ) - doc = self.document_type.objects.create( + doc = self.document_model.objects.create( created=action, title=self.cleaned_data["title"], comment=self.cleaned_data["comment"], @@ -456,13 +446,7 @@ class RecordModalForm(BaseModalForm): self.form_title = _("Unrecord data") self.form_caption = _("I, {} {}, confirm that this data must be unrecorded.").format(self.user.first_name, self.user.last_name) - implemented_cls_logic = { - Intervention, - EcoAccount, - Ema, - } - instance_name = self.instance.__class__ - if instance_name not in implemented_cls_logic: + if not isinstance(self.instance, RecordableObject): raise NotImplementedError def is_valid(self): @@ -471,6 +455,7 @@ class RecordModalForm(BaseModalForm): Returns: """ + from intervention.models import Intervention super_val = super().is_valid() if self.instance.recorded: # If user wants to unrecord an already recorded dataset, we do not need to perform custom checks diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index 682a43f1..b3880623 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -15,7 +15,7 @@ from django.urls import reverse from codelist.models import KonovaCode from compensation.models import Compensation, CompensationState, CompensationAction, EcoAccount -from intervention.models import LegalData, ResponsibilityData, Intervention +from intervention.models import Legal, Responsibility, Intervention from konova.management.commands.setup_data import GROUPS_DATA from konova.models import Geometry from konova.settings import DEFAULT_GROUP @@ -103,9 +103,9 @@ class BaseTestCase(TestCase): action=UserAction.CREATED, ) # Create legal data object (without M2M laws first) - legal_data = LegalData.objects.create() + legal_data = Legal.objects.create() # Create responsible data object - responsibility_data = ResponsibilityData.objects.create() + responsibility_data = Responsibility.objects.create() geometry = Geometry.objects.create() # Finally create main object, holding the other objects intervention = Intervention.objects.create( @@ -162,8 +162,8 @@ class BaseTestCase(TestCase): ) geometry = Geometry.objects.create() # Create responsible data object - lega_data = LegalData.objects.create() - responsible_data = ResponsibilityData.objects.create() + lega_data = Legal.objects.create() + responsible_data = Responsibility.objects.create() # Finally create main object, holding the other objects eco_account = EcoAccount.objects.create( identifier="TEST",