218 lines
6.3 KiB
Python
218 lines
6.3 KiB
Python
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import HttpRequest
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from compensation.models import Compensation
|
|
from konova.contexts import BaseContext
|
|
from konova.decorators import resolve_user_role, valid_process_role_required
|
|
from konova.forms import RemoveForm
|
|
from konova.utils.tables import ChoicesColumnForm
|
|
from process.enums import ProcessStateEnum
|
|
from process.forms import NewProcessForm, EditProcessForm
|
|
from process.models import Process
|
|
from process.settings import PROCESS_STATE_STRINGS
|
|
from process.tables import ProcessTable
|
|
|
|
# URL definitions
|
|
PROCESS_INDEX_URL = "process:index"
|
|
|
|
|
|
@login_required
|
|
@resolve_user_role
|
|
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
|
|
processes = Process.get_role_objects(user)
|
|
table = ProcessTable(
|
|
request=request,
|
|
queryset=processes
|
|
)
|
|
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 process creation
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "konova/form.html"
|
|
form = NewProcessForm(request.POST or None)
|
|
if request.method == "POST":
|
|
if form.is_valid():
|
|
process = form.save(request.user)
|
|
intervention = process.intervention
|
|
messages.info(request, _("A process is based on an intervention. Please fill in the missing data for this intervention"))
|
|
return redirect("intervention:edit", id=intervention.id)
|
|
else:
|
|
messages.error(request, _("Invalid input"))
|
|
else:
|
|
# For clarification: Nothing to do here
|
|
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 detail view for this process
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The uuid id as string
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "process/open.html"
|
|
process = get_object_or_404(Process, id=id)
|
|
context = {
|
|
"process": process,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
@resolve_user_role
|
|
def edit_view(request: HttpRequest, id: str):
|
|
""" Renders an edit view for this process
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The uuid id as string
|
|
|
|
Returns:
|
|
|
|
"""
|
|
template = "konova/form.html"
|
|
process = get_object_or_404(Process, id=id)
|
|
if request.method == "POST":
|
|
form = EditProcessForm(request.POST or None, instance=process, user=request.user)
|
|
if form.is_valid():
|
|
process = form.save(request.user)
|
|
messages.success(request, _("{} edited").format(process))
|
|
return redirect("process:index")
|
|
else:
|
|
messages.error(request, _("Invalid input"))
|
|
form = EditProcessForm(instance=process, user=request.user)
|
|
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"
|
|
process = get_object_or_404(Process, id=id)
|
|
if request.method == "POST":
|
|
form = RemoveForm(
|
|
request.POST or None,
|
|
object_to_remove=process,
|
|
remove_post_url=reverse("process:remove", args=(id,)),
|
|
cancel_url=reverse("process:index"),
|
|
)
|
|
if form.is_valid():
|
|
confirmed = form.is_checked()
|
|
if confirmed:
|
|
process.deactivate()
|
|
messages.success(request, _("Process {} removed").format(process))
|
|
return redirect("process:index")
|
|
else:
|
|
messages.error(request, _("Invalid input"))
|
|
|
|
form = RemoveForm(
|
|
object_to_remove=process,
|
|
remove_post_url=reverse("process:remove", args=(id,)),
|
|
cancel_url=reverse("process:index"),
|
|
)
|
|
context = {
|
|
"form": form,
|
|
}
|
|
context = BaseContext(request, context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def edit_status_view(request: HttpRequest, id: str):
|
|
""" Changes only the status of a process
|
|
|
|
Args:
|
|
request (The incoming request):
|
|
id ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
process = get_object_or_404(Process, id=id)
|
|
old_state = process.state
|
|
form = ChoicesColumnForm(request.POST or None, choices=ProcessStateEnum.as_choices(drop_empty_choice=True))
|
|
if request.method == "POST":
|
|
if form.is_valid():
|
|
process.state = int(form.cleaned_data.get("select", -1))
|
|
process.save()
|
|
_from = PROCESS_STATE_STRINGS.get(old_state)
|
|
_to = PROCESS_STATE_STRINGS.get(process.state)
|
|
messages.info(request, _("{} status changed from {} to {}").format(process.intervention.title, _from, _to))
|
|
else:
|
|
messages.error(request, form.errors)
|
|
return redirect("process:index")
|
|
|
|
|
|
@login_required
|
|
def add_compensation_view(request: HttpRequest, id: str):
|
|
""" Adds a new compensation to a process
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The process' id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
process = get_object_or_404(Process, id=id)
|
|
comp = Compensation()
|
|
comp.process = process
|
|
comp.save()
|
|
messages.info(request, _("Please fill in the data for this compensation"))
|
|
return redirect("compensation:edit", id=comp.id)
|
|
#template = ""
|
|
#context = {}
|
|
#context = BaseContext(request, context).context
|
|
#return render(request, template, context)
|