Share views

* replaces function based share views with class based
* improves team-share autocomplete search
* renames internal share url names
This commit is contained in:
2022-08-22 10:58:07 +02:00
parent a16f68012d
commit a4de394a54
17 changed files with 158 additions and 204 deletions

View File

@@ -5,74 +5,28 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22
"""
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _
from django.utils.decorators import method_decorator
from intervention.forms.modals.share import ShareModalForm
from intervention.models import Intervention
from konova.decorators import default_group_required, shared_access_required
from konova.views.share import AbstractShareByTokenView, AbstractShareFormView
@login_required
def share_view(request: HttpRequest, id: str, token: str):
""" Performs sharing of an intervention
class InterventionShareByTokenView(AbstractShareByTokenView):
model = Intervention
redirect_url = "intervention:detail"
If token given in url is not valid, the user will be redirected to the dashboard
Args:
request (HttpRequest): The incoming request
id (str): Intervention's id
token (str): Access token for intervention
Returns:
"""
user = request.user
intervention = get_object_or_404(Intervention, id=id)
# Check tokens
if intervention.access_token == token:
# Send different messages in case user has already been added to list of sharing users
if intervention.is_shared_with(user):
messages.info(
request,
_("{} has already been shared with you").format(intervention.identifier)
)
else:
messages.success(
request,
_("{} has been shared with you").format(intervention.identifier)
)
intervention.share_with_user(user)
return redirect("intervention:detail", id=id)
else:
messages.error(
request,
_("Share link invalid"),
extra_tags="danger",
)
return redirect("home")
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def create_share_view(request: HttpRequest, id: str):
""" Renders sharing form for an intervention
Args:
request (HttpRequest): The incoming request
id (str): Intervention's id
Returns:
"""
intervention = get_object_or_404(Intervention, id=id)
form = ShareModalForm(request.POST or None, instance=intervention, request=request)
return form.process_request(
request,
msg_success=_("Share settings updated")
)
class InterventionShareFormView(AbstractShareFormView):
model = Intervention
@method_decorator(login_required)
@method_decorator(default_group_required)
@method_decorator(shared_access_required(Intervention, "id"))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)