Access url for interventions
* adds access_token as new attribute * adds generating of access_token * adds new form * adds two new routes for sharing an intervention * adds translation * adds render_submit to BaseModalForm which triggers rendering the modal footer
This commit is contained in:
@@ -4,7 +4,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
|
||||
from intervention.forms import NewInterventionForm, EditInterventionForm
|
||||
from intervention.forms import NewInterventionForm, EditInterventionForm, ShareInterventionForm
|
||||
from intervention.models import Intervention
|
||||
from intervention.tables import InterventionTable
|
||||
from konova.contexts import BaseContext
|
||||
@@ -198,3 +198,68 @@ def remove_view(request: HttpRequest, id: str):
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an intervention
|
||||
|
||||
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.has_access(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.users.add(user)
|
||||
return redirect("intervention:open", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
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:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = ShareInterventionForm(request.POST or None, instance=intervention, request=request)
|
||||
if request.method != "GET":
|
||||
raise NotImplementedError
|
||||
context = {
|
||||
"form": form,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, form.template, context)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user