Merge branch 'master' into test
# Conflicts: # locale/de/LC_MESSAGES/django.mo # locale/de/LC_MESSAGES/django.po
This commit is contained in:
commit
777b7a929d
@ -65,6 +65,22 @@ class NewDeadlineModalForm(BaseModalForm):
|
||||
self.form_title = _("New deadline")
|
||||
self.form_caption = _("Insert data for the new deadline")
|
||||
|
||||
def is_valid(self):
|
||||
valid = super().is_valid()
|
||||
deadline_type = self.cleaned_data.get("type")
|
||||
comment = self.cleaned_data.get("comment") or None
|
||||
|
||||
other_deadline_without_comment = deadline_type == DeadlineType.OTHER and comment is None
|
||||
|
||||
if other_deadline_without_comment:
|
||||
self.add_error(
|
||||
"comment",
|
||||
_("Please explain this 'other' type of deadline.")
|
||||
)
|
||||
valid &= False
|
||||
|
||||
return valid
|
||||
|
||||
def save(self):
|
||||
deadline = self.instance.add_deadline(self)
|
||||
return deadline
|
||||
|
@ -22,7 +22,7 @@ from compensation.utils.quality import CompensationQualityChecker
|
||||
from konova.models import BaseObject, AbstractDocument, Deadline, generate_document_file_upload_path, \
|
||||
GeoReferencedMixin, DeadlineType, ResubmitableObjectMixin
|
||||
from konova.utils.message_templates import DATA_UNSHARED_EXPLANATION, COMPENSATION_REMOVED_TEMPLATE, \
|
||||
DOCUMENT_REMOVED_TEMPLATE, DEADLINE_REMOVED, ADDED_DEADLINE, \
|
||||
DOCUMENT_REMOVED_TEMPLATE, DEADLINE_REMOVED, DEADLINE_ADDED, \
|
||||
COMPENSATION_ACTION_REMOVED, COMPENSATION_STATE_REMOVED, INTERVENTION_HAS_REVOCATIONS_TEMPLATE
|
||||
from user.models import UserActionLogEntry
|
||||
|
||||
@ -76,7 +76,7 @@ class AbstractCompensation(BaseObject,
|
||||
|
||||
self.save()
|
||||
self.deadlines.add(deadline)
|
||||
self.mark_as_edited(user, edit_comment=ADDED_DEADLINE)
|
||||
self.mark_as_edited(user, edit_comment=DEADLINE_ADDED)
|
||||
return deadline
|
||||
|
||||
def remove_deadline(self, form):
|
||||
@ -200,7 +200,9 @@ class AbstractCompensation(BaseObject,
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return qs.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
val = qs.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
val = float('{:0.2f}'.format(val))
|
||||
return val
|
||||
|
||||
def quality_check(self) -> CompensationQualityChecker:
|
||||
""" Performs data quality check
|
||||
|
@ -61,7 +61,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
|
||||
def clean(self):
|
||||
# Deductable surface can not be larger than added states after surface
|
||||
after_state_sum = self.get_state_after_surface_sum()
|
||||
after_state_sum = self.get_surface_after_states()
|
||||
if self.deductable_surface > after_state_sum:
|
||||
raise ValidationError(_("Deductable surface can not be larger than existing surfaces in after states"))
|
||||
|
||||
@ -100,15 +100,9 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
|
||||
def get_state_after_surface_sum(self) -> float:
|
||||
""" Calculates the account's after state surface sum
|
||||
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
val = self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
val = float('{:0.2f}'.format(val))
|
||||
return val
|
||||
|
||||
def __calculate_deductable_rest(self):
|
||||
""" Calculates available rest surface of the eco account
|
||||
@ -118,10 +112,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
Returns:
|
||||
ret_val_total (float): Total amount
|
||||
"""
|
||||
deductions = self.deductions.filter(
|
||||
intervention__deleted=None,
|
||||
)
|
||||
deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
deductions_surfaces = self.get_deductions_surface()
|
||||
|
||||
available_surface = self.deductable_surface
|
||||
if available_surface is None:
|
||||
|
@ -95,7 +95,7 @@ class EcoAccountQualityChecker(CompensationQualityChecker):
|
||||
is_surface_invalid = surface == 0
|
||||
if is_surface_invalid:
|
||||
self._add_missing_attr_name(_("Available Surface"))
|
||||
after_state_surface = self.obj.get_state_after_surface_sum()
|
||||
after_state_surface = self.obj.get_surface_after_states()
|
||||
if surface > after_state_surface:
|
||||
self.messages.append(
|
||||
_("Deductable surface can not be larger than state surface")
|
||||
|
@ -228,8 +228,6 @@ def detail_view(request: HttpRequest, id: str):
|
||||
_user = request.user
|
||||
is_data_shared = comp.intervention.is_shared_with(_user)
|
||||
|
||||
|
||||
|
||||
# Order states according to surface
|
||||
before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
@ -237,8 +235,8 @@ def detail_view(request: HttpRequest, id: str):
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_before_states = comp.get_surface_before_states()
|
||||
sum_after_states = comp.get_surface_after_states()
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
request = comp.set_status_messages(request)
|
||||
|
@ -208,8 +208,8 @@ def detail_view(request: HttpRequest, id: str):
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_before_states = acc.get_surface_before_states()
|
||||
sum_after_states = acc.get_surface_after_states()
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
# Calculate rest of available surface for deductions
|
||||
available_total = acc.deductable_rest
|
||||
|
@ -149,8 +149,8 @@ def detail_view(request: HttpRequest, id: str):
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_before_states = ema.get_surface_before_states()
|
||||
sum_after_states = ema.get_surface_after_states()
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
ema.set_status_messages(request)
|
||||
|
@ -453,7 +453,7 @@ class BaseTestCase(TestCase):
|
||||
eco_account.actions.add(self.comp_action)
|
||||
eco_account.geometry.geom = self.create_dummy_geometry()
|
||||
eco_account.geometry.save()
|
||||
eco_account.deductable_surface = eco_account.get_state_after_surface_sum()
|
||||
eco_account.deductable_surface = eco_account.get_surface_after_states()
|
||||
eco_account.deadlines.add(self.finished_deadline)
|
||||
eco_account.save()
|
||||
return eco_account
|
||||
|
7
konova/tests/unit/__init__.py
Normal file
7
konova/tests/unit/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 29.08.23
|
||||
|
||||
"""
|
118
konova/tests/unit/test_deadline.py
Normal file
118
konova/tests/unit/test_deadline.py
Normal file
@ -0,0 +1,118 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 29.08.23
|
||||
|
||||
"""
|
||||
from django.test import RequestFactory
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
|
||||
from konova.models import DeadlineType
|
||||
from konova.tests.test_views import BaseTestCase
|
||||
from konova.utils.generators import generate_random_string
|
||||
from konova.utils.message_templates import DEADLINE_ADDED, DEADLINE_EDITED
|
||||
from user.models import UserAction
|
||||
|
||||
|
||||
class NewDeadlineModalFormTestCase(BaseTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.request = RequestFactory().request()
|
||||
self.request.user = self.superuser
|
||||
self.today = now().date()
|
||||
|
||||
def test_init(self):
|
||||
form = NewDeadlineModalForm(request=self.request, instance=self.compensation)
|
||||
self.assertEqual(form.form_title, str(_("New deadline")))
|
||||
self.assertEqual(form.form_caption, str(_("Insert data for the new deadline")))
|
||||
self.assertEqual(form.user, self.superuser)
|
||||
self.assertEqual(form.request, self.request)
|
||||
|
||||
def test_is_valid(self):
|
||||
data = {
|
||||
"type": DeadlineType.MAINTAIN,
|
||||
"date": self.today,
|
||||
"comment": "",
|
||||
}
|
||||
form = NewDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation
|
||||
)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
data["type"] = DeadlineType.OTHER
|
||||
form = NewDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation
|
||||
)
|
||||
self.assertFalse(form.is_valid(), msg=form.errors)
|
||||
self.assertTrue(form.has_error("comment"))
|
||||
_error = form.errors["comment"]
|
||||
self.assertEqual(len(_error), 1)
|
||||
self.assertEqual(_error[0], str(_("Please explain this 'other' type of deadline.")))
|
||||
|
||||
data["comment"] = "Test"
|
||||
data["type"] = DeadlineType.OTHER
|
||||
form = NewDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation
|
||||
)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_save(self):
|
||||
data = {
|
||||
"type": DeadlineType.MAINTAIN,
|
||||
"date": self.today,
|
||||
"comment": generate_random_string(length=20, use_letters_lc=True),
|
||||
}
|
||||
form = NewDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation
|
||||
)
|
||||
self.assertTrue(form.is_valid(), msg=form.errors)
|
||||
deadline = form.save()
|
||||
self.assertEqual(deadline.type, data["type"])
|
||||
self.assertEqual(deadline.date, data["date"])
|
||||
self.assertEqual(deadline.comment, data["comment"])
|
||||
self.assertIn(deadline, self.compensation.deadlines.all())
|
||||
|
||||
last_log = self.compensation.log.first()
|
||||
self.assertEqual(last_log.user, self.superuser)
|
||||
self.assertEqual(last_log.action, UserAction.EDITED)
|
||||
self.assertEqual(last_log.comment, DEADLINE_ADDED)
|
||||
|
||||
|
||||
class EditDeadlineModalFormTestCase(NewDeadlineModalFormTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
|
||||
def test_save(self):
|
||||
data = {
|
||||
"type": DeadlineType.MAINTAIN,
|
||||
"date": self.today,
|
||||
"comment": generate_random_string(length=20, use_letters_lc=True),
|
||||
}
|
||||
form = EditDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation,
|
||||
deadline=self.finished_deadline,
|
||||
)
|
||||
|
||||
self.assertTrue(form.is_valid(), msg=form.errors)
|
||||
deadline = form.save()
|
||||
self.assertEqual(deadline.type, data["type"])
|
||||
self.assertEqual(deadline.date, data["date"])
|
||||
self.assertEqual(deadline.comment, data["comment"])
|
||||
|
||||
last_log = self.compensation.log.first()
|
||||
self.assertEqual(last_log.action, UserAction.EDITED)
|
||||
self.assertEqual(last_log.user, self.superuser)
|
||||
self.assertEqual(last_log.comment, DEADLINE_EDITED)
|
@ -79,7 +79,6 @@ DOCUMENT_EDITED = _("Document edited")
|
||||
|
||||
# Edited
|
||||
EDITED_GENERAL_DATA = _("Edited general data")
|
||||
ADDED_DEADLINE = _("Added deadline")
|
||||
|
||||
# Geometry
|
||||
GEOMETRY_CONFLICT_WITH_TEMPLATE = _("Geometry conflict detected with {}")
|
||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user