|
@@ -0,0 +1,137 @@
|
|
|
+from django.contrib.auth.decorators import login_required
|
|
|
+from django.shortcuts import render, redirect, get_object_or_404
|
|
|
+from django.core.exceptions import PermissionDenied
|
|
|
+from django.contrib import messages
|
|
|
+from django.urls import reverse
|
|
|
+from django.http import HttpResponse
|
|
|
+from django.views.decorators.csrf import csrf_exempt
|
|
|
+from django.views.decorators.http import require_POST
|
|
|
+from django.utils import timezone
|
|
|
+
|
|
|
+from .models import TaskList, Task
|
|
|
+from .forms import TaskForm, CommentForm
|
|
|
+from .decorators import allowed_tasklist_required
|
|
|
+
|
|
|
+
|
|
|
+@login_required
|
|
|
+def tasklist_list(request):
|
|
|
+ lists = TaskList.objects.all()
|
|
|
+ if not request.user.is_superuser:
|
|
|
+ lists = lists.filter(group__in=request.user.groups.all())
|
|
|
+ lists = lists.order_by('name')
|
|
|
+ return render(request, 'todo/tasklist_list.html', {
|
|
|
+ 'lists': lists,
|
|
|
+ 'task_count': Task.objects.filter(completed_date__isnull=True, task_list__in=lists).count(),
|
|
|
+ 'list_count': lists.count(),
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def tasklist_detail(request, tasklist, completed=False):
|
|
|
+ task_list = tasklist.task_set.filter(completed_date__isnull=not completed)
|
|
|
+ return render(request, 'todo/tasklist_detail.html', {
|
|
|
+ 'tasklist': tasklist,
|
|
|
+ 'task_list': task_list,
|
|
|
+ 'completed': completed,
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def tasklist_reorder(request, tasklist):
|
|
|
+ newtasklist = request.POST.getlist("tasktable[]")
|
|
|
+ if newtasklist:
|
|
|
+ # Re-prioritize each task in list
|
|
|
+ i = 1
|
|
|
+ for pk in newtasklist:
|
|
|
+ try:
|
|
|
+ task = Task.objects.get(task_list=tasklist, pk=pk)
|
|
|
+ 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
|
|
|
+ # 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)
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def task_form(request, tasklist, task_id=None):
|
|
|
+ if task_id:
|
|
|
+ task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
|
|
|
+ redirect_url = reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task.pk})
|
|
|
+ else:
|
|
|
+ task = None
|
|
|
+ redirect_url = reverse('todo:show-tasklist', kwargs={'tasklist_slug': tasklist.slug})
|
|
|
+ form = TaskForm(request.POST or None, tasklist=tasklist, instance=task)
|
|
|
+ if request.method == 'POST' and form.is_valid():
|
|
|
+ if task:
|
|
|
+ form.save()
|
|
|
+ messages.success(request, 'Tâche mise à jour avec succès.')
|
|
|
+ else:
|
|
|
+ task = form.save(commit=False)
|
|
|
+ task.task_list = tasklist
|
|
|
+ task.created_by = request.user
|
|
|
+ task.save()
|
|
|
+ messages.success(request, 'Tâche créée avec succès.')
|
|
|
+ return redirect(redirect_url)
|
|
|
+ return render(request, 'todo/task_form.html', {
|
|
|
+ 'tasklist': tasklist,
|
|
|
+ 'task': task,
|
|
|
+ 'form': form,
|
|
|
+ 'cancel_url': redirect_url,
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def task_detail(request, tasklist, task_id):
|
|
|
+ task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
|
|
|
+ form = CommentForm(request.POST or None)
|
|
|
+ if request.method == 'POST' and form.is_valid():
|
|
|
+ comment = form.save(commit=False)
|
|
|
+ comment.task = task
|
|
|
+ comment.author = request.user
|
|
|
+ comment.save()
|
|
|
+ messages.success(request, 'Commentaire ajouté avec succès.')
|
|
|
+ return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
|
|
|
+ return render(request, 'todo/task_detail.html', {
|
|
|
+ 'tasklist': tasklist,
|
|
|
+ 'task': task,
|
|
|
+ 'form': form,
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def task_toggle_done(request, tasklist, task_id):
|
|
|
+ task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
|
|
|
+ if task.completed_date:
|
|
|
+ task.completed_date = None
|
|
|
+ messages.success(request, "La tâche « {} » a été marquée en cours.".format(task.title))
|
|
|
+ else:
|
|
|
+ task.completed_date = timezone.now()
|
|
|
+ messages.success(request, "La tâche « {} » a été marquée complétée.".format(task.title))
|
|
|
+ task.save()
|
|
|
+ return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
|
|
|
+
|
|
|
+
|
|
|
+@allowed_tasklist_required
|
|
|
+def task_edit(request, tasklist, task_id):
|
|
|
+ task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
|
|
|
+ form = CommentForm(request.POST)
|
|
|
+ if form.is_valid():
|
|
|
+ comment = form.save(commit=False)
|
|
|
+ comment.save()
|
|
|
+ messages.success(request, "La tâche « {} » a été marquée complétée.".format(task.title))
|
|
|
+ return redirect(reverse('todo:show-task', kwargs={'tasklist_slug': tasklist.slug, 'task_id': task_id}))
|
|
|
+
|
|
|
+
|
|
|
+# TODO: are you sure?
|
|
|
+@require_POST
|
|
|
+@allowed_tasklist_required
|
|
|
+def task_delete(request, tasklist, task_id):
|
|
|
+ task = get_object_or_404(Task, task_list=tasklist, pk=task_id)
|
|
|
+ task.delete()
|
|
|
+ messages.success(request, "La tâche « {} » a été supprimée.".format(task.title))
|
|
|
+ return redirect(reverse('todo:show-tasklist', kwargs={'tasklist_slug': tasklist.slug}))
|