d139119a15
* adds user access relation to certain models * adds pagination to tables * adds checked_on/_by attributes to intervention model * adds custom column rendering for checked and registered columns * adds first simple index filtering of default interventions for user * adds translations
151 lines
4.1 KiB
Python
151 lines
4.1 KiB
Python
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
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, OpenInterventionForm
|
|
from intervention.models import Intervention
|
|
from intervention.tables import InterventionTable
|
|
from konova.contexts import BaseContext
|
|
from konova.decorators import *
|
|
from konova.forms import RemoveForm
|
|
|
|
|
|
@login_required
|
|
def index_view(request: HttpRequest):
|
|
"""
|
|
Renders the index view for Interventions
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
A rendered view
|
|
"""
|
|
template = "generic_index.html"
|
|
user = request.user
|
|
interventions = Intervention.objects.filter(
|
|
deleted_on=None, # not deleted
|
|
next_version=None, # only newest versions
|
|
users__in=[user], # requesting user has access
|
|
)
|
|
table = InterventionTable(
|
|
request=request,
|
|
queryset=interventions
|
|
)
|
|
context = {
|
|
"table": table,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def new_view(request: HttpRequest):
|
|
"""
|
|
Renders a view for a new intervention creation
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "konova/form.html"
|
|
form = NewInterventionForm(request.POST or None)
|
|
if request.method == "POST":
|
|
if form.is_valid():
|
|
intervention = form.save(request.user)
|
|
messages.success(request, _("Intervention {} added").format(intervention.title))
|
|
return redirect("intervention:index")
|
|
else:
|
|
messages.error(request, _("Invalid input"))
|
|
else:
|
|
# For clarification: nothing in this case
|
|
pass
|
|
context = {
|
|
"form": form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def open_view(request: HttpRequest, id: str):
|
|
""" Renders a view for viewing an intervention's data
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The intervention's id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "intervention/open.html"
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
form = OpenInterventionForm(instance=intervention)
|
|
context = {
|
|
"intervention": intervention,
|
|
"form": form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def edit_view(request: HttpRequest, id: str):
|
|
"""
|
|
Renders a view for editing interventions
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "konova/form.html"
|
|
intervention = get_object_or_404(Intervention, id=id)
|
|
if request.method == "POST":
|
|
form = EditInterventionForm(request.POST or None, instance=intervention)
|
|
if form.is_valid():
|
|
intervention = form.save(request.user)
|
|
messages.success(request, _("{} edited").format(intervention))
|
|
return redirect("intervention:index")
|
|
else:
|
|
messages.error(request, _("Invalid input"))
|
|
form = EditInterventionForm(instance=intervention)
|
|
context = {
|
|
"form": form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def remove_view(request: HttpRequest, id: str):
|
|
""" Renders a remove view for this intervention
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The uuid id as string
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "konova/form.html"
|
|
obj = Intervention.objects.get(id=id)
|
|
|
|
# ToDo
|
|
|
|
form = RemoveForm(
|
|
object_to_remove=obj,
|
|
remove_post_url=reverse("intervention:remove", args=(id,)),
|
|
cancel_url=reverse("intervention:index"),
|
|
)
|
|
context = {
|
|
"form": form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|