From 6bc8ada286674c20fd5ca7d86c99d73b7f26935d Mon Sep 17 00:00:00 2001 From: mipel Date: Mon, 2 Aug 2021 10:53:34 +0200 Subject: [PATCH] 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 --- compensation/views.py | 71 ++++++------------- .../detail/includes/compensations.html | 12 +++- intervention/views.py | 7 ++ konova/forms.py | 42 +++++++++++ konova/views.py | 28 ++------ 5 files changed, 86 insertions(+), 74 deletions(-) diff --git a/compensation/views.py b/compensation/views.py index 88f9b929..c0b55a34 100644 --- a/compensation/views.py +++ b/compensation/views.py @@ -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") + ) diff --git a/intervention/templates/intervention/detail/includes/compensations.html b/intervention/templates/intervention/detail/includes/compensations.html index 3703ac0d..21726b25 100644 --- a/intervention/templates/intervention/detail/includes/compensations.html +++ b/intervention/templates/intervention/detail/includes/compensations.html @@ -4,7 +4,7 @@
- {{intervention.compensations.count}} + {{compensations.count}} {% trans 'Compensations' %}
@@ -30,10 +30,13 @@ {% trans 'Title' %} + + {% trans 'Action' %} + - {% for comp in intervention.compensations.all %} + {% for comp in compensations %} @@ -41,6 +44,11 @@ {{ comp.title }} + + + {% endfor %} diff --git a/intervention/views.py b/intervention/views.py index 473cb2f4..e982b8be 100644 --- a/intervention/views.py +++ b/intervention/views.py @@ -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, } diff --git a/konova/forms.py b/konova/forms.py index 543f61ad..16bc8d47 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -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 diff --git a/konova/views.py b/konova/views.py index 28ad4f93..8f53aa99 100644 --- a/konova/views.py +++ b/konova/views.py @@ -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 \ No newline at end of file + return form.process_request( + request=request, + msg_success=_("Document '{}' deleted").format(title) + )