Split up views into separate modules

This commit is contained in:
Scot Hacker 2018-12-21 02:00:36 -08:00
parent 21ec87cee4
commit 78e9c510bc
15 changed files with 557 additions and 464 deletions

55
todo/views/list_lists.py Normal file
View file

@ -0,0 +1,55 @@
import datetime
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from todo.forms import SearchForm
from todo.models import Task, TaskList
@login_required
def list_lists(request) -> HttpResponse:
"""Homepage view - list of lists a user can view, and ability to add a list.
"""
thedate = datetime.datetime.now()
searchform = SearchForm(auto_id=False)
# Make sure user belongs to at least one group.
if request.user.groups.all().count() == 0:
messages.warning(
request,
"You do not yet belong to any groups. Ask your administrator to add you to one.",
)
# Superusers see all lists
if request.user.is_superuser:
lists = TaskList.objects.all().order_by("group", "name")
else:
lists = TaskList.objects.filter(group__in=request.user.groups.all()).order_by(
"group", "name"
)
list_count = lists.count()
# superusers see all lists, so count shouldn't filter by just lists the admin belongs to
if request.user.is_superuser:
task_count = Task.objects.filter(completed=0).count()
else:
task_count = (
Task.objects.filter(completed=0)
.filter(task_list__group__in=request.user.groups.all())
.count()
)
context = {
"lists": lists,
"thedate": thedate,
"searchform": searchform,
"list_count": list_count,
"task_count": task_count,
}
return render(request, "todo/list_lists.html", context)