Skip to content

Instantly share code, notes, and snippets.

@hunt3r
Created March 1, 2012 03:35
Show Gist options
  • Save hunt3r/1947065 to your computer and use it in GitHub Desktop.
Save hunt3r/1947065 to your computer and use it in GitHub Desktop.
models/__init__.py:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
DBSession = scoped_session(sessionmaker(autoflush=True))
Base = declarative_base()
def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
models/Artifact.py:
from visionare.models import Base
from sqlalchemy import (Column, Unicode, Integer, Text, VARCHAR, DateTime)
class Artifact(Base):
__tablename__ = 'artifacts'
id = Column('artifact_id', Integer, primary_key=True)
name = Column('name', VARCHAR(300))
data = Column('data', Text)
date_added = Column('date_added', DateTime)
date_updated = Column('date_updated', DateTime)
def __init__(self, name, data):
self.name = name
self.data = data
root __init__.py:
from sqlalchemy import engine_from_config
from pyramid.config import Configurator
from visionare.models import initialize_sql
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.add_route('home', '/')
config.scan('visionare.models') # the "important" line
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
config.add_static_view('static', 'static', cache_max_age=3600)
# other statements here
config.scan()
return config.make_wsgi_app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment