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.http import HttpResponse
|
2019-01-10 08:39:21 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2018-12-21 10:00:36 +00:00
|
|
|
|
|
|
|
from todo.models import Task
|
2019-01-10 08:39:21 +00:00
|
|
|
from todo.utils import staff_check
|
2018-12-21 10:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
@login_required
|
2019-01-10 08:39:21 +00:00
|
|
|
@user_passes_test(staff_check)
|
2018-12-21 10:00:36 +00:00
|
|
|
def reorder_tasks(request) -> HttpResponse:
|
|
|
|
"""Handle task re-ordering (priorities) from JQuery drag/drop in list_detail.html
|
|
|
|
"""
|
|
|
|
newtasklist = request.POST.getlist("tasktable[]")
|
|
|
|
if newtasklist:
|
|
|
|
# First task in received list is always empty - remove it
|
|
|
|
del newtasklist[0]
|
|
|
|
|
|
|
|
# Re-prioritize each task in list
|
|
|
|
i = 1
|
|
|
|
for id in newtasklist:
|
2019-03-25 14:45:26 +00:00
|
|
|
try:
|
|
|
|
task = Task.objects.get(pk=id)
|
|
|
|
task.priority = i
|
|
|
|
task.save()
|
|
|
|
i += 1
|
|
|
|
except Task.DoesNotExist:
|
|
|
|
# Can occur if task is deleted behind the scenes during re-ordering.
|
|
|
|
# Not easy to remove it from the UI without page refresh, but prevent crash.
|
|
|
|
pass
|
2018-12-21 10:00:36 +00:00
|
|
|
|
|
|
|
# All views must return an httpresponse of some kind ... without this we get
|
|
|
|
# error 500s in the log even though things look peachy in the browser.
|
|
|
|
return HttpResponse(status=201)
|