From a058598f1ee11c60f9718d0ec677df1424be79c3 Mon Sep 17 00:00:00 2001 From: ootachi Date: Tue, 29 Sep 2009 01:41:25 -0700 Subject: [PATCH] Switch over to Elixir --- floof/config/environment.py | 12 ++++++++++-- floof/controllers/main.py | 3 ++- floof/lib/base.py | 5 +---- floof/model/__init__.py | 37 +++++++++++++++++++++++-------------- floof/model/art.py | 17 ++++++++--------- floof/model/meta.py | 8 +++----- floof/model/users.py | 27 ++++++++++++--------------- floof/websetup.py | 3 ++- 8 files changed, 61 insertions(+), 51 deletions(-) diff --git a/floof/config/environment.py b/floof/config/environment.py index 1ee51b8..20683db 100644 --- a/floof/config/environment.py +++ b/floof/config/environment.py @@ -8,8 +8,8 @@ from sqlalchemy import engine_from_config 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`` @@ -39,7 +39,15 @@ def load_environment(global_conf, app_conf): # 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) diff --git a/floof/controllers/main.py b/floof/controllers/main.py index 2b8b877..e57e99a 100644 --- a/floof/controllers/main.py +++ b/floof/controllers/main.py @@ -4,7 +4,8 @@ from pylons import request, response, session, tmpl_context as c 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__) diff --git a/floof/lib/base.py b/floof/lib/base.py index d2756ba..af47069 100644 --- a/floof/lib/base.py +++ b/floof/lib/base.py @@ -23,7 +23,4 @@ class BaseController(WSGIController): # 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) diff --git a/floof/model/__init__.py b/floof/model/__init__.py index d0f03b3..22ceeaf 100644 --- a/floof/model/__init__.py +++ b/floof/model/__init__.py @@ -1,21 +1,30 @@ +# +# 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) diff --git a/floof/model/art.py b/floof/model/art.py index 8cfcd5a..517fea0 100644 --- a/floof/model/art.py +++ b/floof/model/art.py @@ -1,12 +1,11 @@ -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) diff --git a/floof/model/meta.py b/floof/model/meta.py index 03d1f92..3b3b1c5 100644 --- a/floof/model/meta.py +++ b/floof/model/meta.py @@ -1,9 +1,8 @@ """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 @@ -11,6 +10,5 @@ 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() + diff --git a/floof/model/users.py b/floof/model/users.py index f51282c..0f05a52 100644 --- a/floof/model/users.py +++ b/floof/model/users.py @@ -1,19 +1,16 @@ -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") diff --git a/floof/websetup.py b/floof/websetup.py index a812633..705c342 100644 --- a/floof/websetup.py +++ b/floof/websetup.py @@ -3,6 +3,7 @@ import logging from floof.config.environment import load_environment from floof.model import meta +import elixir log = logging.getLogger(__name__) @@ -11,4 +12,4 @@ def setup_app(command, conf, vars): 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) -- 2.7.4