EcoAccount withdraws
* adds functionality for withdraws from eco accounts in detail view of interventions and eco account as well * adds get_surface() method to AbstractCompensation class to provide a simple getter for a sql calculation * adds get_surface_withdraws() method to EcoAccount class to provide a simple getter for a sql calculation * renames some routes to match coherent rout naming * adds logic check on NewWithdrawForm * renames templates/table directory to templates/form, since there are form-table templates inside --> more clarity * adds new autocomplete routes to konova/urls.py for Interventions and EcoAccounts * adds/updates translations * adds/updates template comments * updates requirements.txt
This commit is contained in:
@@ -7,9 +7,8 @@ Created on: 17.11.20
|
||||
"""
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.gis.db import models
|
||||
from django.core.validators import MinValueValidator, MaxValueValidator
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db.models import Sum
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@@ -121,6 +120,14 @@ class AbstractCompensation(BaseObject):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def get_surface(self) -> float:
|
||||
""" Calculates the compensation's/account's surface
|
||||
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"]
|
||||
|
||||
|
||||
class Compensation(AbstractCompensation):
|
||||
"""
|
||||
@@ -188,6 +195,14 @@ class EcoAccount(AbstractCompensation):
|
||||
def __str__(self):
|
||||
return "{}".format(self.identifier)
|
||||
|
||||
def get_surface_withdraws(self) -> float:
|
||||
""" Calculates the compensation's/account's surface
|
||||
|
||||
Returns:
|
||||
sum_surface (float)
|
||||
"""
|
||||
return self.withdraws.all().aggregate(Sum("surface"))["surface__sum"]
|
||||
|
||||
|
||||
class EcoAccountWithdraw(BaseResource):
|
||||
"""
|
||||
|
||||
@@ -11,12 +11,10 @@
|
||||
<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>
|
||||
<button class="btn btn-outline-default btn-modal" data-form-url="{% url 'compensation:acc-new-withdraw' obj.id %}" title="{% trans 'Add new withdraw' %}">
|
||||
{% fa5_icon 'plus' %}
|
||||
{% fa5_icon 'tree' %}
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@@ -32,6 +30,9 @@
|
||||
<th scope="col">
|
||||
{% trans 'Amount' %}
|
||||
</th>
|
||||
<th scope="col">
|
||||
{% trans 'Created' %}
|
||||
</th>
|
||||
<th scope="col">
|
||||
{% trans 'Action' %}
|
||||
</th>
|
||||
@@ -46,6 +47,7 @@
|
||||
</a>
|
||||
</td>
|
||||
<td class="align-middle">{{ withdraw.surface|floatformat:2|intcomma }} m²</td>
|
||||
<td class="align-middle">{{ withdraw.created.timestamp|default_if_none:""|naturalday}}</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' %}">
|
||||
|
||||
@@ -36,6 +36,7 @@ urlaptterns_eco_acc = [
|
||||
|
||||
# Eco-account withdraws
|
||||
path('acc/<id>/remove/<withdraw_id>', eco_account_views.withdraw_remove_view, name='withdraw-remove'),
|
||||
path('acc/<id>/withdraw/new', eco_account_views.new_withdraw_view, name='acc-new-withdraw'),
|
||||
|
||||
]
|
||||
urlpatterns_compensation = [
|
||||
|
||||
@@ -16,6 +16,7 @@ from django.shortcuts import render, get_object_or_404
|
||||
from compensation.forms import NewStateModalForm, NewActionModalForm, NewDeadlineModalForm
|
||||
from compensation.models import EcoAccount
|
||||
from compensation.tables import EcoAccountTable
|
||||
from intervention.forms import NewWithdrawForm
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import any_group_check, default_group_required
|
||||
from konova.forms import RemoveModalForm, SimpleGeomForm, NewDocumentForm
|
||||
@@ -94,8 +95,7 @@ def open_view(request: HttpRequest, id: str):
|
||||
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
|
||||
withdraw_surfaces = acc.get_surface_withdraws() or 0
|
||||
available = int(((sum_after_states - withdraw_surfaces) / sum_after_states) * 100)
|
||||
|
||||
context = {
|
||||
@@ -259,4 +259,24 @@ def new_document_view(request: HttpRequest, id: str):
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Document added")
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_withdraw_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form view for creating withdraws
|
||||
|
||||
Args:
|
||||
request ():
|
||||
id ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewWithdrawForm(request.POST or None, instance=acc, user=request.user)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Withdraw added")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user