Source

crosswiki / crosswiki / models.py

Full commit
from docutils.core import publish_parts
import sqlalchemy as sa
import sqlahelper
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound

Base = sqlahelper.get_base()
DBSession = sqlahelper.get_session()

class WikiResource(object):
    def __init__(self, request):
        self.request = request

    def get_page(self, page_name):
        try:
            page = Page.query.filter_by(page_name=page_name).one()
            return page
        except NoResultFound:
            return None

    def add(self, data):
        DBSession.add(data)

class Page(Base):
    query = DBSession.query_property()
    __tablename__ = 'pages'
    id = sa.Column(sa.Integer, primary_key=True)
    page_name = sa.Column(sa.Unicode(255), unique=True)
    content = sa.Column(sa.UnicodeText)

    @property
    def html_content(self):

        parts = publish_parts(source=self.content,
            writer_name='html')
        return parts['html_body'] 

def create_frontpage(engine):
    sqlahelper.add_engine(engine)
    Base.metadata.create_all(bind=DBSession.bind)
    page = Page(page_name='FrontPage',
        content='FrontPage\n_______________')
    DBSession.add(page)
    import transaction
    try:
        transaction.commit()
    except IntegrityError:
        transaction.abort()