'Create a new page'

@web.expose
@web.require_login('create    pages')
def create(self, trans, page_title='', page_slug='', page_annotation=''):

    user = trans.get_user()
    page_title_err = page_slug_err = page_annotation_err = ''
    if (trans.request.method == 'POST'):
        if (not page_title):
            page_title_err = 'Page    name    is    required'
        elif (not page_slug):
            page_slug_err = 'Page    id    is    required'
        elif (not self._is_valid_slug(page_slug)):
            page_slug_err = "Page    identifier    must    consist    of    only    lowercase    letters,    numbers,    and    the    '-'    character"
        elif trans.sa_session.query(model.Page).filter_by(user=user, slug=page_slug, deleted=False).first():
            page_slug_err = 'Page    id    must    be    unique'
        else:
            page = model.Page()
            page.title = page_title
            page.slug = page_slug
            page_annotation = sanitize_html(page_annotation, 'utf-8', 'text/html')
            self.add_item_annotation(trans.sa_session, trans.get_user(), page, page_annotation)
            page.user = user
            page_revision = model.PageRevision()
            page_revision.title = page_title
            page_revision.page = page
            page.latest_revision = page_revision
            page_revision.content = ''
            session = trans.sa_session
            session.add(page)
            session.flush()
            return trans.response.send_redirect(web.url_for(controller='pages', action='list'))
    return trans.show_form(web.FormBuilder(web.url_for(controller='page', action='create'), 'Create    new    page', submit_text='Submit').add_text('page_title', 'Page    title', value=page_title, error=page_title_err).add_text('page_slug', 'Page    identifier', value=page_slug, error=page_slug_err, help="A    unique    identifier    that    will    be    used    for\n                                                                                                                public    links    to    this    page.    A    default    is    generated\n                                                                                                                from    the    page    title,    but    can    be    edited.    This    field\n                                                                                                                must    contain    only    lowercase    letters,    numbers,    and\n                                                                                                                the    '-'    character.").add_text('page_annotation', 'Page    annotation', value=page_annotation, error=page_annotation_err, help='A    description    of    the    page;    annotation    is    shown    alongside    published    pages.'), template='page/create.mako')
