From: Eevee Date: Fri, 2 Oct 2009 23:35:43 +0000 (-0700) Subject: Switched existing sqla code over to elixir style. X-Git-Url: http://git.veekun.com/zzz-floof.git/commitdiff_plain/7913f8d5b2d58dbd8db4503378eddb00578e90a7?ds=sidebyside Switched existing sqla code over to elixir style. --- diff --git a/floof/controllers/account.py b/floof/controllers/account.py index a32583c..1dab66d 100644 --- a/floof/controllers/account.py +++ b/floof/controllers/account.py @@ -1,3 +1,4 @@ +import elixir import logging from openid.consumer.consumer import Consumer from openid.extensions.sreg import SRegRequest, SRegResponse @@ -8,9 +9,8 @@ from pylons import request, response, session, tmpl_context as c from pylons.controllers.util import abort, redirect_to from routes import url_for, request_config -from floof import model -from floof.model.meta import Session from floof.lib.base import BaseController, render +from floof.model.users import IdentityURL, User log = logging.getLogger(__name__) @@ -50,8 +50,7 @@ class AccountController(BaseController): try: # Grab an existing user record, if one exists - q = Session.query(model.User) \ - .filter(model.User.identity_urls.any(url=res.identity_url)) + q = User.query.filter(User.identity_urls.any(url=res.identity_url)) user = q.one() except NoResultFound: # Try to pull a name out of the SReg response @@ -62,14 +61,14 @@ class AccountController(BaseController): username = 'Anonymous' # Create db records - user = model.User(name=username) - Session.add(user) - identity_url = model.IdentityURL(url=res.identity_url) + user = User(name=username) + identity_url = IdentityURL(url=res.identity_url) user.identity_urls.append(identity_url) - Session.commit() + elixir.session.commit() # Remember who's logged in, and we're good to go session['user_id'] = user.id session.save() - return "Hello, %s from %s" % (user.name, res.identity_url) + # XXX send me where I came from + redirect_to('/') diff --git a/floof/controllers/main.py b/floof/controllers/main.py index e57e99a..1eb447c 100644 --- a/floof/controllers/main.py +++ b/floof/controllers/main.py @@ -1,10 +1,10 @@ import logging +import elixir 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 from floof.model.art import Art log = logging.getLogger(__name__) @@ -12,5 +12,5 @@ log = logging.getLogger(__name__) class MainController(BaseController): def index(self): - c.artwork = meta.Session.query(Art).order_by(Art.id.desc()).all() + c.artwork = Art.query.order_by(Art.id.desc()).all() return render('/index.mako') diff --git a/floof/lib/base.py b/floof/lib/base.py index af47069..4de5e19 100644 --- a/floof/lib/base.py +++ b/floof/lib/base.py @@ -6,15 +6,14 @@ from pylons import session, tmpl_context as c from pylons.controllers import WSGIController from pylons.templating import render_mako as render -from floof import model -from floof.model import meta +from floof.model.users import User class BaseController(WSGIController): def __before__(self, action, **params): # Fetch current user object try: - c.user = meta.Session.query(model.User).get(session['user_id']) + c.user = User.query.get(session['user_id']) except: pass diff --git a/floof/model/__init__.py b/floof/model/__init__.py index 22ceeaf..5e963e2 100644 --- a/floof/model/__init__.py +++ b/floof/model/__init__.py @@ -12,8 +12,6 @@ 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 }) metadata = elixir.metadata diff --git a/floof/model/meta.py b/floof/model/meta.py index 3b3b1c5..7992ce1 100644 --- a/floof/model/meta.py +++ b/floof/model/meta.py @@ -1,14 +1,11 @@ -"""SQLAlchemy Metadata and Session object""" +"""SQLAlchemy Metadata object""" from sqlalchemy import MetaData from sqlalchemy.orm import scoped_session, sessionmaker -__all__ = ['Session', 'engine', 'metadata'] +__all__ = ['engine', 'metadata'] # SQLAlchemy database engine. Updated by model.init_model() engine = None -# SQLAlchemy session manager. Updated by model.init_model() -Session = scoped_session(sessionmaker()) - metadata = MetaData()