Documents removing

* adds generic modal rendering using package django-bootstrap-modal-forms
* adds document file removing from hard drive
* adds translations
This commit is contained in:
mipel
2021-07-23 18:27:53 +02:00
parent df8b62de22
commit 76c7cce9bc
13 changed files with 202 additions and 61 deletions

View File

@@ -23,6 +23,7 @@ class BaseContext:
"user": None,
"current_role": None,
"help_link": HELP_LINK,
"modal_reload_page": "true", # must be string and lower case true, since it's used in JS code
}
def __init__(self, request: HttpRequest, additional_context: dict = {}):

View File

@@ -8,10 +8,12 @@ Created on: 16.11.20
from abc import abstractmethod
from bootstrap_modal_forms.forms import BSModalModelForm, BSModalForm
from django import forms
from django.contrib.auth.models import User
from django.contrib.gis.forms import GeometryField, OSMWidget
from django.contrib.gis.geos import Polygon
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@@ -127,3 +129,18 @@ class SimpleGeomForm(BaseForm):
self.fields["geom"].widget.attrs["default_zoom"] = 1
self.initialize_form_field("geom", geom)
self.area = geom.area
class RemoveDocumentForm(BaseForm, BSModalForm):
confirm = forms.BooleanField(
label=_("Confirm"),
label_suffix=_(""),
widget=forms.CheckboxInput(),
required=True,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.instance = kwargs.get("instance", None)
self.form_title = _("Remove document")
self.form_caption = _("This will remove '{}'. Are you sure?").format(self.instance.title)

View File

@@ -5,6 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 17.11.20
"""
import os
import uuid
from django.contrib.auth.models import User
@@ -65,6 +66,19 @@ class Document(BaseResource):
document = models.FileField()
comment = models.TextField()
def delete(self, using=None, keep_parents=False):
""" Custom delete function to remove the real file from the hard drive
Args:
using ():
keep_parents ():
Returns:
"""
os.remove(self.document.file.name)
super().delete(using=using, keep_parents=keep_parents)
class Geometry(BaseResource):
"""

View File

@@ -55,6 +55,7 @@ INSTALLED_APPS = [
'django.contrib.gis',
'simple_sso.sso_server',
'django_tables2',
'bootstrap_modal_forms',
'fontawesome_5',
'bootstrap4',
'konova',

View File

@@ -38,7 +38,7 @@ urlpatterns = [
# Documents
path('document/<id>', get_document_view, name="doc-open"),
path('document/new', new_document_view, name="doc-new"),
path('document/<id>/remove>', remove_document_view, name="doc-remove"),
path('document/<id>/remove', remove_document_view, name="doc-remove"),
# Autocomplete paths
path("atcmplt/orgs", OrganisationAutocomplete.as_view(), name="orgs-autocomplete"),

View File

@@ -14,6 +14,7 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from konova.contexts import BaseContext
from konova.forms import RemoveDocumentForm
from konova.models import Document
from news.models import ServerMessage
from konova.settings import SSO_SERVER_BASE
@@ -104,17 +105,43 @@ def new_document_view(request: HttpRequest):
def remove_document_view(request: HttpRequest, id: str):
""" Renders a form for uploading new documents
This function works using a modal. We are not using the regular way, the django bootstrap modal forms are
intended to be used. Instead of View classes we work using the classic way of dealing with forms (see below).
It is important to mention, that modal forms, which should reload the page afterwards, must provide a
'reload_page' bool in the context. This way, the modal may reload the page or not.
For further details see the comments in templates/modal or
https://github.com/trco/django-bootstrap-modal-forms
Args:
request (HttpRequest): The incoming request
Returns:
"""
template = "modal/modal_form.html"
doc = get_object_or_404(Document, id=id)
title = doc.title
#doc.delete()
messages.success(
request,
_("Document '{}' deleted").format(title)
)
return redirect(request.META.get("HTTP_REFERER", "home"))
form = RemoveDocumentForm(request.POST or None, instance=doc)
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,
_("There was an error on this form.")
)
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