views.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from django.contrib.auth.decorators import login_required
  2. from django.shortcuts import render, redirect, get_object_or_404
  3. from django.core.exceptions import PermissionDenied
  4. from django.contrib import messages
  5. from django.urls import reverse
  6. from django.http import HttpResponse
  7. from django.views.decorators.csrf import csrf_exempt
  8. from django.views.decorators.http import require_POST
  9. from django.utils import timezone
  10. from django.db.models import Count, Q
  11. from .models import TaskList, Task
  12. from .forms import TaskForm, CommentForm
  13. from .decorators import allowed_tasklist_required
  14. @login_required
  15. def tasklist_list(request):
  16. lists = TaskList.objects.all()
  17. if not request.user.is_superuser:
  18. lists = lists.filter(group__in=request.user.groups.all())
  19. lists = lists.order_by('name')
  20. lists = lists.annotate(own_uncompleted_task_count=Count('task', filter=Q(task__completed_date__isnull=True, task__assigned_to=request.user)))
  21. return render(request, 'todo/tasklist_list.html', {
  22. 'lists': lists,
  23. 'task_count': Task.objects.filter(completed_date__isnull=True, task_list__in=lists).count(),
  24. 'list_count': lists.count(),
  25. })
  26. @allowed_tasklist_required
  27. def tasklist_detail(request, tasklist, completed=False):
  28. task_list = tasklist.task_set.filter(completed_date__isnull=not completed)
  29. return render(request, 'todo/tasklist_detail.html', {
  30. 'tasklist': tasklist,
  31. 'task_list': task_list,
  32. 'completed': completed,
  33. })
  34. @allowed_tasklist_required
  35. def tasklist_reorder(request, tasklist):
  36. newtasklist = request.POST.getlist("tasktable[]")
  37. if newtasklist:
  38. # Re-prioritize each task in list
  39. i = 1
  40. for pk in newtasklist:
  41. try:
  42. task = Task.objects.get(task_list=tasklist, pk=pk)
  43. task.priority = i
  44. task.save()
  45. i += 1
  46. except Task.DoesNotExist:
  47. # Can occur if task is deleted behind the scenes during re-ordering.
  48. # Not easy to remove it from the UI without page refresh, but prevent crash.
  49. pass
  50. # All views must return an httpresponse of some kind ... without this we get
  51. # error 500s in the log even though things look peachy in the browser.
  52. return HttpResponse(status=201)
  53. @allowed_tasklist_required
  54. def task_form(request, tasklist, task_id=None):
  55. if task_id:
  56. task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
  57. redirect_url = reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task.pk})
  58. else:
  59. task = None
  60. redirect_url = reverse('todo:show-tasklist', kwargs={'tasklist_slug': tasklist.slug})
  61. form = TaskForm(request.POST or None, tasklist=tasklist, instance=task)
  62. if request.method == 'POST' and form.is_valid():
  63. if task:
  64. form.save()
  65. messages.success(request, 'Tâche mise à jour avec succès.')
  66. else:
  67. task = form.save(commit=False)
  68. task.task_list = tasklist
  69. task.created_by = request.user
  70. task.save()
  71. messages.success(request, 'Tâche créée avec succès.')
  72. return redirect(redirect_url)
  73. return render(request, 'todo/task_form.html', {
  74. 'tasklist': tasklist,
  75. 'task': task,
  76. 'form': form,
  77. 'cancel_url': redirect_url,
  78. })
  79. @allowed_tasklist_required
  80. def task_detail(request, tasklist, task_id):
  81. task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
  82. form = CommentForm(request.POST or None)
  83. if request.method == 'POST' and form.is_valid():
  84. comment = form.save(commit=False)
  85. comment.task = task
  86. comment.author = request.user
  87. comment.save()
  88. messages.success(request, 'Commentaire ajouté avec succès.')
  89. return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
  90. return render(request, 'todo/task_detail.html', {
  91. 'tasklist': tasklist,
  92. 'task': task,
  93. 'form': form,
  94. })
  95. @allowed_tasklist_required
  96. def task_toggle_done(request, tasklist, task_id):
  97. task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
  98. if task.completed_date:
  99. task.completed_date = None
  100. messages.success(request, "La tâche « {} » a été marquée en cours.".format(task.title))
  101. else:
  102. task.completed_date = timezone.now()
  103. messages.success(request, "La tâche « {} » a été marquée complétée.".format(task.title))
  104. task.save()
  105. return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
  106. @allowed_tasklist_required
  107. def task_edit(request, tasklist, task_id):
  108. task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
  109. form = CommentForm(request.POST)
  110. if form.is_valid():
  111. comment = form.save(commit=False)
  112. comment.save()
  113. messages.success(request, "La tâche « {} » a été marquée complétée.".format(task.title))
  114. return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
  115. # TODO: are you sure?
  116. @require_POST
  117. @allowed_tasklist_required
  118. def task_delete(request, tasklist, task_id):
  119. task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
  120. task.delete()
  121. messages.success(request, "La tâche « {} » a été supprimée.".format(task.title))
  122. return redirect(reverse('todo:show-tasklist', kwargs={'tasklist_slug': tasklist.slug}))