import floof.lib.app_globals as app_globals
import floof.lib.helpers
+from floof import model
from floof.config.routing import make_map
-from floof.model import init_model
def load_environment(global_conf, app_conf):
"""Configure the Pylons environment via the ``pylons.config``
# Setup the SQLAlchemy database engine
engine = engine_from_config(config, 'sqlalchemy.')
- init_model(engine)
+
+ if model.elixir.options_defaults.get('autoload'):
+ model.elixir.bind = engine
+ model.metadata.bind = engine
+ model.elixir.setup_all()
+ else:
+ model.init_model(engine)
+
+ model.meta.engine = engine
# CONFIGURATION OPTIONS HERE (note: all config options will override
# any Pylons config options)
from pylons.controllers.util import abort, redirect_to
from floof.lib.base import BaseController, render
-from floof.model import meta, Art
+from floof.model import meta
+from floof.model.art import Art
log = logging.getLogger(__name__)
# WSGIController.__call__ dispatches to the Controller method
# the request is routed to. This routing information is
# available in environ['pylons.routes_dict']
- try:
- return WSGIController.__call__(self, environ, start_response)
- finally:
- meta.Session.remove()
+ return WSGIController.__call__(self, environ, start_response)
+#
+# floof/floof/model/__init__.py
+#
+# Copyright (c) 2009 Scribblr
+#
+# See: http://bel-epa.com/docs/elixir_pylons/
+#
+
"""The application's model objects"""
-import sqlalchemy as sa
-from sqlalchemy import orm
+from floof.model import art, users
from floof.model import meta
+import elixir
+
+elixir.session = meta.Session
+Session = elixir.session
+elixir.options_defaults.update({ 'autoload': True, 'shortnames': True })
-# Tables are defined in separate files and imported to here
-from floof.model.users import *
-from floof.model.art import *
+metadata = elixir.metadata
def init_model(engine):
- """Call me before using any of the tables or classes in the model"""
- ## Reflected tables must be defined and mapped here
- #global reflected_table
- #reflected_table = sa.Table("Reflected", meta.metadata, autoload=True,
- # autoload_with=engine)
- #orm.mapper(Reflected, reflected_table)
- #
- meta.Session.configure(bind=engine)
- meta.engine = engine
+ elixir.session.configure(bind=engine)
+ metadata.bind = engine
+
+ if elixir.options_defaults.get('autoload', False):
+ if not metadata.is_bound():
+ elixir.delay_setup = True
+ else:
+ elixir.setup_all(True)
-from sqlalchemy import Column, ForeignKey
-from sqlalchemy.orm import relation
-from sqlalchemy.types import Integer, Unicode
+#
+# floof/floof/model/art.py
+#
+# Copyright (c) 2009 Scribblr
+#
-from floof.model import meta
+from elixir import Entity, Field, Integer, Unicode
-__all__ = ['Art']
+class Art(Entity):
+ title = Field(Unicode(120))
-class Art(meta.TableBase):
- __tablename__ = 'art'
- id = Column(Integer, primary_key=True)
- title = Column(Unicode(length=120), nullable=False)
"""SQLAlchemy Metadata and Session object"""
from sqlalchemy import MetaData
-from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
-__all__ = ['Session', 'engine', 'TableBase']
+__all__ = ['Session', 'engine', 'metadata']
# SQLAlchemy database engine. Updated by model.init_model()
engine = None
# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker())
-# Base class for declarative; creates its own metadata object
-TableBase = declarative_base()
-metadata = TableBase.metadata
+metadata = MetaData()
+
-from sqlalchemy import Column, ForeignKey
-from sqlalchemy.orm import relation
-from sqlalchemy.types import Integer, Unicode
+#
+# floof/floof/model/users.py
+#
+# Copyright (c) 2009 Scribblr
+#
-from floof.model import meta
+from elixir import Entity, Field, Unicode, belongs_to, has_many
-__all__ = ['User', 'IdentityURL']
+class User(Entity):
+ name = Field(Unicode(20))
+ has_many('identity_urls', of_kind='IdentityURL')
-class User(meta.TableBase):
- __tablename__ = 'users'
- id = Column(Integer, primary_key=True)
- name = Column(Unicode(length=20), nullable=False)
+class IdentityURL(Entity):
+ url = Field(Unicode(255))
+ belongs_to('user', of_kind='User')
-class IdentityURL(meta.TableBase):
- __tablename__ = 'identity_urls'
- url = Column(Unicode(length=255), primary_key=True)
- user_id = Column(Integer, ForeignKey('users.id'))
-
-IdentityURL.user = relation(User, lazy=False, backref="identity_urls")
from floof.config.environment import load_environment
from floof.model import meta
+import elixir
log = logging.getLogger(__name__)
load_environment(conf.global_conf, conf.local_conf)
# Create the tables if they don't already exist
- meta.metadata.create_all(bind=meta.engine)
+ elixir.create_all(bind=meta.engine, checkfirst=False)