From 1c3ab898cc2923782393278f167ebd698304a49f Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 25 Oct 2021 17:01:02 +0200 Subject: [PATCH] #36 Quality checks * refactors toggling of recorded status into RecordableMixin --- compensation/models.py | 4 ++-- ema/models.py | 4 ++-- intervention/models.py | 5 ++--- konova/forms.py | 18 +----------------- konova/models.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/compensation/models.py b/compensation/models.py index c672714e..27fb9638 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -22,7 +22,7 @@ from compensation.managers import CompensationStateManager, EcoAccountDeductionM 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 + generate_document_file_upload_path, RecordableMixin from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from user.models import UserActionLogEntry @@ -310,7 +310,7 @@ class CompensationDocument(AbstractDocument): pass -class EcoAccount(AbstractCompensation): +class EcoAccount(AbstractCompensation, RecordableMixin): """ 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. diff --git a/ema/models.py b/ema/models.py index a0b5f5e7..1fa85019 100644 --- a/ema/models.py +++ b/ema/models.py @@ -7,12 +7,12 @@ from django.db.models import QuerySet from compensation.models import AbstractCompensation from ema.managers import EmaManager from ema.utils.quality import EmaQualityChecker -from konova.models import AbstractDocument, generate_document_file_upload_path +from konova.models import AbstractDocument, generate_document_file_upload_path, RecordableMixin from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE from user.models import UserActionLogEntry -class Ema(AbstractCompensation): +class Ema(AbstractCompensation, RecordableMixin): """ EMA = Ersatzzahlungsmaßnahme (compensation actions from payments) diff --git a/intervention/models.py b/intervention/models.py index 7fbb9226..6bd042fc 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -10,7 +10,6 @@ import shutil from django.contrib.auth.models import User from django.contrib.gis.db import models 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, \ @@ -18,7 +17,7 @@ from codelist.settings import CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVA from intervention.managers import InterventionManager from intervention.utils.quality import InterventionQualityChecker from konova.models import BaseObject, Geometry, UuidModel, BaseResource, AbstractDocument, \ - generate_document_file_upload_path + generate_document_file_upload_path, RecordableMixin from konova.settings import DEFAULT_SRID_RLP, LANIS_LINK_TEMPLATE, LANIS_ZOOM_LUT from konova.utils import generators from user.models import UserActionLogEntry @@ -172,7 +171,7 @@ class LegalData(UuidModel): revocation = models.OneToOneField(Revocation, null=True, blank=True, help_text="Refers to 'Widerspruch am'", on_delete=models.SET_NULL) -class Intervention(BaseObject): +class Intervention(BaseObject, RecordableMixin): """ Interventions are e.g. construction sites where nature used to be. """ diff --git a/konova/forms.py b/konova/forms.py index 83cf720a..cf868186 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -486,21 +486,5 @@ class RecordModalForm(BaseModalForm): def save(self): with transaction.atomic(): if self.cleaned_data["confirm"]: - if self.instance.recorded: - # unrecord! - unrecord_action = UserActionLogEntry.objects.create( - user=self.user, - action=UserAction.UNRECORDED - ) - # Do not delete the old .recorded attribute, since it shall stay in the .log list! - self.instance.recorded = None - self.instance.log.add(unrecord_action) - else: - record_action = UserActionLogEntry.objects.create( - user=self.user, - action=UserAction.RECORDED - ) - self.instance.recorded = record_action - self.instance.log.add(record_action) - self.instance.save() + self.instance.toggle_recorded(self.user) return self.instance \ No newline at end of file diff --git a/konova/models.py b/konova/models.py index f9f87584..2d34a4bf 100644 --- a/konova/models.py +++ b/konova/models.py @@ -313,3 +313,34 @@ class Geometry(BaseResource): """ from konova.settings import DEFAULT_SRID geom = MultiPolygonField(null=True, blank=True, srid=DEFAULT_SRID) + + +class RecordableMixin: + """ Mixin to be combined with BaseObject class + + Provides functionality related to un/recording of data + + """ + def toggle_recorded(self, user: User): + """ Un/Record intervention + + Args: + user (User): Performing user + + Returns: + + """ + if not self.recorded: + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.RECORDED + ) + self.recorded = action + else: + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.UNRECORDED + ) + self.recorded = None + self.save() + self.log.add(action)