def on_edit(request, page_name):
    """Edit the current revision of a page."""
    change_note = error = ""
    revision = (
        Revision.query.filter(
            (Page.name == page_name) & (Page.page_id == Revision.page_id)
        )
        .order_by(Revision.revision_id.desc())
        .first()
    )
    if revision is None:
        page = None
    else:
        page = revision.page

    if request.method == "POST":
        text = request.form.get("text")
        if request.form.get("cancel") or revision and revision.text == text:
            return redirect(href(page.name))
        elif not text:
            error = "You cannot save empty revisions."
        else:
            change_note = request.form.get("change_note", "")
            if page is None:
                page = Page(page_name)
                session.add(page)
            session.add(Revision(page, text, change_note))
            session.commit()
            return redirect(href(page.name))

    return Response(
        generate_template(
            "action_edit.html",
            revision=revision,
            page=page,
            new=page is None,
            page_name=page_name,
            change_note=change_note,
            error=error,
        )
    )