del_list.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. from django.contrib import messages
  2. from django.contrib.auth.decorators import login_required, user_passes_test
  3. from django.core.exceptions import PermissionDenied
  4. from django.http import HttpResponse
  5. from django.shortcuts import get_object_or_404, redirect, render
  6. from tickets.models import TaskList
  7. from tickets.utils import staff_check
  8. @login_required
  9. @user_passes_test(staff_check)
  10. def del_list(request, list_id: int, list_slug: str) -> HttpResponse:
  11. """Delete an entire list. Only staff members should be allowed to access this view.
  12. """
  13. task_list = get_object_or_404(TaskList, id=list_id)
  14. # Ensure user has permission to delete list. Get the group this list belongs to,
  15. # and check whether current user is a member of that group AND a staffer.
  16. if task_list.group not in request.user.groups.all():
  17. raise PermissionDenied
  18. if not request.user.is_staff:
  19. raise PermissionDenied
  20. if request.method == "POST":
  21. TaskList.objects.get(id=task_list.id).delete()
  22. messages.success(request, "{list_name} is gone.".format(list_name=task_list.name))
  23. return redirect("tickets:lists")
  24. context = {
  25. "task_list": task_list
  26. }
  27. return render(request, "tickets/del_list.html", context)