From abdc574ea235262bce9b0c6bda62188fe5668874 Mon Sep 17 00:00:00 2001 From: mipel Date: Mon, 9 Aug 2021 14:39:36 +0200 Subject: [PATCH] EcoAccount detail * adds detail view for eco account * adds withdraw detail view to eco acount detail * generalizes compensation/includes templates to be reusable for eco-accounts * moves is_shared_with() check to class BaseObject * adds/updates translations --- compensation/models.py | 2 +- compensation/tables.py | 2 +- .../eco_account/includes/withdraws.html | 61 +++++++ .../compensation/detail/eco_account/view.html | 108 +++++++++++ .../compensation/detail/includes/actions.html | 6 +- .../detail/includes/controls.html | 4 +- .../detail/includes/deadlines.html | 6 +- .../detail/includes/documents.html | 6 +- .../detail/includes/states-after.html | 4 +- .../detail/includes/states-before.html | 4 +- .../templates/compensation/detail/view.html | 22 +-- compensation/views/compensation_views.py | 4 +- compensation/views/eco_account_views.py | 53 +++++- intervention/models.py | 13 -- ...-account-withdraws.html => withdraws.html} | 4 +- .../templates/intervention/detail/view.html | 2 +- konova/models.py | 16 ++ .../konova/custom_widgets/progressbar.html | 2 +- locale/de/LC_MESSAGES/django.mo | Bin 15990 -> 16047 bytes locale/de/LC_MESSAGES/django.po | 172 ++++++++++-------- 20 files changed, 364 insertions(+), 127 deletions(-) create mode 100644 compensation/templates/compensation/detail/eco_account/includes/withdraws.html create mode 100644 compensation/templates/compensation/detail/eco_account/view.html rename intervention/templates/intervention/detail/includes/{eco-account-withdraws.html => withdraws.html} (93%) diff --git a/compensation/models.py b/compensation/models.py index ecd432a..1d3e7cc 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -17,7 +17,7 @@ from compensation.settings import COMPENSATION_IDENTIFIER_LENGTH, COMPENSATION_I from intervention.models import Intervention, ResponsibilityData from konova.models import BaseObject, BaseResource, Geometry, UuidModel from konova.utils.generators import generate_random_string -from user.models import UserActionLogEntry, UserAction +from user.models import UserActionLogEntry class Payment(BaseResource): diff --git a/compensation/tables.py b/compensation/tables.py index c8b5ae3..29c9627 100644 --- a/compensation/tables.py +++ b/compensation/tables.py @@ -239,7 +239,7 @@ class EcoAccountTable(BaseTable): """ withdraws = record.withdraws.all() - withdraw_surfaces = withdraws.aggregate(Sum("amount"))["amount__sum"] or 0 + withdraw_surfaces = withdraws.aggregate(Sum("surface"))["surface__sum"] or 0 after_states_surfaces = record.after_states.all().aggregate(Sum("surface"))["surface__sum"] or withdraw_surfaces ## no division by zero value = int(((after_states_surfaces - withdraw_surfaces) / after_states_surfaces) * 100) html = render_to_string("konova/custom_widgets/progressbar.html", {"value": value}) diff --git a/compensation/templates/compensation/detail/eco_account/includes/withdraws.html b/compensation/templates/compensation/detail/eco_account/includes/withdraws.html new file mode 100644 index 0000000..d085742 --- /dev/null +++ b/compensation/templates/compensation/detail/eco_account/includes/withdraws.html @@ -0,0 +1,61 @@ +{% load i18n l10n fontawesome_5 humanize %} +
+
+
+
+
+ {{obj.withdraws.count}} + {% trans 'Eco Account Withdraws' %} +
+
+
+
+ {% if is_default_member and has_access %} + + + + {% endif %} +
+
+
+
+
+ + + + + + + + + + {% for withdraw in obj.withdraws.all %} + + + + + + {% endfor %} + +
+ {% trans 'Intervention Identifier' %} + + {% trans 'Amount' %} + + {% trans 'Action' %} +
+ + {{ withdraw.intervention.identifier }} + + {{ withdraw.surface|floatformat:2|intcomma }} m² + {% if is_default_member and has_access %} + + {% endif %} +
+
+
\ No newline at end of file diff --git a/compensation/templates/compensation/detail/eco_account/view.html b/compensation/templates/compensation/detail/eco_account/view.html new file mode 100644 index 0000000..e42a9b3 --- /dev/null +++ b/compensation/templates/compensation/detail/eco_account/view.html @@ -0,0 +1,108 @@ +{% extends 'base.html' %} +{% load i18n l10n static fontawesome_5 humanize %} + +{% block head %} + +{% endblock %} + +{% block body %} + +
+
+

{% trans 'Eco-account' %} {{obj.identifier}}

+
+
+ {% include 'compensation/detail/includes/controls.html' %} +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + +
{% trans 'Title' %}{{obj.title}}
{% trans 'Available' %} + {% with available as value %} + {% include 'konova/custom_widgets/progressbar.html' %} + {% endwith %} +
{% trans 'Recorded' %} + {% if obj.recorded is None %} + + {% fa5_icon 'bookmark' 'far' %} + + {% else %} + + {% fa5_icon 'bookmark' %} + + {% endif %} +
{% trans 'Last modified' %} + {{obj.created.timestamp|default_if_none:""|naturalday}} +
+ {{obj.created.user.username}} +
{% trans 'Shared with' %} + {% for user in obj.users.all %} + {% include 'user/includes/contact_modal_button.html' %} + {% endfor %} +
+
+
+
+ {% if geom_form.area == 0 %} +
{% trans 'No geometry added, yet.' %}
+ {% endif %} + {{geom_form.media}} + {{geom_form.geom}} +
+
+
+ +
+
+ {% include 'compensation/detail/includes/states-before.html' %} +
+
+ {% include 'compensation/detail/includes/states-after.html' %} +
+
+
+
+ {% include 'compensation/detail/includes/actions.html' %} +
+
+ {% include 'compensation/detail/includes/deadlines.html' %} +
+
+
+
+ {% include 'compensation/detail/includes/documents.html' %} +
+
+ {% include 'compensation/detail/eco_account/includes/withdraws.html' %} +
+
+ + +{% with 'btn-modal' as btn_class %} + {% include 'modal/modal_form_script.html' %} +{% endwith %} + +{% endblock %} \ No newline at end of file diff --git a/compensation/templates/compensation/detail/includes/actions.html b/compensation/templates/compensation/detail/includes/actions.html index ec6fd4a..c4b7fb4 100644 --- a/compensation/templates/compensation/detail/includes/actions.html +++ b/compensation/templates/compensation/detail/includes/actions.html @@ -4,14 +4,14 @@
- {{comp.actions.count}} + {{obj.actions.count}} {% trans 'Actions' context 'Compensation' %}
{% if is_default_member and has_access %} - @@ -39,7 +39,7 @@ - {% for action in comp.actions.all %} + {% for action in obj.actions.all %} {{ action.action_type }} diff --git a/compensation/templates/compensation/detail/includes/controls.html b/compensation/templates/compensation/detail/includes/controls.html index 528833a..34a5c03 100644 --- a/compensation/templates/compensation/detail/includes/controls.html +++ b/compensation/templates/compensation/detail/includes/controls.html @@ -18,10 +18,10 @@ {% fa5_icon 'edit' %} - - {% endif %} diff --git a/compensation/templates/compensation/detail/includes/deadlines.html b/compensation/templates/compensation/detail/includes/deadlines.html index 2463acd..f1385a5 100644 --- a/compensation/templates/compensation/detail/includes/deadlines.html +++ b/compensation/templates/compensation/detail/includes/deadlines.html @@ -4,14 +4,14 @@
- {{comp.deadlines.count}} + {{obj.deadlines.count}} {% trans 'Deadlines' %}
{% if is_default_member and has_access %} - @@ -39,7 +39,7 @@ - {% for deadline in comp.deadlines.all %} + {% for deadline in obj.deadlines.all %} {% trans deadline.type_humanized %} diff --git a/compensation/templates/compensation/detail/includes/documents.html b/compensation/templates/compensation/detail/includes/documents.html index 8f4c8f5..3801f7c 100644 --- a/compensation/templates/compensation/detail/includes/documents.html +++ b/compensation/templates/compensation/detail/includes/documents.html @@ -4,14 +4,14 @@
- {{comp.documents.count}} + {{obj.documents.count}} {% trans 'Documents' %}
{% if is_default_member and has_access %} - @@ -36,7 +36,7 @@ - {% for doc in comp.documents.all %} + {% for doc in obj.documents.all %} diff --git a/compensation/templates/compensation/detail/includes/states-after.html b/compensation/templates/compensation/detail/includes/states-after.html index 993cead..585d539 100644 --- a/compensation/templates/compensation/detail/includes/states-after.html +++ b/compensation/templates/compensation/detail/includes/states-after.html @@ -4,14 +4,14 @@
- {{comp.after_states.count}} + {{obj.after_states.count}} {% trans 'States after' %}
{% if is_default_member and has_access %} - diff --git a/compensation/templates/compensation/detail/includes/states-before.html b/compensation/templates/compensation/detail/includes/states-before.html index 2a440f5..fe6e434 100644 --- a/compensation/templates/compensation/detail/includes/states-before.html +++ b/compensation/templates/compensation/detail/includes/states-before.html @@ -4,14 +4,14 @@
- {{comp.before_states.count}} + {{obj.before_states.count}} {% trans 'States before' %}
{% if is_default_member and has_access %} - diff --git a/compensation/templates/compensation/detail/view.html b/compensation/templates/compensation/detail/view.html index d932210..a2b6ad8 100644 --- a/compensation/templates/compensation/detail/view.html +++ b/compensation/templates/compensation/detail/view.html @@ -9,7 +9,7 @@
-

{% trans 'Compensation' %} {{comp.identifier}}

+

{% trans 'Compensation' %} {{obj.identifier}}

{% include 'compensation/detail/includes/controls.html' %} @@ -22,25 +22,25 @@ - + diff --git a/compensation/views/compensation_views.py b/compensation/views/compensation_views.py index a062af8..1ed6116 100644 --- a/compensation/views/compensation_views.py +++ b/compensation/views/compensation_views.py @@ -84,7 +84,7 @@ def open_view(request: HttpRequest, id: str): diff_states = abs(sum_before_states - sum_after_states) context = { - "comp": comp, + "obj": comp, "geom_form": geom_form, "has_access": is_data_shared, "before_states": before_states, @@ -142,6 +142,8 @@ def remove_view(request: HttpRequest, id: str): msg_success=_("Compensation removed"), redirect_url="", ) + + @login_required def new_document_view(request: HttpRequest, id: str): """ Renders a form for uploading new documents diff --git a/compensation/views/eco_account_views.py b/compensation/views/eco_account_views.py index 67c407f..5552db9 100644 --- a/compensation/views/eco_account_views.py +++ b/compensation/views/eco_account_views.py @@ -5,6 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 09.08.21 """ +from django.db.models import Sum from django.utils.translation import gettext_lazy as _ from django.contrib.auth.decorators import login_required from django.core.exceptions import ObjectDoesNotExist @@ -15,7 +16,9 @@ from compensation.models import EcoAccount from compensation.tables import EcoAccountTable from konova.contexts import BaseContext from konova.decorators import any_group_check, default_group_required -from konova.forms import RemoveModalForm +from konova.forms import RemoveModalForm, SimpleGeomForm +from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP +from konova.utils.user_checks import in_group @login_required @@ -63,8 +66,52 @@ def account_edit_view(request: HttpRequest, id: str): @login_required @any_group_check def account_open_view(request: HttpRequest, id: str): - # ToDo - pass + """ Renders a detail view for a compensation + + Args: + request (HttpRequest): The incoming request + id (str): The compensation's id + + Returns: + + """ + template = "compensation/detail/eco_account/view.html" + acc = get_object_or_404(EcoAccount, id=id) + geom_form = SimpleGeomForm(instance=acc) + _user = request.user + is_data_shared = acc.is_shared_with(_user) + + # Order states according to surface + before_states = acc.before_states.all().order_by("-surface") + after_states = acc.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 = 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) + + # Calculate rest of available surface for withdraws + withdraws = acc.withdraws.all() + withdraw_surfaces = withdraws.aggregate(Sum("surface"))["surface__sum"] or 0 + available = int(((sum_after_states - withdraw_surfaces) / sum_after_states) * 100) + + context = { + "obj": acc, + "geom_form": geom_form, + "has_access": is_data_shared, + "before_states": before_states, + "after_states": after_states, + "sum_before_states": sum_before_states, + "sum_after_states": sum_after_states, + "available": available, + "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) @login_required diff --git a/intervention/models.py b/intervention/models.py index 7a25ca8..bdafbb4 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -193,19 +193,6 @@ class Intervention(BaseObject): self.identifier = new_id super().save(*args, **kwargs) - def is_shared_with(self, user: User): - """ Access check - - Checks whether a given user has access to this intervention - - Args: - user (): - - Returns: - - """ - return self.users.filter(username=user.username).exists() - def check_validity(self) -> (bool, dict): """ Validity check diff --git a/intervention/templates/intervention/detail/includes/eco-account-withdraws.html b/intervention/templates/intervention/detail/includes/withdraws.html similarity index 93% rename from intervention/templates/intervention/detail/includes/eco-account-withdraws.html rename to intervention/templates/intervention/detail/includes/withdraws.html index 84a1bca..24199e4 100644 --- a/intervention/templates/intervention/detail/includes/eco-account-withdraws.html +++ b/intervention/templates/intervention/detail/includes/withdraws.html @@ -1,4 +1,4 @@ -{% load i18n l10n fontawesome_5 %} +{% load i18n l10n fontawesome_5 humanize %}
@@ -45,7 +45,7 @@ {{ withdraw.account.identifier }} -
+
{% trans 'Title' %}{{comp.title}}{{obj.title}}
{% trans 'compensates intervention' %} - - {{comp.intervention.identifier}} + + {{obj.intervention.identifier}}
{% trans 'Checked' %} - {% if comp.intervention.checked is None %} + {% if obj.intervention.checked is None %} {% fa5_icon 'star' 'far' %} {% else %} - + {% fa5_icon 'star' %} {% endif %} @@ -49,12 +49,12 @@
{% trans 'Recorded' %} - {% if comp.intervention.recorded is None %} + {% if obj.intervention.recorded is None %} {% fa5_icon 'bookmark' 'far' %} {% else %} - + {% fa5_icon 'bookmark' %} {% endif %} @@ -63,15 +63,15 @@
{% trans 'Last modified' %} - {{comp.created.timestamp|default_if_none:""|naturalday}} + {{obj.created.timestamp|default_if_none:""|naturalday}}
- {{comp.created.user.username}} + {{obj.created.user.username}}
{% trans 'Shared with' %} - {% for user in comp.intervention.users.all %} + {% for user in obj.intervention.users.all %} {% include 'user/includes/contact_modal_button.html' %} {% endfor %} {{ withdraw.amount|floatformat:2 }} %{{ withdraw.surface|floatformat:2|intcomma }} m² {% if is_default_member and has_access %}