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