RemoveModalForm refactoring
* wraps generic request processing logic into RemoveModalForm * adds delete action to intervention detail compensations.html * fixes rendering of deleted compensations in intervention detail view
This commit is contained in:
parent
98de05089e
commit
6bc8ada286
@ -61,8 +61,21 @@ def open_view(request: HttpRequest, id: str):
|
||||
|
||||
@login_required
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
# ToDo
|
||||
pass
|
||||
""" Renders a modal view for removing the compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, user=request.user)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Compensation removed"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@ -167,29 +180,10 @@ def payment_remove_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
payment = get_object_or_404(Payment, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
|
||||
template = form.template
|
||||
if request.method == "POST":
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(
|
||||
request,
|
||||
_("Payment removed")
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
else:
|
||||
messages.info(
|
||||
request,
|
||||
FORM_INVALID
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
elif request.method == "GET":
|
||||
context = {
|
||||
"form": form,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Payment removed"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@ -211,26 +205,7 @@ def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
|
||||
raise Http404("Unknown withdraw")
|
||||
|
||||
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
|
||||
template = form.template
|
||||
if request.method == "POST":
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(
|
||||
request,
|
||||
_("Withdraw removed")
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
else:
|
||||
messages.info(
|
||||
request,
|
||||
FORM_INVALID
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
elif request.method == "GET":
|
||||
context = {
|
||||
"form": form,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Withdraw removed")
|
||||
)
|
||||
|
@ -4,7 +4,7 @@
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<h5>
|
||||
<span class="badge badge-light">{{intervention.compensations.count}}</span>
|
||||
<span class="badge badge-light">{{compensations.count}}</span>
|
||||
{% trans 'Compensations' %}
|
||||
</h5>
|
||||
</div>
|
||||
@ -30,10 +30,13 @@
|
||||
<th scope="col">
|
||||
{% trans 'Title' %}
|
||||
</th>
|
||||
<th scope="col">
|
||||
{% trans 'Action' %}
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for comp in intervention.compensations.all %}
|
||||
{% for comp in compensations %}
|
||||
<tr>
|
||||
<td class="align-middle">
|
||||
<a href="{% url 'compensation:open' comp.id %}">
|
||||
@ -41,6 +44,11 @@
|
||||
</a>
|
||||
</td>
|
||||
<td class="align-middle">{{ comp.title }}</td>
|
||||
<td>
|
||||
<button data-form-url="{% url 'compensation:remove' comp.id %}" class="btn btn-default btn-modal" title="{% trans 'Remove compensation' %}">
|
||||
{% fa5_icon 'trash' %}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
@ -124,7 +124,13 @@ def open_view(request: HttpRequest, id: str):
|
||||
|
||||
"""
|
||||
template = "intervention/detail/view.html"
|
||||
|
||||
# Fetch data, filter out deleted related data
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
compensations = intervention.compensations.filter(
|
||||
deleted_on=None,
|
||||
deleted_by=None,
|
||||
)
|
||||
has_access = intervention.has_access(user=request.user)
|
||||
|
||||
geom_form = SimpleGeomForm(
|
||||
@ -133,6 +139,7 @@ def open_view(request: HttpRequest, id: str):
|
||||
|
||||
context = {
|
||||
"intervention": intervention,
|
||||
"compensations": compensations,
|
||||
"has_access": has_access,
|
||||
"geom_form": geom_form,
|
||||
}
|
||||
|
@ -10,14 +10,19 @@ from abc import abstractmethod
|
||||
|
||||
from bootstrap_modal_forms.forms import BSModalForm
|
||||
from django import forms
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.gis.forms import GeometryField, OSMWidget
|
||||
from django.contrib.gis.geos import Polygon
|
||||
from django.db import transaction
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
from konova.models import Document
|
||||
from konova.utils.message_templates import FORM_INVALID
|
||||
|
||||
|
||||
class BaseForm(forms.Form):
|
||||
@ -172,6 +177,43 @@ class RemoveModalForm(BaseModalForm):
|
||||
# If the class does not provide restorable delete functionality, we must delete the entry finally
|
||||
self.instance.delete()
|
||||
|
||||
def process_request(self, request: HttpRequest, msg_success: str = _("Object removed"), msg_error: str = FORM_INVALID):
|
||||
""" 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:
|
||||
|
||||
"""
|
||||
template = self.template
|
||||
if request.method == "POST":
|
||||
if self.is_valid():
|
||||
self.save()
|
||||
messages.success(
|
||||
request,
|
||||
msg_success
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
else:
|
||||
messages.info(
|
||||
request,
|
||||
msg_error
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
elif request.method == "GET":
|
||||
context = {
|
||||
"form": self,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class NewDocumentForm(BaseModalForm):
|
||||
""" Modal form for new documents
|
||||
|
@ -18,7 +18,6 @@ from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import RemoveModalForm
|
||||
from konova.models import Document
|
||||
from konova.utils.message_templates import FORM_INVALID
|
||||
from news.models import ServerMessage
|
||||
from konova.settings import SSO_SERVER_BASE
|
||||
|
||||
@ -139,26 +138,7 @@ def remove_document_view(request: HttpRequest, id: str):
|
||||
doc = get_object_or_404(Document, id=id)
|
||||
title = doc.title
|
||||
form = RemoveModalForm(request.POST or None, instance=doc, user=request.user)
|
||||
template = form.template
|
||||
if request.method == "POST":
|
||||
if form.is_valid():
|
||||
doc.delete()
|
||||
messages.success(
|
||||
request,
|
||||
_("Document '{}' deleted").format(title)
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
else:
|
||||
messages.info(
|
||||
request,
|
||||
FORM_INVALID
|
||||
)
|
||||
return redirect(request.META.get("HTTP_REFERER", "home"))
|
||||
elif request.method == "GET":
|
||||
context = {
|
||||
"form": form,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Document '{}' deleted").format(title)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user