Ejemplo n.º 1
0
def _exportUnknownNames():
    """ Build and return an HttpResponse which exports the unknown names.

        We create a CSV-format file containing the list of unknown names.
    """
    response = HttpResponse(mimetype="text/csv")
    response['Content-Disposition'] = "attachment; filename=unknown-names.csv"

    writer = UnicodeWriter(response)

    writer.writerow(["Name", "Context", "Number of Occurrences"])
    for rec in unknownNames.getCurrent(cutoff=CUTOFF):
        writer.writerow([rec.name, rec.context, str(rec.numOccurrences)])

    return response
Ejemplo n.º 2
0
def _buildPage(request, rejected):
    """ Build and return our web page.

        We build and return the "unknown" page.  'request' is the Django
        request object we are processing, and 'rejected' will be True iff we
        are showing the page of rejected names.

        Note that this is shared by the current() and rejected() view
        functions.
    """
    if rejected:
        tab = "rejected"
        recs = unknownNames.getRejected()
    else:
        tab = "current"
        recs = unknownNames.getCurrent(cutoff=CUTOFF)

    paginator = Paginator(recs, 15) # Show 15 records per page.
    page_num = request.GET.get('page', 0)

    try:
        page = paginator.page(page_num)
    except (PageNotAnInteger, EmptyPage):
        # Invalid page -> show the first page.
        page = paginator.page(1)

    if request.method == "GET":

        # We're visiting the page for the first time -> just display it.

        confirm = request.GET.get("confirm")

    elif request.method == "POST":

        # The user is submitting our form.  See what the user wants to do.

        # Did the user click on the "Export" button?

        if request.POST.get("export") == "Export":
            return _exportUnknownNames()

        # Did the user click on the "Clear" button?

        if request.POST.get("clear") == "Clear":
            return HttpResponseRedirect("/geo/editor/unknown/clear/")

        # Did the user click on the "Finished" button?

        if request.POST.get("finished") == "Finished":
            return HttpResponseRedirect("/geo/editor/")

        # Did the user click on one of our "Reject" or "Unreject" buttons?

        if rejected:
            for rec in recs:
                unrejectValue = request.POST.get("unreject-" + str(rec.id))

                if unrejectValue == "Un-Reject":
                    # The user clicked on the "Un-Reject" button for this
                    # entry.  Un-reject this name and reload the page.
                    unknownNames.unReject(rec)
                    return HttpResponseRedirect("/geo/editor/unknown/rejected/"
                                                + "?page=" + str(page_num))
        else:
            for rec in recs:
                rejectValue = request.POST.get("reject-" + str(rec.id))

                if rejectValue == "Reject":
                    # The user clicked on the "Reject" button for this entry.
                    # Reject this name and reload the page.
                    unknownNames.reject(rec)
                    return HttpResponseRedirect("/geo/editor/unknown/current/"
                                                + "?page=" + str(page_num))

    # If we get here, display the form to the user.

    return render_to_response("editor/templates/unknown.html",
                              {'tab'  : tab,
                               'page' : page},
                              context_instance=RequestContext(request))