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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user