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
6a650d2021
commit
92cacd7aaa
@ -61,8 +61,21 @@ def open_view(request: HttpRequest, id: str):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def remove_view(request: HttpRequest, id: str):
|
def remove_view(request: HttpRequest, id: str):
|
||||||
# ToDo
|
""" Renders a modal view for removing the compensation
|
||||||
pass
|
|
||||||
|
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
|
@login_required
|
||||||
@ -167,29 +180,10 @@ def payment_remove_view(request: HttpRequest, id: str):
|
|||||||
"""
|
"""
|
||||||
payment = get_object_or_404(Payment, id=id)
|
payment = get_object_or_404(Payment, id=id)
|
||||||
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
|
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
|
||||||
template = form.template
|
return form.process_request(
|
||||||
if request.method == "POST":
|
request=request,
|
||||||
if form.is_valid():
|
msg_success=_("Payment removed"),
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@ -211,26 +205,7 @@ def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
|
|||||||
raise Http404("Unknown withdraw")
|
raise Http404("Unknown withdraw")
|
||||||
|
|
||||||
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
|
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
|
||||||
template = form.template
|
return form.process_request(
|
||||||
if request.method == "POST":
|
request=request,
|
||||||
if form.is_valid():
|
msg_success=_("Withdraw removed")
|
||||||
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
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-6">
|
||||||
<h5>
|
<h5>
|
||||||
<span class="badge badge-light">{{intervention.compensations.count}}</span>
|
<span class="badge badge-light">{{compensations.count}}</span>
|
||||||
{% trans 'Compensations' %}
|
{% trans 'Compensations' %}
|
||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
@ -30,10 +30,13 @@
|
|||||||
<th scope="col">
|
<th scope="col">
|
||||||
{% trans 'Title' %}
|
{% trans 'Title' %}
|
||||||
</th>
|
</th>
|
||||||
|
<th scope="col">
|
||||||
|
{% trans 'Action' %}
|
||||||
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for comp in intervention.compensations.all %}
|
{% for comp in compensations %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="align-middle">
|
<td class="align-middle">
|
||||||
<a href="{% url 'compensation:open' comp.id %}">
|
<a href="{% url 'compensation:open' comp.id %}">
|
||||||
@ -41,6 +44,11 @@
|
|||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td class="align-middle">{{ comp.title }}</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>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@ -124,7 +124,13 @@ def open_view(request: HttpRequest, id: str):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
template = "intervention/detail/view.html"
|
template = "intervention/detail/view.html"
|
||||||
|
|
||||||
|
# Fetch data, filter out deleted related data
|
||||||
intervention = get_object_or_404(Intervention, id=id)
|
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)
|
has_access = intervention.has_access(user=request.user)
|
||||||
|
|
||||||
geom_form = SimpleGeomForm(
|
geom_form = SimpleGeomForm(
|
||||||
@ -133,6 +139,7 @@ def open_view(request: HttpRequest, id: str):
|
|||||||
|
|
||||||
context = {
|
context = {
|
||||||
"intervention": intervention,
|
"intervention": intervention,
|
||||||
|
"compensations": compensations,
|
||||||
"has_access": has_access,
|
"has_access": has_access,
|
||||||
"geom_form": geom_form,
|
"geom_form": geom_form,
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,19 @@ from abc import abstractmethod
|
|||||||
|
|
||||||
from bootstrap_modal_forms.forms import BSModalForm
|
from bootstrap_modal_forms.forms import BSModalForm
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.contrib import messages
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.gis.forms import GeometryField, OSMWidget
|
from django.contrib.gis.forms import GeometryField, OSMWidget
|
||||||
from django.contrib.gis.geos import Polygon
|
from django.contrib.gis.geos import Polygon
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.http import HttpRequest
|
||||||
|
from django.shortcuts import redirect, render
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from konova.contexts import BaseContext
|
||||||
from konova.models import Document
|
from konova.models import Document
|
||||||
|
from konova.utils.message_templates import FORM_INVALID
|
||||||
|
|
||||||
|
|
||||||
class BaseForm(forms.Form):
|
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
|
# If the class does not provide restorable delete functionality, we must delete the entry finally
|
||||||
self.instance.delete()
|
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):
|
class NewDocumentForm(BaseModalForm):
|
||||||
""" Modal form for new documents
|
""" Modal form for new documents
|
||||||
|
@ -18,7 +18,6 @@ from intervention.models import Intervention
|
|||||||
from konova.contexts import BaseContext
|
from konova.contexts import BaseContext
|
||||||
from konova.forms import RemoveModalForm
|
from konova.forms import RemoveModalForm
|
||||||
from konova.models import Document
|
from konova.models import Document
|
||||||
from konova.utils.message_templates import FORM_INVALID
|
|
||||||
from news.models import ServerMessage
|
from news.models import ServerMessage
|
||||||
from konova.settings import SSO_SERVER_BASE
|
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)
|
doc = get_object_or_404(Document, id=id)
|
||||||
title = doc.title
|
title = doc.title
|
||||||
form = RemoveModalForm(request.POST or None, instance=doc, user=request.user)
|
form = RemoveModalForm(request.POST or None, instance=doc, user=request.user)
|
||||||
template = form.template
|
return form.process_request(
|
||||||
if request.method == "POST":
|
request=request,
|
||||||
if form.is_valid():
|
msg_success=_("Document '{}' deleted").format(title)
|
||||||
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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user