Split up views into separate modules
This commit is contained in:
parent
21ec87cee4
commit
78e9c510bc
15 changed files with 557 additions and 464 deletions
37
todo/views/toggle_done.py
Normal file
37
todo/views/toggle_done.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import reverse
|
||||
|
||||
from todo.models import Task
|
||||
from todo.utils import toggle_task_completed
|
||||
|
||||
|
||||
@login_required
|
||||
def toggle_done(request, task_id: int) -> HttpResponse:
|
||||
"""Toggle the completed status of a task from done to undone, or vice versa.
|
||||
Redirect to the list from which the task came.
|
||||
"""
|
||||
|
||||
task = get_object_or_404(Task, pk=task_id)
|
||||
|
||||
# Permissions
|
||||
if not (
|
||||
(request.user.is_superuser)
|
||||
or (task.created_by == request.user)
|
||||
or (task.assigned_to == request.user)
|
||||
or (task.task_list.group in request.user.groups.all())
|
||||
):
|
||||
raise PermissionDenied
|
||||
|
||||
toggle_task_completed(task.id)
|
||||
messages.success(request, "Task status changed for '{}'".format(task.title))
|
||||
|
||||
return redirect(
|
||||
reverse(
|
||||
"todo:list_detail",
|
||||
kwargs={"list_id": task.task_list.id, "list_slug": task.task_list.slug},
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue