From 6e032b22bbc4093a0cdcdbb595e3f2b1e947182d Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Mon, 17 Nov 2014 14:42:17 -0800 Subject: [PATCH] Enable simple search of tasks --- todo/forms.py | 2 +- todo/templates/todo/search_results.html | 3 ++- todo/urls.py | 1 + todo/views.py | 19 +++++++++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/todo/forms.py b/todo/forms.py index ae72b41..9b1b59d 100644 --- a/todo/forms.py +++ b/todo/forms.py @@ -76,7 +76,7 @@ class AddExternalItemForm(ModelForm): exclude = ('list', 'created_date', 'due_date', 'created_by', 'assigned_to',) -class SearchForm(ModelForm): +class SearchForm(forms.Form): """Search.""" q = forms.CharField( diff --git a/todo/templates/todo/search_results.html b/todo/templates/todo/search_results.html index fe7d336..3abd973 100644 --- a/todo/templates/todo/search_results.html +++ b/todo/templates/todo/search_results.html @@ -20,10 +20,11 @@

{{ f.title }}
- On list: {{ f.list }}
+ On list: {{ f.list.name }}
Assigned to: {{ f.assigned_to }} (created by: {{ f.created_by }})
Complete: {{ f.completed|yesno:"Yes,No" }}
+

{% endfor %} diff --git a/todo/urls.py b/todo/urls.py index c793c78..01ccfe9 100644 --- a/todo/urls.py +++ b/todo/urls.py @@ -9,6 +9,7 @@ urlpatterns = patterns( url(r'^(?P\d{1,4})/(?P[\w-]+)/completed$', 'todo.views.view_list', {'view_completed': 1}, name='todo-completed_tasks'), url(r'^add_list/$', 'todo.views.add_list', name="todo-add_list"), + url(r'^search-post/$', 'todo.views.search_post', name="todo-search-post"), url(r'^search/$', 'todo.views.search', name="todo-search"), url(r'^$', 'todo.views.list_lists', name="todo-lists"), diff --git a/todo/views.py b/todo/views.py index 0a52aec..f7a97ce 100644 --- a/todo/views.py +++ b/todo/views.py @@ -1,6 +1,6 @@ from django.shortcuts import render_to_response from todo.models import Item, List, Comment -from todo.forms import AddListForm, AddItemForm, EditItemForm, AddExternalItemForm +from todo.forms import AddListForm, AddItemForm, EditItemForm, AddExternalItemForm, SearchForm from todo import settings from django.contrib.auth.models import User from django.shortcuts import get_object_or_404 @@ -40,6 +40,7 @@ def list_lists(request): 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. group_count = request.user.groups.all().count() @@ -374,6 +375,20 @@ def add_list(request): return render_to_response('todo/add_list.html', locals(), context_instance=RequestContext(request)) + +@user_passes_test(check_user_allowed) +def search_post(request): + """ + Redirect POST'd search param to query GET string + """ + if request.POST: + q = request.POST.get('q') + url = reverse('todo-search') + "?q=" + q + print(url) + return HttpResponseRedirect(url) + + + @user_passes_test(check_user_allowed) def search(request): """ @@ -396,7 +411,7 @@ def search(request): # In that case we still need found_items in a queryset so it can be "excluded" below. found_items = Item.objects.all() - if request.GET['inc_complete'] == "0": + if 'inc_complete' in request.GET: found_items = found_items.exclude(completed=True) else: