Updated login to work correctly with current spline. #69
authorEevee <git@veekun.com>
Thu, 22 Apr 2010 06:09:07 +0000 (23:09 -0700)
committerEevee <git@veekun.com>
Fri, 23 Apr 2010 04:37:10 +0000 (21:37 -0700)
migration/versions/001_Add_user_and_openid_tables.py [new file with mode: 0644]
migration/versions/__init__.py [new file with mode: 0755]
spline/plugins/users/__init__.py
spline/plugins/users/controllers/accounts.py
spline/plugins/users/templates/widgets/user_state.mako [new file with mode: 0644]

diff --git a/migration/versions/001_Add_user_and_openid_tables.py b/migration/versions/001_Add_user_and_openid_tables.py
new file mode 100644 (file)
index 0000000..473d3df
--- /dev/null
@@ -0,0 +1,25 @@
+from sqlalchemy import *
+from migrate import *
+
+from sqlalchemy.ext.declarative import declarative_base
+TableBase = declarative_base(bind=migrate_engine)
+
+
+class User(TableBase):
+    __tablename__ = 'users'
+    id = Column(Integer, primary_key=True)
+    name = Column(Unicode(length=20), nullable=False)
+
+class OpenID(TableBase):
+    __tablename__ = 'openid'
+    openid = Column(Unicode(length=255), primary_key=True)
+    user_id = Column(Integer, ForeignKey('users.id'))
+
+
+def upgrade():
+    User.__table__.create()
+    OpenID.__table__.create()
+
+def downgrade():
+    OpenID.__table__.drop()
+    User.__table__.drop()
diff --git a/migration/versions/__init__.py b/migration/versions/__init__.py
new file mode 100755 (executable)
index 0000000..e69de29
index 53633d7..92a825c 100644 (file)
@@ -1,17 +1,27 @@
+from pkg_resources import resource_filename
+
 from pylons import c, session
 
 from spline.lib.plugin import PluginBase
+from spline.lib.plugin import PluginBase, PluginLink, Priority
 import spline.model as model
 import spline.model.meta as meta
 
-import controllers.accounts
-import model as user_model
+import spline.plugins.users.controllers.accounts
+import spline.plugins.users.model
+
+def add_routes_hook(map, *args, **kwargs):
+    """Hook to inject some of our behavior into the routes configuration."""
+    map.connect('/accounts/login_begin', controller='accounts', action='login_begin')
+    map.connect('/accounts/login_finish', controller='accounts', action='login_finish')
 
 def check_userid_hook(action, **params):
     """Hook to see if a user is logged in and, if so, stick a user object in
     c.
     """
 
+    c.user = None
+
     if not 'user_id' in session:
         return
 
@@ -28,13 +38,27 @@ def check_userid_hook(action, **params):
 class UsersPlugin(PluginBase):
     def controllers(self):
         return dict(
-            accounts = controllers.accounts.AccountsController,
+            accounts = spline.plugins.users.controllers.accounts.AccountsController,
         )
 
     def model(self):
-        return [user_model.User, user_model.OpenID]
+        return [
+            spline.plugins.users.model.User,
+            spline.plugins.users.model.OpenID,
+        ]
+
+    def template_dirs(self):
+        return [
+            (resource_filename(__name__, 'templates'), Priority.NORMAL)
+        ]
 
     def hooks(self):
         return [
-            ('before_controller', 1, check_userid_hook),
+            ('routes_mapping',    Priority.NORMAL,      add_routes_hook),
+            ('before_controller', Priority.VERY_FIRST,  check_userid_hook),
+        ]
+
+    def widgets(self):
+        return [
+            ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
         ]
index 09ca1ac..03c9392 100644 (file)
@@ -61,7 +61,8 @@ class AccountsController(BaseController):
             sreg_res = SRegResponse.fromSuccessResponse(res)
             try:
                 username = sreg_res['nickname']
-            except KeyError:
+            except (KeyError, TypeError):
+                # KeyError if sreg has no nickname; TypeError if sreg is None
                 username = 'Anonymous'
 
             # Create db records
@@ -75,4 +76,4 @@ class AccountsController(BaseController):
         session['user_id'] = user.id
         session.save()
 
-        return "Hello, %s" % user.name
+        redirect_to(url('/'))
diff --git a/spline/plugins/users/templates/widgets/user_state.mako b/spline/plugins/users/templates/widgets/user_state.mako
new file mode 100644 (file)
index 0000000..d279dc4
--- /dev/null
@@ -0,0 +1,9 @@
+${h.form(url(controller='accounts', action='login_begin'), id='user')}
+    % if c.user:
+    Logged in as ${c.user.name}.
+    % else:
+    <img src="${h.static_uri('spline', 'icons/openid.png')}">
+    <input type="text" name="openid" size="30">
+    <input type="submit" value="Log in">
+    % endif
+${h.end_form()}