* refactors views for adding, editing and removing revocations * refactors view for getting the document of a revocation * updates tests
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: ksp-servicestelle@sgdnord.rlp.de
|
|
Created on: 19.08.22
|
|
|
|
"""
|
|
from django.contrib import messages
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.http import HttpRequest
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from intervention.forms.modals.revocation import NewRevocationModalForm, EditRevocationModalForm, \
|
|
RemoveRevocationModalForm
|
|
from intervention.models import Intervention, RevocationDocument
|
|
from konova.utils.documents import get_document
|
|
from konova.utils.message_templates import DATA_UNSHARED, REVOCATION_EDITED, REVOCATION_REMOVED, REVOCATION_ADDED
|
|
from konova.views.base import BaseModalFormView, BaseView
|
|
|
|
|
|
class BaseRevocationView(LoginRequiredMixin, BaseModalFormView):
|
|
_MODEL_CLS = Intervention
|
|
_REDIRECT_URL = "intervention:detail"
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def _user_has_permission(self, user):
|
|
return user.is_default_user()
|
|
|
|
def _get_redirect_url(self, *args, **kwargs):
|
|
url = super()._get_redirect_url(*args, **kwargs)
|
|
return f"{url}#related_data"
|
|
|
|
|
|
class NewRevocationView(BaseRevocationView):
|
|
_FORM_CLS = NewRevocationModalForm
|
|
_MSG_SUCCESS = REVOCATION_ADDED
|
|
|
|
|
|
class EditRevocationView(BaseRevocationView):
|
|
_FORM_CLS = EditRevocationModalForm
|
|
_MSG_SUCCESS = REVOCATION_EDITED
|
|
|
|
|
|
class RemoveRevocationView(BaseRevocationView):
|
|
_FORM_CLS = RemoveRevocationModalForm
|
|
_MSG_SUCCESS = REVOCATION_REMOVED
|
|
|
|
|
|
class GetRevocationDocumentView(LoginRequiredMixin, BaseView):
|
|
_MODEL_CLS = RevocationDocument
|
|
_REDIRECT_URL = "intervention:detail"
|
|
|
|
def get(self, request: HttpRequest, doc_id: str):
|
|
doc = get_object_or_404(RevocationDocument, id=doc_id)
|
|
# File download only possible if related instance is shared with user
|
|
if not doc.instance.legal.intervention.users.filter(id=request.user.id):
|
|
messages.info(
|
|
request,
|
|
DATA_UNSHARED
|
|
)
|
|
return redirect("intervention:detail", id=doc.instance.id)
|
|
return get_document(doc)
|
|
|
|
def _user_has_permission(self, user):
|
|
return user.is_default_user()
|
|
|
|
def _user_has_shared_access(self, user, **kwargs):
|
|
obj = get_object_or_404(self._MODEL_CLS, id=kwargs.get("doc_id"))
|
|
assert obj is not None
|
|
return obj.instance.intervention.is_shared_with(user)
|
|
|
|
def _get_redirect_url(self, *args, **kwargs):
|
|
url = super()._get_redirect_url(*args, **kwargs)
|
|
return f"{url}#related_data"
|