def save(self, trans, id, content, annotations): id = self.decode_id(id) page = trans.sa_session.query(model.Page).get(id) assert page.user == trans.user # Sanitize content content = sanitize_html(content) processor = _PageContentProcessor(trans, _placeholderRenderForSave) processor.feed(content) # Output is string, so convert to unicode for saving. content = unicodify(processor.output(), 'utf-8') # Add a new revision to the page with the provided content. page_revision = model.PageRevision() page_revision.title = page.title page_revision.page = page page.latest_revision = page_revision page_revision.content = content # Save annotations. annotations = loads(annotations) for annotation_dict in annotations: item_id = self.decode_id(annotation_dict['item_id']) item_class = self.get_class(annotation_dict['item_class']) item = trans.sa_session.query(item_class).filter_by( id=item_id).first() if not item: raise RuntimeError("cannot find annotated item") text = sanitize_html(annotation_dict['text']) # Add/update annotation. if item_id and item_class and text: # Get annotation association. annotation_assoc_class = eval("model.%sAnnotationAssociation" % item_class.__name__) annotation_assoc = trans.sa_session.query( annotation_assoc_class).filter_by(user=trans.get_user()) if item_class == model.History.__class__: annotation_assoc = annotation_assoc.filter_by(history=item) elif item_class == model.HistoryDatasetAssociation.__class__: annotation_assoc = annotation_assoc.filter_by(hda=item) elif item_class == model.StoredWorkflow.__class__: annotation_assoc = annotation_assoc.filter_by( stored_workflow=item) elif item_class == model.WorkflowStep.__class__: annotation_assoc = annotation_assoc.filter_by( workflow_step=item) annotation_assoc = annotation_assoc.first() if not annotation_assoc: # Create association. annotation_assoc = annotation_assoc_class() item.annotations.append(annotation_assoc) annotation_assoc.user = trans.get_user() # Set annotation user text. annotation_assoc.annotation = text trans.sa_session.flush()
def create(self, trans, payload=None, **kwd): """ Create a new page. """ if trans.request.method == 'GET': return { 'title' : 'Create a new page', 'inputs' : [{ 'name' : 'title', 'label' : 'Name' }, { 'name' : 'slug', 'label' : 'Identifier', 'help' : 'A unique identifier that will be used for public links to this page. This field can only contain lowercase letters, numbers, and dashes (-).' }, { 'name' : 'annotation', 'label' : 'Annotation', 'help' : 'A description of the page. The annotation is shown alongside published pages.' }] } else: user = trans.get_user() p_title = payload.get('title') p_slug = payload.get('slug') p_annotation = payload.get('annotation') if not p_title: return self.message_exception(trans, 'Please provide a page name is required.') elif not p_slug: return self.message_exception(trans, 'Please provide a unique identifier.') elif not self._is_valid_slug(p_slug): return self.message_exception(trans, 'Page identifier can only contain lowercase letters, numbers, and dashes (-).') elif trans.sa_session.query(model.Page).filter_by(user=user, slug=p_slug, deleted=False).first(): return self.message_exception(trans, 'Page id must be unique.') else: # Create the new stored page p = model.Page() p.title = p_title p.slug = p_slug p.user = user if p_annotation: p_annotation = sanitize_html(p_annotation, 'utf-8', 'text/html') self.add_item_annotation(trans.sa_session, user, p, p_annotation) # And the first (empty) page revision p_revision = model.PageRevision() p_revision.title = p_title p_revision.page = p p.latest_revision = p_revision p_revision.content = "" # Persist trans.sa_session.add(p) trans.sa_session.flush() return {'message': 'Page \'%s\' successfully created.' % p.title, 'status': 'success'}
def create( self, trans, page_title="", page_slug="", page_annotation="" ): """ Create a new page """ 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: # Create the new stored page 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 # And the first (empty) page revision page_revision = model.PageRevision() page_revision.title = page_title page_revision.page = page page.latest_revision = page_revision page_revision.content = "" # Persist session = trans.sa_session session.add( page ) session.flush() # Display the management page ## trans.set_message( "Page '%s' created" % page.title ) return trans.response.send_redirect( web.url_for(controller='page', 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 public links to this page. A default is generated from the page title, but can be edited. This field must contain only lowercase letters, numbers, and 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" )