#36 Quality checks
* adds quality check logic for Compensations with CompensationQUalityChecker * adds compensation quality checking to checking routine of RunCheckModalForm.is_valid() * adds/updates translations
This commit is contained in:
@@ -19,6 +19,7 @@ from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES
|
||||
CODELIST_COMPENSATION_FUNDING_ID
|
||||
from compensation.managers import CompensationStateManager, EcoAccountDeductionManager, CompensationActionManager, \
|
||||
EcoAccountManager, CompensationManager
|
||||
from compensation.utils.quality import CompensationQualityChecker
|
||||
from intervention.models import Intervention, ResponsibilityData, LegalData
|
||||
from konova.models import BaseObject, BaseResource, Geometry, UuidModel, AbstractDocument, \
|
||||
generate_document_file_upload_path
|
||||
@@ -163,13 +164,42 @@ class AbstractCompensation(BaseObject):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def get_surface(self) -> float:
|
||||
def get_surface_after_states(self) -> float:
|
||||
""" Calculates the compensation's/account's surface
|
||||
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"]
|
||||
return self._calc_surface(self.after_states.all())
|
||||
|
||||
def get_surface_before_states(self) -> float:
|
||||
""" Calculates the compensation's/account's surface
|
||||
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self._calc_surface(self.before_states.all())
|
||||
|
||||
def _calc_surface(self, qs: QuerySet):
|
||||
""" Calculates the surface sum of a given queryset
|
||||
|
||||
Args:
|
||||
qs (QuerySet): The queryset containing CompensationState entries
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return qs.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
|
||||
def quality_check(self) -> CompensationQualityChecker:
|
||||
""" Performs data quality check
|
||||
|
||||
Returns:
|
||||
checker (CompensationQualityChecker): Holds validity data and error messages
|
||||
"""
|
||||
checker = CompensationQualityChecker(self)
|
||||
checker.run_check()
|
||||
return checker
|
||||
|
||||
|
||||
class Compensation(AbstractCompensation):
|
||||
|
||||
48
compensation/utils/quality.py
Normal file
48
compensation/utils/quality.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 25.10.21
|
||||
|
||||
"""
|
||||
from django.utils.translation import gettext_lazy as _, pgettext_lazy as _con
|
||||
from konova.utils.quality import AbstractQualityChecker
|
||||
|
||||
|
||||
class CompensationQualityChecker(AbstractQualityChecker):
|
||||
def run_check(self):
|
||||
""" Perform all defined data checks
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self._check_states()
|
||||
self._check_actions()
|
||||
self._check_geometry()
|
||||
self.valid = len(self.messages) == 0
|
||||
|
||||
def _check_states(self):
|
||||
""" Checks data quality for related CompensationState objects
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
after_states = self.obj.get_surface_after_states()
|
||||
before_states = self.obj.get_surface_before_states()
|
||||
if after_states != before_states:
|
||||
self.messages.append(
|
||||
_("States unequal")
|
||||
)
|
||||
if before_states == 0:
|
||||
self._add_missing_attr_name(_("States before"))
|
||||
if after_states == 0:
|
||||
self._add_missing_attr_name(_("States after"))
|
||||
|
||||
def _check_actions(self):
|
||||
""" Checks data quality for related CompensationState objects
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if not self.obj.actions.all():
|
||||
self._add_missing_attr_name(_con("Compensation", "Actions"))
|
||||
Reference in New Issue
Block a user