Source

alchemyadmin / src / alchemyadmin / demo / __init__.py

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
from werkzeug import SharedDataMiddleware

Base = declarative_base()
metadata = Base.metadata

user_job_table = Table('user_job', metadata,
                       Column('user_id', Integer, 
                              ForeignKey('user.user_id')),
                       Column('job_id', Integer,
                              ForeignKey('job.job_id')))

class User(Base):
    __tablename__ = 'user'
    user_id = Column(Integer, primary_key=True)
    user_name = Column(String(255), unique=True)
    password = Column(String(255))
    display_name = Column(Unicode(255))
    description = Column(UnicodeText)
    birthday = Column(DateTime)
    job = relation("Job", backref='users', secondary=user_job_table)


    def __unicode__(self):
        return self.display_name


class Job(Base):
    __tablename__ = 'job'
    job_id = Column(Integer, primary_key=True)
    name = Column(Unicode(255), unique=True)

    def __unicode__(self):
        return self.name
    

def make_app(global_conf, **app_conf):
    from alchemyadmin import Application
    conf = global_conf.copy()
    conf.update(app_conf)
    engine = engine_from_config(conf)
    metadata.bind = engine
    metadata.drop_all()
    metadata.create_all()
    Session = sessionmaker()
    session = Session()
    u = User()
    u.user_name = 'johndoe'
    u.display_name = u'John Doe'
    u.password = 'secret'
    u.birthday = datetime.now()
    u.description = """ This is demo user."""
    session.add(u)

    for j in (u"programmer", u"engineer", u"sales guy"):
        job = Job()
        job.name = j
        session.add(job)

    session.commit()

    app = Application(conf, User, Job)

    import os
    app = SharedDataMiddleware(app, {
            '/css':os.path.join(os.path.dirname(__file__), 'static', 'css')
            })
    return app