Show the diff between two revisions.

def on_diff(request, page_name):
    """Show the diff between two revisions."""
    old = request.args.get("old", type=int)
    new = request.args.get("new", type=int)
    error = ""
    diff = page = old_rev = new_rev = None

    if not (old and new):
        error = "No revisions specified."
    else:
        revisions = dict(
            (x.revision_id, x)
            for x in Revision.query.filter(
                (Revision.revision_id.in_((old, new)))
                & (Revision.page_id == Page.page_id)
                & (Page.name == page_name)
            )
        )
        if len(revisions) != 2:
            error = "At least one of the revisions requested does not exist."
        else:
            new_rev = revisions[new]
            old_rev = revisions[old]
            page = old_rev.page
            diff = unified_diff(
                (old_rev.text + "\n").splitlines(True),
                (new_rev.text + "\n").splitlines(True),
                page.name,
                page.name,
                format_datetime(old_rev.timestamp),
                format_datetime(new_rev.timestamp),
                3,
            )

    return Response(
        generate_template(
            "action_diff.html",
            error=error,
            old_revision=old_rev,
            new_revision=new_rev,
            page=page,
            diff=diff,
        )
    )