5e006cf5baf0d74bdc461c8f79cb7429d16984b0
[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 map.connect('/accounts/login_begin', controller='accounts', action='login_begin')
17 map.connect('/accounts/login_finish', controller='accounts', action='login_finish')
18 map.connect('/accounts/logout', controller='accounts', action='logout')
19
20 map.connect('/users/{id};{name}', controller='users', action='view')
21 map.connect('/users/{id}', controller='users', action='view')
22
23 def check_userid_hook(action, **params):
24 """Hook to see if a user is logged in and, if so, stick a user object in
25 c.
26 """
27
28 c.user = None
29
30 if not 'user_id' in session:
31 return
32
33 user = meta.Session.query(model.User).get(session['user_id'])
34 if not user:
35 # Bogus id in the session somehow. Clear it
36 del session['user_id']
37 session.save()
38 return
39
40 c.user = user
41
42
43 class UsersPlugin(PluginBase):
44 def controllers(self):
45 return dict(
46 accounts = spline.plugins.users.controllers.accounts.AccountsController,
47 users = spline.plugins.users.controllers.users.UsersController,
48 )
49
50 def model(self):
51 return [
52 spline.plugins.users.model.User,
53 spline.plugins.users.model.OpenID,
54 ]
55
56 def template_dirs(self):
57 return [
58 (resource_filename(__name__, 'templates'), Priority.NORMAL)
59 ]
60
61 def hooks(self):
62 return [
63 ('routes_mapping', Priority.NORMAL, add_routes_hook),
64 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
65 ]
66
67 def widgets(self):
68 return [
69 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
70 ]