Beispiel #1
0
 def testTooFewArrows(self):
     try:
         synonym_utils.parse_synonyms("foo, bar, baz")
     except synonym_utils.SynonymParseError as e:
         eq_(len(e.errors), 1)
     else:
         assert False, "Parser did not catch error as expected."
Beispiel #2
0
 def testTooManyArrows(self):
     try:
         synonym_utils.parse_synonyms('foo => bar => baz')
     except synonym_utils.SynonymParseError as e:
         eq_(len(e.errors), 1)
     else:
         assert False, "Parser did not catch error as expected."
Beispiel #3
0
 def testTooFewArrows(self):
     try:
         synonym_utils.parse_synonyms('foo, bar, baz')
     except synonym_utils.SynonymParseError as e:
         eq_(len(e.errors), 1)
     else:
         assert False, "Parser did not catch error as expected."
Beispiel #4
0
 def testItWorks(self):
     synonym_text = dedent("""
         one, two => apple, banana
         three => orange, grape
         four, five => jellybean
         """)
     synonyms = {
         ("one, two", "apple, banana"),
         ("three", "orange, grape"),
         ("four, five", "jellybean"),
     }
     eq_(synonyms, synonym_utils.parse_synonyms(synonym_text))
Beispiel #5
0
 def testItWorks(self):
     synonym_text = dedent("""
         one, two => apple, banana
         three => orange, grape
         four, five => jellybean
         """)
     synonyms = set([
         ('one, two', 'apple, banana'),
         ('three', 'orange, grape'),
         ('four, five', 'jellybean'),
     ])
     eq_(synonyms, synonym_utils.parse_synonyms(synonym_text))
Beispiel #6
0
 def testItWorks(self):
     synonym_text = dedent("""
         one, two => apple, banana
         three => orange, grape
         four, five => jellybean
         """)
     synonyms = set([
         ('one, two', 'apple, banana'),
         ('three', 'orange, grape'),
         ('four, five', 'jellybean'),
     ])
     eq_(synonyms, synonym_utils.parse_synonyms(synonym_text))
Beispiel #7
0
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,
        },
    )
Beispiel #8
0
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,
        },
    )