Split up views into separate modules
This commit is contained in:
parent
21ec87cee4
commit
78e9c510bc
15 changed files with 557 additions and 464 deletions
40
todo/views/search.py
Normal file
40
todo/views/search.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
from todo.models import Task
|
||||
|
||||
|
||||
@login_required
|
||||
def search(request) -> HttpResponse:
|
||||
"""Search for tasks user has permission to see.
|
||||
"""
|
||||
if request.GET:
|
||||
|
||||
query_string = ""
|
||||
found_tasks = None
|
||||
if ("q" in request.GET) and request.GET["q"].strip():
|
||||
query_string = request.GET["q"]
|
||||
|
||||
found_tasks = Task.objects.filter(
|
||||
Q(title__icontains=query_string) | Q(note__icontains=query_string)
|
||||
)
|
||||
else:
|
||||
# What if they selected the "completed" toggle but didn't enter a query string?
|
||||
# We still need found_tasks in a queryset so it can be "excluded" below.
|
||||
found_tasks = Task.objects.all()
|
||||
|
||||
if "inc_complete" in request.GET:
|
||||
found_tasks = found_tasks.exclude(completed=True)
|
||||
|
||||
else:
|
||||
query_string = None
|
||||
found_tasks = None
|
||||
|
||||
# Only include tasks that are in groups of which this user is a member:
|
||||
if not request.user.is_superuser:
|
||||
found_tasks = found_tasks.filter(task_list__group__in=request.user.groups.all())
|
||||
|
||||
context = {"query_string": query_string, "found_tasks": found_tasks}
|
||||
return render(request, "todo/search_results.html", context)
|
Loading…
Add table
Add a link
Reference in a new issue