4ce44653fac8e4ccead2221c636b13af1d0b266d
[zzz-spline-users.git] / spline / plugins / 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 as model
8 import spline.model.meta as meta
9
10 import spline.plugins.users.controllers.accounts
11 import spline.plugins.users.controllers.users
12 import spline.plugins.users.model
13
14 def add_routes_hook(map, *args, **kwargs):
15 """Hook to inject some of our behavior into the routes configuration."""
16 # Login, logout
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 per-user pages
25 map.connect('/users/{id};{name}', controller='users', action='profile')
26 map.connect('/users/{id}', controller='users', action='profile')
27
28 def check_userid_hook(action, **params):
29 """Hook to see if a user is logged in and, if so, stick a user object in
30 c.
31 """
32
33 c.user = None
34
35 if not 'user_id' in session:
36 return
37
38 user = meta.Session.query(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 return
44
45 c.user = user
46
47
48 class UsersPlugin(PluginBase):
49 def controllers(self):
50 return dict(
51 accounts = spline.plugins.users.controllers.accounts.AccountsController,
52 users = spline.plugins.users.controllers.users.UsersController,
53 )
54
55 def model(self):
56 return [
57 spline.plugins.users.model.User,
58 spline.plugins.users.model.OpenID,
59 ]
60
61 def template_dirs(self):
62 return [
63 (resource_filename(__name__, 'templates'), Priority.NORMAL)
64 ]
65
66 def hooks(self):
67 return [
68 ('routes_mapping', Priority.NORMAL, add_routes_hook),
69 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
70 ]
71
72 def widgets(self):
73 return [
74 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
75 ]