##// END OF EJS Templates
protected admin controllers
marcink -
r305:61be6dcd default
parent child Browse files
Show More
@@ -1,54 +1,52 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 # admin controller for pylons
4 4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 5
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; version 2
9 9 # of the License or (at your opinion) any later version of the license.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19 # MA 02110-1301, USA.
20 20 """
21 21 Created on April 7, 2010
22 22 admin controller for pylons
23 23 @author: marcink
24 24 """
25 25 import logging
26 26 from pylons import request, response, session, tmpl_context as c
27 27 from pylons_app.lib.base import BaseController, render
28 28 from pylons_app.model import meta
29 29 from pylons_app.model.db import UserLog
30 30 from webhelpers.paginate import Page
31 from pylons_app.lib.auth import LoginRequired
31 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
32 32
33 33 log = logging.getLogger(__name__)
34 34
35 35 class AdminController(BaseController):
36 36
37 37 @LoginRequired()
38 38 def __before__(self):
39 user = session['hg_app_user']
40 c.admin_user = user.is_admin
41 c.admin_username = user.username
42 39 super(AdminController, self).__before__()
43 40
41 @HasPermissionAllDecorator('hg.admin')
44 42 def index(self):
45 43 sa = meta.Session
46 44
47 45 users_log = sa.query(UserLog).order_by(UserLog.action_date.desc())
48 46 p = int(request.params.get('page', 1))
49 47 c.users_log = Page(users_log, page=p, items_per_page=10)
50 48 c.log_data = render('admin/admin_log.html')
51 49 if request.params.get('partial'):
52 50 return c.log_data
53 51 return render('admin/admin.html')
54 52
@@ -1,77 +1,90 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 # permissions controller for pylons
4 4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 5
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; version 2
9 9 # of the License or (at your opinion) any later version of the license.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19 # MA 02110-1301, USA.
20 20 """
21 21 Created on April 27, 2010
22 22 permissions controller for pylons
23 23 @author: marcink
24 24 """
25 from formencode import htmlfill
26 from pylons import request, session, tmpl_context as c, url
27 from pylons.controllers.util import abort, redirect
28 from pylons.i18n.translation import _
29 from pylons_app.lib import helpers as h
30 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
31 from pylons_app.lib.base import BaseController, render
32 from pylons_app.model.db import User, UserLog
33 from pylons_app.model.forms import UserForm
34 from pylons_app.model.user_model import UserModel
35 import formencode
25 36 import logging
26 37
27 from pylons import request, response, session, tmpl_context as c, url
28 from pylons.controllers.util import abort, redirect
29
30 from pylons_app.lib.base import BaseController, render
31
32 38 log = logging.getLogger(__name__)
33 39
34 40 class PermissionsController(BaseController):
35 41 """REST Controller styled on the Atom Publishing Protocol"""
36 42 # To properly map this controller, ensure your config/routing.py
37 43 # file has a resource setup:
38 44 # map.resource('permission', 'permissions')
39 45
46 @LoginRequired()
47 @HasPermissionAllDecorator('hg.admin')
48 def __before__(self):
49 c.admin_user = session.get('admin_user')
50 c.admin_username = session.get('admin_username')
51 super(PermissionsController, self).__before__()
52
40 53 def index(self, format='html'):
41 54 """GET /permissions: All items in the collection"""
42 55 # url('permissions')
43 56 return render('admin/permissions/permissions.html')
44 57
45 58 def create(self):
46 59 """POST /permissions: Create a new item"""
47 60 # url('permissions')
48 61
49 62 def new(self, format='html'):
50 63 """GET /permissions/new: Form to create a new item"""
51 64 # url('new_permission')
52 65
53 66 def update(self, id):
54 67 """PUT /permissions/id: Update an existing item"""
55 68 # Forms posted to this method should contain a hidden field:
56 69 # <input type="hidden" name="_method" value="PUT" />
57 70 # Or using helpers:
58 71 # h.form(url('permission', id=ID),
59 72 # method='put')
60 73 # url('permission', id=ID)
61 74
62 75 def delete(self, id):
63 76 """DELETE /permissions/id: Delete an existing item"""
64 77 # Forms posted to this method should contain a hidden field:
65 78 # <input type="hidden" name="_method" value="DELETE" />
66 79 # Or using helpers:
67 80 # h.form(url('permission', id=ID),
68 81 # method='delete')
69 82 # url('permission', id=ID)
70 83
71 84 def show(self, id, format='html'):
72 85 """GET /permissions/id: Show a specific item"""
73 86 # url('permission', id=ID)
74 87
75 88 def edit(self, id, format='html'):
76 89 """GET /permissions/id/edit: Form to edit an existing item"""
77 90 # url('edit_permission', id=ID)
@@ -1,147 +1,149 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 # users controller for pylons
4 4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 5
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; version 2
9 9 # of the License or (at your opinion) any later version of the license.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19 # MA 02110-1301, USA.
20 20 """
21 21 Created on April 4, 2010
22 22 users controller for pylons
23 23 @author: marcink
24 24 """
25 import logging
25 from formencode import htmlfill
26 26 from pylons import request, session, tmpl_context as c, url
27 27 from pylons.controllers.util import abort, redirect
28 28 from pylons.i18n.translation import _
29 29 from pylons_app.lib import helpers as h
30 from pylons_app.lib.auth import LoginRequired
30 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
31 31 from pylons_app.lib.base import BaseController, render
32 32 from pylons_app.model.db import User, UserLog
33 33 from pylons_app.model.forms import UserForm
34 34 from pylons_app.model.user_model import UserModel
35 35 import formencode
36 from formencode import htmlfill
36 import logging
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40 class UsersController(BaseController):
41 41 """REST Controller styled on the Atom Publishing Protocol"""
42 42 # To properly map this controller, ensure your config/routing.py
43 43 # file has a resource setup:
44 44 # map.resource('user', 'users')
45
45 46 @LoginRequired()
47 @HasPermissionAllDecorator('hg.admin')
46 48 def __before__(self):
47 49 c.admin_user = session.get('admin_user')
48 50 c.admin_username = session.get('admin_username')
49 51 super(UsersController, self).__before__()
50 52
51 53
52 54 def index(self, format='html'):
53 55 """GET /users: All items in the collection"""
54 56 # url('users')
55 57
56 58 c.users_list = self.sa.query(User).all()
57 59 return render('admin/users/users.html')
58 60
59 61 def create(self):
60 62 """POST /users: Create a new item"""
61 63 # url('users')
62 64
63 65 user_model = UserModel()
64 66 login_form = UserForm()()
65 67 try:
66 68 form_result = login_form.to_python(dict(request.POST))
67 69 user_model.create(form_result)
68 70 h.flash(_('created user %s') % form_result['username'],
69 71 category='success')
70 72 except formencode.Invalid as errors:
71 73 c.form_errors = errors.error_dict
72 74 return htmlfill.render(
73 75 render('admin/users/user_add.html'),
74 76 defaults=errors.value,
75 77 encoding="UTF-8")
76 78 except Exception:
77 79 h.flash(_('error occured during creation of user %s') \
78 80 % form_result['username'], category='error')
79 81 return redirect(url('users'))
80 82
81 83 def new(self, format='html'):
82 84 """GET /users/new: Form to create a new item"""
83 85 # url('new_user')
84 86 return render('admin/users/user_add.html')
85 87
86 88 def update(self, id):
87 89 """PUT /users/id: Update an existing item"""
88 90 # Forms posted to this method should contain a hidden field:
89 91 # <input type="hidden" name="_method" value="PUT" />
90 92 # Or using helpers:
91 93 # h.form(url('user', id=ID),
92 94 # method='put')
93 95 # url('user', id=ID)
94 96 user_model = UserModel()
95 97 _form = UserForm(edit=True)()
96 98 try:
97 99 form_result = _form.to_python(dict(request.POST))
98 100 user_model.update(id, form_result)
99 101 h.flash(_('User updated succesfully'), category='success')
100 102
101 103 except formencode.Invalid as errors:
102 104 c.user = user_model.get_user(id)
103 105 c.form_errors = errors.error_dict
104 106 return htmlfill.render(
105 107 render('admin/users/user_edit.html'),
106 108 defaults=errors.value,
107 109 encoding="UTF-8")
108 110 except Exception:
109 111 h.flash(_('error occured during update of user %s') \
110 112 % form_result['username'], category='error')
111 113
112 114 return redirect(url('users'))
113 115
114 116 def delete(self, id):
115 117 """DELETE /users/id: Delete an existing item"""
116 118 # Forms posted to this method should contain a hidden field:
117 119 # <input type="hidden" name="_method" value="DELETE" />
118 120 # Or using helpers:
119 121 # h.form(url('user', id=ID),
120 122 # method='delete')
121 123 # url('user', id=ID)
122 124 user_model = UserModel()
123 125 try:
124 126 user_model.delete(id)
125 127 h.flash(_('sucessfully deleted user'), category='success')
126 128 except Exception:
127 129 h.flash(_('An error occured during deletion of user'),
128 130 category='error')
129 131
130 132 return redirect(url('users'))
131 133
132 134 def show(self, id, format='html'):
133 135 """GET /users/id: Show a specific item"""
134 136 # url('user', id=ID)
135 137
136 138
137 139 def edit(self, id, format='html'):
138 140 """GET /users/id/edit: Form to edit an existing item"""
139 141 # url('edit_user', id=ID)
140 142 c.user = self.sa.query(User).get(id)
141 143 defaults = c.user.__dict__
142 144 return htmlfill.render(
143 145 render('admin/users/user_edit.html'),
144 146 defaults=defaults,
145 147 encoding="UTF-8",
146 148 force_defaults=False
147 149 )
General Comments 0
You need to be logged in to leave comments. Login now