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 .models import TaskList, Task from .forms import TaskForm 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(task_list__in=lists).count(), ## TODO check it works properly 'list_count': lists.count(), }) @allowed_tasklist_required def tasklist_detail(request, tasklist): return render(request, 'todo/tasklist_detail.html', { 'tasklist': tasklist, }) @allowed_tasklist_required def tasklist_delete(request, tasklist): pass @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_add(request, tasklist): form = TaskForm(request.POST or None) if request.method == 'POST' and form.is_valid(): 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(reverse('todo:show-tasklist', kwargs={'tasklist_slug': tasklist.slug})) return render(request, 'todo/task_add.html', { 'tasklist': tasklist, 'form': form, }) @allowed_tasklist_required def task_detail(request, tasklist, task_id): task = get_object_or_404(Task, pk=task_id) return render(request, 'todo/task_detail.html', { 'tasklist': tasklist, 'task': task, }) @allowed_tasklist_required def task_toggle_done(request, tasklist, task_id): task = get_object_or_404(Task, pk=task_id) return redirect() # TODO @allowed_tasklist_required def task_delete(request, tasklist): pass