konova/intervention/views.py

172 lines
5.2 KiB
Python
Raw Normal View History

2021-07-01 13:36:07 +02:00
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, redirect
from django.urls import reverse
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
from process.models import Process
@login_required
2021-07-01 14:38:57 +02:00
2021-07-01 13:36:07 +02:00
def index_view(request: HttpRequest):
"""
Renders the index view for process
Args:
request (HttpRequest): The incoming request
Returns:
A rendered view
"""
template = "generic_index.html"
user = request.user
2021-07-01 14:38:57 +02:00
interventions = Intervention # ToDo
2021-07-01 13:36:07 +02:00
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)
if intervention.process is None:
# An intervention can not be created without a process -> automatically create a new process
process = Process.create_from_intervention(intervention)
messages.info(request, _("Interventions must be part of a process. Please fill in the missing data for the process"))
return redirect("process:edit", id=process.id)
else:
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 process
Args:
request (HttpRequest): The incoming request
id (str): The uuid id as string
Returns:
"""
template = "konova/form.html"
# Since an intervention is always organized inside a process, we will call the process removing routine, which
# disables all related elements by default
obj = get_object_or_404(Intervention, id=id)
process = obj.process
if request.method == "POST":
form = RemoveForm(
request.POST or None,
object_to_remove=obj,
remove_post_url=reverse("process:remove", args=(process.id,)),
cancel_url=reverse("intervention:index"),
)
if form.is_valid():
confirmed = form.is_checked()
if confirmed:
process.deactivate()
messages.success(request, _("Intervention {} removed").format(obj))
return redirect("intervention:index")
else:
messages.error(request, _("Invalid input"))
form = RemoveForm(
object_to_remove=obj,
remove_post_url=reverse("process:remove", args=(process.id,)),
cancel_url=reverse("intervention:index"),
)
context = {
"form": form,
}
context = BaseContext(request, context).context
return render(request, template, context)