78 lines
2.2 KiB
Python
78 lines
2.2 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.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 ema.models import Ema
|
||
|
from intervention.forms.modals.share import ShareModalForm
|
||
|
from konova.decorators import conservation_office_group_required, shared_access_required
|
||
|
|
||
|
|
||
|
@login_required
|
||
|
def share_view(request: HttpRequest, id: str, token: str):
|
||
|
""" Performs sharing of an ema
|
||
|
|
||
|
If token given in url is not valid, the user will be redirected to the dashboard
|
||
|
|
||
|
Args:
|
||
|
request (HttpRequest): The incoming request
|
||
|
id (str): EMA's id
|
||
|
token (str): Access token for EMA
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
user = request.user
|
||
|
obj = get_object_or_404(Ema, id=id)
|
||
|
# Check tokens
|
||
|
if obj.access_token == token:
|
||
|
# Send different messages in case user has already been added to list of sharing users
|
||
|
if obj.is_shared_with(user):
|
||
|
messages.info(
|
||
|
request,
|
||
|
_("{} has already been shared with you").format(obj.identifier)
|
||
|
)
|
||
|
else:
|
||
|
messages.success(
|
||
|
request,
|
||
|
_("{} has been shared with you").format(obj.identifier)
|
||
|
)
|
||
|
obj.share_with_user(user)
|
||
|
return redirect("ema:detail", id=id)
|
||
|
else:
|
||
|
messages.error(
|
||
|
request,
|
||
|
_("Share link invalid"),
|
||
|
extra_tags="danger",
|
||
|
)
|
||
|
return redirect("home")
|
||
|
|
||
|
|
||
|
@login_required
|
||
|
@conservation_office_group_required
|
||
|
@shared_access_required(Ema, "id")
|
||
|
def create_share_view(request: HttpRequest, id: str):
|
||
|
""" Renders sharing form for an Ema
|
||
|
|
||
|
Args:
|
||
|
request (HttpRequest): The incoming request
|
||
|
id (str): Ema's id
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""
|
||
|
obj = get_object_or_404(Ema, id=id)
|
||
|
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
||
|
return form.process_request(
|
||
|
request,
|
||
|
msg_success=_("Share settings updated")
|
||
|
)
|