a6a6ca79fbd6ea99a44928413dd387987f1f0d7c
[zzz-spline-users.git] / splinext / users / __init__.py
1 from pkg_resources import resource_filename
2
3 from pylons import c, session
4
5 from spline.lib.plugin import PluginBase
6 from spline.lib.plugin import PluginBase, PluginLink, Priority
7 import spline.model.meta as meta
8
9 import splinext.users.controllers.accounts
10 import splinext.users.controllers.users
11 from splinext.users import model as users_model
12
13 def add_routes_hook(map, *args, **kwargs):
14 """Hook to inject some of our behavior into the routes configuration."""
15 # Login, logout
16 map.connect('/accounts/login', controller='accounts', action='login')
17 map.connect('/accounts/login_begin', controller='accounts', action='login_begin')
18 map.connect('/accounts/login_finish', controller='accounts', action='login_finish')
19 map.connect('/accounts/logout', controller='accounts', action='logout')
20
21 # Self-admin
22 map.connect('/users/{id};{name}/edit', controller='users', action='profile_edit')
23
24 # Public user pages
25 map.connect('/users', controller='users', action='list')
26 map.connect('/users/{id};{name}', controller='users', action='profile')
27 map.connect('/users/{id}', controller='users', action='profile')
28
29 def check_userid_hook(action, **params):
30 """Hook to see if a user is logged in and, if so, stick a user object in
31 c.
32 """
33
34 if not 'user_id' in session:
35 c.user = users_model.AnonymousUser()
36 return
37
38 user = meta.Session.query(users_model.User).get(session['user_id'])
39 if not user:
40 # Bogus id in the session somehow. Clear it
41 del session['user_id']
42 session.save()
43
44 c.user = users_model.AnonymousUser()
45 return
46
47 c.user = user
48
49
50 class UsersPlugin(PluginBase):
51 def controllers(self):
52 return dict(
53 accounts = splinext.users.controllers.accounts.AccountsController,
54 users = splinext.users.controllers.users.UsersController,
55 )
56
57 def template_dirs(self):
58 return [
59 (resource_filename(__name__, 'templates'), Priority.NORMAL)
60 ]
61
62 def hooks(self):
63 return [
64 ('routes_mapping', Priority.NORMAL, add_routes_hook),
65 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
66 ]
67
68 def widgets(self):
69 return [
70 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
71 ]