'Revert a wiki `page` to `revision`'

@require_oauth2_scope('modwiki')
@validate(VModhash(), VWikiModerator(), pv=VWikiPageAndVersion(('page', 'revision')))
@api_doc(api_section.wiki, uri='/api/wiki/revert', uses_site=True)
def POST_wiki_revision_revert(self, pv):

    (page, revision) = pv
    if (not revision):
        self.handle_error(400, 'INVALID_REVISION')
    VNotInTimeout().run(action_name='wikirevise', details_text='revision_revert', target=page)
    content = revision.content
    reason = ('reverted    back    %s' % timesince(revision.date))
    if (page.name == 'config/stylesheet'):
        (css_errors, parsed) = c.site.parse_css(content)
        if css_errors:
            self.handle_error(403, 'INVALID_CSS')
        c.site.change_css(content, parsed, prev=None, reason=reason, force=True)
    else:
        try:
            page.revise(content, author=c.user._id36, reason=reason, force=True)
            if (page.name in ATTRIBUTE_BY_PAGE):
                setattr(c.site, ATTRIBUTE_BY_PAGE[page.name], content)
                c.site._commit()
        except ContentLengthError as e:
            self.handle_error(403, 'CONTENT_LENGTH_ERROR', max_length=e.max_length)
    return json.dumps({})
