示例#1
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,
        },
    )
示例#2
0
文件: admin.py 项目: zu83/kitsune
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,
        },
    )