Fixes and improvements

* moves diff_states message back to table top for direct presentation in compensation/detail/view.html
* removes diff_states rendering in deadline card in compensation/detail/view.html
* fixes before_state adding based on GET parameter
* refactors UserActionlogEntryEnum into a UserAction TextChoice (Django 3.x)
* adds ordering of compensation states depending on surface value
* refactors ServerMessageImportance from enum into TextChoice (Django 3.x)
* adds/updates translations
This commit is contained in:
mipel
2021-08-03 17:22:41 +02:00
parent 881edaeba6
commit 04db4e4e7f
17 changed files with 215 additions and 164 deletions

View File

@@ -6,14 +6,18 @@ Created on: 04.12.20
"""
from django import forms
from django.contrib import messages
from django.db import transaction
from django.http import HttpRequest
from django.shortcuts import redirect, render
from django.utils.translation import gettext_lazy as _
from compensation.models import Payment, CompensationState
from konova.enums import UserActionLogEntryEnum
from konova.contexts import BaseContext
from konova.forms import BaseForm, BaseModalForm
from konova.models import Deadline, DeadlineType
from user.models import UserActionLogEntry
from konova.utils.message_templates import FORM_INVALID
from user.models import UserActionLogEntry, UserAction
class NewCompensationForm(BaseForm):
@@ -61,7 +65,7 @@ class NewPaymentForm(BaseModalForm):
with transaction.atomic():
action = UserActionLogEntry.objects.create(
user=self.user,
action=UserActionLogEntryEnum.CREATED.value,
action=UserAction.CREATED,
)
pay = Payment.objects.create(
created=action,
@@ -106,6 +110,45 @@ class NewStateModalForm(BaseModalForm):
self.instance.after_states.add(state)
return state
def process_request(self, request: HttpRequest, msg_success: str = _("Object removed"), msg_error: str = FORM_INVALID, redirect_url: str = None):
""" Generic processing of request
Wraps the request processing logic, so we don't need the same code everywhere a RemoveModalForm is being used
Args:
request (HttpRequest): The incoming request
msg_success (str): The message in case of successful removing
msg_error (str): The message in case of an error
Returns:
"""
redirect_url = redirect_url if redirect_url is not None else request.META.get("HTTP_REFERER", "home")
template = self.template
if request.method == "POST":
if self.is_valid():
is_before_state = bool(request.GET.get("before", False))
self.save(is_before_state=is_before_state)
messages.success(
request,
msg_success
)
return redirect(redirect_url)
else:
messages.info(
request,
msg_error
)
return redirect(redirect_url)
elif request.method == "GET":
context = {
"form": self,
}
context = BaseContext(request, context).context
return render(request, template, context)
else:
raise NotImplementedError
class NewDeadlineModalForm(BaseModalForm):
type = forms.ChoiceField(
@@ -150,7 +193,7 @@ class NewDeadlineModalForm(BaseModalForm):
with transaction.atomic():
action = UserActionLogEntry.objects.create(
user=self.user,
action=UserActionLogEntryEnum.CREATED.value
action=UserAction.CREATED.value
)
deadline = Deadline.objects.create(
type=self.cleaned_data["type"],

View File

@@ -14,11 +14,10 @@ from django.utils.timezone import now
from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_IDENTIFIER_TEMPLATE
from intervention.models import Intervention, ResponsibilityData
from konova.enums import UserActionLogEntryEnum
from konova.models import BaseObject, BaseResource, Geometry, UuidModel
from konova.utils.generators import generate_random_string
from organisation.models import Organisation
from user.models import UserActionLogEntry
from user.models import UserActionLogEntry, UserAction
class Payment(BaseResource):
@@ -156,11 +155,9 @@ class Compensation(AbstractCompensation):
action = UserActionLogEntry.objects.create(
user=_user,
timestamp=_now,
action=UserActionLogEntryEnum.DELETED.value
action=UserAction.DELETED
)
self.deleted = action
#self.deleted_on = _now
#self.deleted_by = _user
self.save()
def save(self, *args, **kwargs):

View File

@@ -57,10 +57,5 @@
{% endfor %}
</tbody>
</table>
{% if sum_before_states > sum_after_states %}
<div class="row alert alert-danger">
{% trans 'Missing surfaces: ' %}{{ diff_states|floatformat:2 }} m²
</div>
{% endif %}
</div>
</div>

View File

@@ -21,6 +21,11 @@
</div>
</div>
<div class="card-body scroll-300">
{% if sum_before_states > sum_after_states %}
<div class="row alert alert-danger">
{% trans 'Missing surfaces: ' %}{{ diff_states|floatformat:2 }} m²
</div>
{% endif %}
<table class="table table-hover">
<thead>
<tr>
@@ -36,7 +41,7 @@
</tr>
</thead>
<tbody>
{% for state in comp.after_states.all %}
{% for state in after_states %}
<tr>
<td class="align-middle">
{{ state.biotope_type }}
@@ -53,10 +58,5 @@
{% endfor %}
</tbody>
</table>
{% if sum_before_states > sum_after_states %}
<div class="row alert alert-danger">
{% trans 'Missing surfaces: ' %}{{ diff_states|floatformat:2 }} m²
</div>
{% endif %}
</div>
</div>

View File

@@ -21,6 +21,11 @@
</div>
</div>
<div class="card-body scroll-300">
{% if sum_before_states < sum_after_states %}
<div class="row alert alert-danger">
{% trans 'Missing surfaces: ' %}{{ diff_states|floatformat:2 }} m²
</div>
{% endif %}
<table class="table table-hover">
<thead>
<tr>
@@ -36,7 +41,7 @@
</tr>
</thead>
<tbody>
{% for state in comp.before_states.all %}
{% for state in before_states %}
<tr>
<td class="align-middle">
{{ state.biotope_type }}
@@ -53,10 +58,5 @@
{% endfor %}
</tbody>
</table>
{% if sum_before_states < sum_after_states %}
<div class="row alert alert-danger">
{% trans 'Missing surfaces: ' %}{{ diff_states|floatformat:2 }} m²
</div>
{% endif %}
</div>
</div>

View File

@@ -76,22 +76,28 @@ def open_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().order_by("-surface")
after_states = comp.after_states.all().order_by("-surface")
# 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 = comp.before_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
sum_after_states = comp.after_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
diff_states = abs(sum_before_states - sum_after_states)
context = {
"comp": comp,
"geom_form": geom_form,
"has_access": is_data_shared,
"is_default_member": in_group(_user, _(DEFAULT_GROUP)),
"is_zb_member": in_group(_user, _(ZB_GROUP)),
"is_ets_member": in_group(_user, _(ETS_GROUP)),
"before_states": before_states,
"after_states": after_states,
"sum_before_states": sum_before_states,
"sum_after_states": sum_after_states,
"diff_states": diff_states,
"is_default_member": in_group(_user, _(DEFAULT_GROUP)),
"is_zb_member": in_group(_user, _(ZB_GROUP)),
"is_ets_member": in_group(_user, _(ETS_GROUP)),
}
context = BaseContext(request, context).context
return render(request, template, context)