Skip to content

Commit

Permalink
Fixes checklisthq#53 by using a Q obj to query for tag or title, inst…
Browse files Browse the repository at this point in the history
…ead of potentially matching both.
  • Loading branch information
rossjones committed Jul 6, 2013
1 parent f541635 commit c060d05
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
38 changes: 38 additions & 0 deletions checklisthq/main/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"""
import unittest

from django.contrib.auth.models import User
from django.test.client import Client

from forms import ChecklistForm
from models import Checklist

Expand All @@ -18,3 +21,38 @@ def test_META(self):
self.assertEqual(Checklist, ChecklistForm.Meta.model)
self.assertEqual(('title', 'content', 'tags'),
ChecklistForm.Meta.fields)

class TestSearch(unittest.TestCase):
"""
Ensures the correct behaviour of the form used for editing checklists.
"""

def setUp(self):
self.user,_ = User.objects.get_or_create(username="test_user")
self.cl1 = Checklist(title='foo', owner=self.user, content='bar')
self.cl1.save()
self.cl1.tags.add("foo")

self.cl2 = Checklist(title='bar', owner=self.user, content='vavavoom')
self.cl2.save()
self.cl2.tags.add("foo")

self.cl2 = Checklist(title='bar', owner=self.user, content='vavavoom')
self.cl2.save()
self.cl2.tags.add("bar")


def test_duplicates(self):
"""
Test for issue #53.
Search Function returns two instances of the same checklist if
a term is used both in the title and in the tag.
"""
c = Client()
response = c.get('/search', {'query': 'foo'})
# Just count TRs for now to get the results.
self.assertEqual( response.content.count("<tr "), 2)




12 changes: 8 additions & 4 deletions checklisthq/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q

from checklistdsl import lex, parse

from forms import ChecklistForm, NewUserForm
from models import Checklist


_logger = logging.getLogger(__name__)

def home(request):
Expand Down Expand Up @@ -161,10 +163,12 @@ def delete_checklist(request, id):
def search(request):
query = request.REQUEST["query"]
tags = [t.strip() for t in query.split(",")]
tagged_checklists = Checklist.objects.filter(tags__name__in=tags).distinct()
matched_checklists = Checklist.objects.filter(title__icontains=query.strip())
checklists = [checklist for checklist in tagged_checklists]
checklists.extend([checklist for checklist in matched_checklists])

tag_query = Q(tags__name__in=tags)
title_query = Q(title__icontains=query.strip())

matched_checklists = Checklist.objects.filter(tag_query | title_query)
checklists = [checklist for checklist in matched_checklists]
context = { 'checklists': checklists, 'tags': ', '.join(tags) }
return render(request, 'search_checklist.html', context)

Expand Down

0 comments on commit c060d05

Please sign in to comment.