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
This commit is contained in:
mipel
2021-08-09 14:39:36 +02:00
parent 3dc2af4b7c
commit d27a39f6d9
20 changed files with 364 additions and 127 deletions

View File

@@ -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):

View File

@@ -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})

View File

@@ -0,0 +1,61 @@
{% load i18n l10n fontawesome_5 humanize %}
<div id="eco-account-withdraws" class="card">
<div class="card-header rlp-r">
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{obj.withdraws.count}}</span>
{% trans 'Eco Account Withdraws' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<a href="{% url 'compensation:new' %}" title="{% trans 'Add new withdraw' %}">
<button class="btn btn-outline-default">
{% fa5_icon 'plus' %}
{% fa5_icon 'tree' %}
</button>
</a>
{% endif %}
</div>
</div>
</div>
</div>
<div class="card-body scroll-300">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">
{% trans 'Intervention Identifier' %}
</th>
<th scope="col">
{% trans 'Amount' %}
</th>
<th scope="col">
{% trans 'Action' %}
</th>
</tr>
</thead>
<tbody>
{% for withdraw in obj.withdraws.all %}
<tr>
<td class="align-middle">
<a href="{% url 'intervention:open' withdraw.intervention.id %}">
{{ withdraw.intervention.identifier }}
</a>
</td>
<td class="align-middle">{{ withdraw.surface|floatformat:2|intcomma }} m²</td>
<td>
{% if is_default_member and has_access %}
<button data-form-url="{% url 'compensation:withdraw-remove' withdraw.account.id withdraw.id %}" class="btn btn-default btn-modal" title="{% trans 'Remove Withdraw' %}">
{% fa5_icon 'trash' %}
</button>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

View File

@@ -0,0 +1,108 @@
{% extends 'base.html' %}
{% load i18n l10n static fontawesome_5 humanize %}
{% block head %}
{% endblock %}
{% block body %}
<div id="detail-header" class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<h3>{% trans 'Eco-account' %} {{obj.identifier}}</h3>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/controls.html' %}
</div>
</div>
<hr>
<div id="data" class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="table-container">
<table class="table table-hover">
<tr>
<th class="w-25" scope="row">{% trans 'Title' %}</th>
<td class="align-middle">{{obj.title}}</td>
</tr>
<tr>
<th scope="row">{% trans 'Available' %}</th>
<td class="align-middle">
{% with available as value %}
{% include 'konova/custom_widgets/progressbar.html' %}
{% endwith %}
</td>
</tr>
<tr>
<th scope="row">{% trans 'Recorded' %}</th>
<td class="align-middle">
{% if obj.recorded is None %}
<span title="{% trans 'Not recorded yet' %}">
{% fa5_icon 'bookmark' 'far' %}
</span>
{% else %}
<span class="registered-bookmark" title="{% trans 'Recorded on '%} {{obj.recorded.timestamp}} {% trans 'by' %} {{obj.recorded.user}}">
{% fa5_icon 'bookmark' %}
</span>
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans 'Last modified' %}</th>
<td class="align-middle">
{{obj.created.timestamp|default_if_none:""|naturalday}}
<br>
{{obj.created.user.username}}
</td>
</tr>
<tr>
<th scope="row">{% trans 'Shared with' %}</th>
<td class="align-middle">
{% for user in obj.users.all %}
{% include 'user/includes/contact_modal_button.html' %}
{% endfor %}
</td>
</tr>
</table>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% if geom_form.area == 0 %}
<div class="alert alert-info">{% trans 'No geometry added, yet.' %}</div>
{% endif %}
{{geom_form.media}}
{{geom_form.geom}}
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/states-before.html' %}
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/states-after.html' %}
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/actions.html' %}
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/deadlines.html' %}
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/documents.html' %}
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/eco_account/includes/withdraws.html' %}
</div>
</div>
{% with 'btn-modal' as btn_class %}
{% include 'modal/modal_form_script.html' %}
{% endwith %}
{% endblock %}

View File

@@ -4,14 +4,14 @@
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{comp.actions.count}}</span>
<span class="badge badge-light">{{obj.actions.count}}</span>
{% trans 'Actions' context 'Compensation' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-action' comp.id %}" title="{% trans 'Add new action' %}">
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-action' obj.id %}" title="{% trans 'Add new action' %}">
{% fa5_icon 'plus' %}
{% fa5_icon 'seedling' %}
</button>
@@ -39,7 +39,7 @@
</tr>
</thead>
<tbody>
{% for action in comp.actions.all %}
{% for action in obj.actions.all %}
<tr>
<td class="align-middle">
{{ action.action_type }}

View File

@@ -18,10 +18,10 @@
{% fa5_icon 'edit' %}
</button>
</a>
<button class="btn btn-default btn-modal mr-2" data-form-url="{% url 'compensation:log' comp.id %}" title="{% trans 'Show log' %}">
<button class="btn btn-default btn-modal mr-2" data-form-url="{% url 'compensation:log' obj.id %}" title="{% trans 'Show log' %}">
{% fa5_icon 'history' %}
</button>
<button class="btn btn-default btn-modal" data-form-url="{% url 'compensation:remove' comp.id %}" title="{% trans 'Delete' %}">
<button class="btn btn-default btn-modal" data-form-url="{% url 'compensation:remove' obj.id %}" title="{% trans 'Delete' %}">
{% fa5_icon 'trash' %}
</button>
{% endif %}

View File

@@ -4,14 +4,14 @@
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{comp.deadlines.count}}</span>
<span class="badge badge-light">{{obj.deadlines.count}}</span>
{% trans 'Deadlines' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-deadline' comp.id %}" title="{% trans 'Add new deadline' %}">
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-deadline' obj.id %}" title="{% trans 'Add new deadline' %}">
{% fa5_icon 'plus' %}
{% fa5_icon 'calendar-check' %}
</button>
@@ -39,7 +39,7 @@
</tr>
</thead>
<tbody>
{% for deadline in comp.deadlines.all %}
{% for deadline in obj.deadlines.all %}
<tr>
<td class="align-middle">
{% trans deadline.type_humanized %}

View File

@@ -4,14 +4,14 @@
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{comp.documents.count}}</span>
<span class="badge badge-light">{{obj.documents.count}}</span>
{% trans 'Documents' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-doc' comp.id %}" title="{% trans 'Add new document' %}">
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-doc' obj.id %}" title="{% trans 'Add new document' %}">
{% fa5_icon 'plus' %}
{% fa5_icon 'file' %}
</button>
@@ -36,7 +36,7 @@
</tr>
</thead>
<tbody>
{% for doc in comp.documents.all %}
{% for doc in obj.documents.all %}
<tr>
<td class="align-middle">
<a href="{% url 'doc-open' doc.id %}">

View File

@@ -4,14 +4,14 @@
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{comp.after_states.count}}</span>
<span class="badge badge-light">{{obj.after_states.count}}</span>
{% trans 'States after' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-state' comp.id %}" title="{% trans 'Add new state after' %}">
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-state' obj.id %}" title="{% trans 'Add new state after' %}">
{% fa5_icon 'plus' %}
{% fa5_icon 'layer-group' %}
</button>

View File

@@ -4,14 +4,14 @@
<div class="row">
<div class="col-sm-6">
<h5>
<span class="badge badge-light">{{comp.before_states.count}}</span>
<span class="badge badge-light">{{obj.before_states.count}}</span>
{% trans 'States before' %}
</h5>
</div>
<div class="col-sm-6">
<div class="d-flex justify-content-end">
{% if is_default_member and has_access %}
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-state' comp.id %}?before=true" title="{% trans 'Add new state before' %}">
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:new-state' obj.id %}?before=true" title="{% trans 'Add new state before' %}">
{% fa5_icon 'plus' %}
{% fa5_icon 'layer-group' %}
</button>

View File

@@ -9,7 +9,7 @@
<div id="detail-header" class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<h3>{% trans 'Compensation' %} {{comp.identifier}}</h3>
<h3>{% trans 'Compensation' %} {{obj.identifier}}</h3>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
{% include 'compensation/detail/includes/controls.html' %}
@@ -22,25 +22,25 @@
<table class="table table-hover">
<tr>
<th class="w-25" scope="row">{% trans 'Title' %}</th>
<td class="align-middle">{{comp.title}}</td>
<td class="align-middle">{{obj.title}}</td>
</tr>
<tr>
<th scope="row">{% trans 'compensates intervention' %}</th>
<td class="align-middle">
<a href="{% url 'intervention:open' comp.intervention.id %}">
{{comp.intervention.identifier}}
<a href="{% url 'intervention:open' obj.intervention.id %}">
{{obj.intervention.identifier}}
</a>
</td>
</tr>
<tr>
<th scope="row">{% trans 'Checked' %}</th>
<td class="align-middle">
{% if comp.intervention.checked is None %}
{% if obj.intervention.checked is None %}
<span>
{% fa5_icon 'star' 'far' %}
</span>
{% else %}
<span class="check-star" title="{% trans 'Checked on '%} {{comp.intervention.checked.timestamp}} {% trans 'by' %} {{comp.intervention.checked.user}}">
<span class="check-star" title="{% trans 'Checked on '%} {{obj.intervention.checked.timestamp}} {% trans 'by' %} {{obj.intervention.checked.user}}">
{% fa5_icon 'star' %}
</span>
{% endif %}
@@ -49,12 +49,12 @@
<tr>
<th scope="row">{% trans 'Recorded' %}</th>
<td class="align-middle">
{% if comp.intervention.recorded is None %}
{% if obj.intervention.recorded is None %}
<span title="{% trans 'Not recorded yet' %}">
{% fa5_icon 'bookmark' 'far' %}
</span>
{% else %}
<span class="registered-bookmark" title="{% trans 'Recorded on '%} {{comp.intervention.recorded.timestamp}} {% trans 'by' %} {{comp.intervention.recorded.user}}">
<span class="registered-bookmark" title="{% trans 'Recorded on '%} {{obj.intervention.recorded.timestamp}} {% trans 'by' %} {{obj.intervention.recorded.user}}">
{% fa5_icon 'bookmark' %}
</span>
{% endif %}
@@ -63,15 +63,15 @@
<tr>
<th scope="row">{% trans 'Last modified' %}</th>
<td class="align-middle">
{{comp.created.timestamp|default_if_none:""|naturalday}}
{{obj.created.timestamp|default_if_none:""|naturalday}}
<br>
{{comp.created.user.username}}
{{obj.created.user.username}}
</td>
</tr>
<tr>
<th scope="row">{% trans 'Shared with' %}</th>
<td class="align-middle">
{% for user in comp.intervention.users.all %}
{% for user in obj.intervention.users.all %}
{% include 'user/includes/contact_modal_button.html' %}
{% endfor %}
</td>

View File

@@ -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

View File

@@ -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