def test_synonyms_work_in_search_view(self): d1 = DocumentFactory(title="frob") d2 = DocumentFactory(title="glork") RevisionFactory(document=d1, is_approved=True) RevisionFactory(document=d2, is_approved=True) self.refresh() # First search without synonyms response = self.client.get(reverse("search"), {"q": "frob"}) doc = pq(response.content) header = doc.find("#search-results h2").text().strip() eq_(header, "Found 1 result for frob for All Products") # Now add a synonym. SynonymFactory(from_words="frob", to_words="frob, glork") update_synonyms_task() self.refresh() # Forward search response = self.client.get(reverse("search"), {"q": "frob"}) doc = pq(response.content) header = doc.find("#search-results h2").text().strip() eq_(header, "Found 2 results for frob for All Products") # Reverse search response = self.client.get(reverse("search"), {"q": "glork"}) doc = pq(response.content) header = doc.find("#search-results h2").text().strip() eq_(header, "Found 1 result for glork for All Products")
def test_synonyms_work_in_search_view(self): d1 = document(title='frob', save=True) d2 = document(title='glork', save=True) revision(document=d1, is_approved=True, save=True) revision(document=d2, is_approved=True, save=True) self.refresh() # First search without synonyms response = self.client.get(reverse('search'), {'q': 'frob'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 1 result for frob for All Products') # Now add a synonym. synonym(from_words='frob', to_words='frob, glork', save=True) update_synonyms_task() self.refresh() # Forward search response = self.client.get(reverse('search'), {'q': 'frob'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 2 results for frob for All Products') # Reverse search response = self.client.get(reverse('search'), {'q': 'glork'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 1 result for glork for All Products')
def test_synonyms_work_in_search_view(self): d1 = DocumentFactory(title='frob') d2 = DocumentFactory(title='glork') RevisionFactory(document=d1, is_approved=True) RevisionFactory(document=d2, is_approved=True) self.refresh() # First search without synonyms response = self.client.get(reverse('search'), {'q': 'frob'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 1 result for frob for All Products') # Now add a synonym. SynonymFactory(from_words='frob', to_words='frob, glork') update_synonyms_task() self.refresh() # Forward search response = self.client.get(reverse('search'), {'q': 'frob'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 2 results for frob for All Products') # Reverse search response = self.client.get(reverse('search'), {'q': 'glork'}) doc = pq(response.content) header = doc.find('#search-results h2').text().strip() eq_(header, 'Found 1 result for glork for All Products')
def synonym_editor(request): parse_errors = [] all_synonyms = Synonym.objects.all() if "sync_synonyms" in request.POST: # This is a task. Normally we would call tasks asyncronously, right? # In this case, since it runs quickly and is in the admin interface, # the advantage of it being run in the request/response cycle # outweight the delay in responding. If this becomes a problem # we should make a better UI and make this .delay() again. update_synonyms_task() return HttpResponseRedirect(request.path) synonyms_text = request.POST.get("synonyms_text") if synonyms_text is not None: db_syns = set((s.from_words, s.to_words) for s in all_synonyms) try: post_syns = set(synonym_utils.parse_synonyms(synonyms_text)) except synonym_utils.SynonymParseError as e: parse_errors = e.errors else: syns_to_add = post_syns - db_syns syns_to_remove = db_syns - post_syns for (from_words, to_words) in syns_to_remove: # This uses .get() because I want it to blow up if # there isn't exactly 1 matching synonym. (Synonym.objects.get(from_words=from_words, to_words=to_words).delete()) for (from_words, to_words) in syns_to_add: Synonym(from_words=from_words, to_words=to_words).save() return HttpResponseRedirect(request.path) # If synonyms_text is not None, it came from POST, and there were # errors. It shouldn't be modified, so the error messages make sense. if synonyms_text is None: synonyms_text = "\n".join(unicode(s) for s in all_synonyms) synonym_add_count, synonym_remove_count = synonym_utils.count_out_of_date() return render( request, "admin/search_synonyms.html", { "synonyms_text": synonyms_text, "errors": parse_errors, "synonym_add_count": synonym_add_count, "synonym_remove_count": synonym_remove_count, }, )
def synonym_editor(request): parse_errors = [] all_synonyms = Synonym.objects.all() if "sync_synonyms" in request.POST: # This is a task. Normally we would call tasks asyncronously, right? # In this case, since it runs quickly and is in the admin interface, # the advantage of it being run in the request/response cycle # outweight the delay in responding. If this becomes a problem # we should make a better UI and make this .delay() again. update_synonyms_task() return HttpResponseRedirect(request.path) synonyms_text = request.POST.get("synonyms_text") if synonyms_text is not None: db_syns = set((s.from_words, s.to_words) for s in all_synonyms) try: post_syns = set(synonym_utils.parse_synonyms(synonyms_text)) except synonym_utils.SynonymParseError as e: parse_errors = e.errors else: syns_to_add = post_syns - db_syns syns_to_remove = db_syns - post_syns for (from_words, to_words) in syns_to_remove: # This uses .get() because I want it to blow up if # there isn't exactly 1 matching synonym. (Synonym.objects.get(from_words=from_words, to_words=to_words).delete()) for (from_words, to_words) in syns_to_add: Synonym(from_words=from_words, to_words=to_words).save() return HttpResponseRedirect(request.path) # If synonyms_text is not None, it came from POST, and there were # errors. It shouldn't be modified, so the error messages make sense. if synonyms_text is None: synonyms_text = "\n".join(str(s) for s in all_synonyms) synonym_add_count, synonym_remove_count = synonym_utils.count_out_of_date() return render( request, "admin/search_synonyms.html", { "synonyms_text": synonyms_text, "errors": parse_errors, "synonym_add_count": synonym_add_count, "synonym_remove_count": synonym_remove_count, }, )