reorder_tasks.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from django.contrib.auth.decorators import login_required, user_passes_test
  2. from django.http import HttpResponse
  3. from django.views.decorators.csrf import csrf_exempt
  4. from tickets.models import Task
  5. from tickets.utils import staff_check
  6. @csrf_exempt
  7. @login_required
  8. @user_passes_test(staff_check)
  9. def reorder_tasks(request) -> HttpResponse:
  10. """Handle task re-ordering (priorities) from JQuery drag/drop in list_detail.html
  11. """
  12. newtasklist = request.POST.getlist("tasktable[]")
  13. if newtasklist:
  14. # First task in received list is always empty - remove it
  15. del newtasklist[0]
  16. # Re-prioritize each task in list
  17. i = 1
  18. for id in newtasklist:
  19. try:
  20. task = Task.objects.get(pk=id)
  21. task.priority = i
  22. task.save()
  23. i += 1
  24. except Task.DoesNotExist:
  25. # Can occur if task is deleted behind the scenes during re-ordering.
  26. # Not easy to remove it from the UI without page refresh, but prevent crash.
  27. pass
  28. # All views must return an httpresponse of some kind ... without this we get
  29. # error 500s in the log even though things look peachy in the browser.
  30. return HttpResponse(status=201)