2018-12-21 10:00:36 +00:00
|
|
|
import bleach
|
|
|
|
from django.contrib import messages
|
2019-01-10 08:39:21 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, user_passes_test
|
2018-12-21 10:00:36 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from todo.forms import AddEditTaskForm
|
|
|
|
from todo.models import Task, TaskList
|
2019-01-10 08:39:21 +00:00
|
|
|
from todo.utils import send_notify_mail, staff_check
|
2018-12-21 10:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2019-01-10 08:39:21 +00:00
|
|
|
@user_passes_test(staff_check)
|
2018-12-21 10:00:36 +00:00
|
|
|
def list_detail(request, list_id=None, list_slug=None, view_completed=False) -> HttpResponse:
|
|
|
|
"""Display and manage tasks in a todo list.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Defaults
|
|
|
|
task_list = None
|
|
|
|
form = None
|
|
|
|
|
|
|
|
# Which tasks to show on this list view?
|
|
|
|
if list_slug == "mine":
|
|
|
|
tasks = Task.objects.filter(assigned_to=request.user)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Show a specific list, ensuring permissions.
|
|
|
|
task_list = get_object_or_404(TaskList, id=list_id)
|
2019-04-12 06:44:28 +00:00
|
|
|
if task_list.group not in request.user.groups.all() and not request.user.is_superuser:
|
2018-12-21 10:00:36 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
tasks = Task.objects.filter(task_list=task_list.id)
|
|
|
|
|
|
|
|
# Additional filtering
|
|
|
|
if view_completed:
|
|
|
|
tasks = tasks.filter(completed=True)
|
|
|
|
else:
|
|
|
|
tasks = tasks.filter(completed=False)
|
|
|
|
|
|
|
|
# ######################
|
|
|
|
# Add New Task Form
|
|
|
|
# ######################
|
|
|
|
|
|
|
|
if request.POST.getlist("add_edit_task"):
|
|
|
|
form = AddEditTaskForm(
|
|
|
|
request.user,
|
|
|
|
request.POST,
|
|
|
|
initial={"assigned_to": request.user.id, "priority": 999, "task_list": task_list},
|
|
|
|
)
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
new_task = form.save(commit=False)
|
2019-07-30 05:53:33 +00:00
|
|
|
new_task.created_by = request.user
|
2018-12-21 10:00:36 +00:00
|
|
|
new_task.note = bleach.clean(form.cleaned_data["note"], strip=True)
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
# Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter
|
|
|
|
if (
|
|
|
|
"notify" in request.POST
|
|
|
|
and new_task.assigned_to
|
|
|
|
and new_task.assigned_to != request.user
|
|
|
|
):
|
|
|
|
send_notify_mail(new_task)
|
|
|
|
|
|
|
|
messages.success(request, 'New task "{t}" has been added.'.format(t=new_task.title))
|
|
|
|
return redirect(request.path)
|
|
|
|
else:
|
|
|
|
# Don't allow adding new tasks on some views
|
|
|
|
if list_slug not in ["mine", "recent-add", "recent-complete"]:
|
|
|
|
form = AddEditTaskForm(
|
|
|
|
request.user,
|
|
|
|
initial={"assigned_to": request.user.id, "priority": 999, "task_list": task_list},
|
|
|
|
)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"list_id": list_id,
|
|
|
|
"list_slug": list_slug,
|
|
|
|
"task_list": task_list,
|
|
|
|
"form": form,
|
|
|
|
"tasks": tasks,
|
|
|
|
"view_completed": view_completed,
|
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, "todo/list_detail.html", context)
|