Show More
@@ -1,155 +1,156 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 | 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 | 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, DefaultUserException |
|
35 | 35 | import formencode |
|
36 | 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 | 46 | @LoginRequired() |
|
47 | 47 | @HasPermissionAllDecorator('hg.admin') |
|
48 | 48 | def __before__(self): |
|
49 | 49 | c.admin_user = session.get('admin_user') |
|
50 | 50 | c.admin_username = session.get('admin_username') |
|
51 | 51 | super(UsersController, self).__before__() |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def index(self, format='html'): |
|
55 | 55 | """GET /users: All items in the collection""" |
|
56 | 56 | # url('users') |
|
57 | 57 | |
|
58 | 58 | c.users_list = self.sa.query(User).all() |
|
59 | 59 | return render('admin/users/users.html') |
|
60 | 60 | |
|
61 | 61 | def create(self): |
|
62 | 62 | """POST /users: Create a new item""" |
|
63 | 63 | # url('users') |
|
64 | 64 | |
|
65 | 65 | user_model = UserModel() |
|
66 | 66 | login_form = UserForm()() |
|
67 | 67 | try: |
|
68 | 68 | form_result = login_form.to_python(dict(request.POST)) |
|
69 | 69 | user_model.create(form_result) |
|
70 | 70 | h.flash(_('created user %s') % form_result['username'], |
|
71 | 71 | category='success') |
|
72 | 72 | except formencode.Invalid as errors: |
|
73 | 73 | c.form_errors = errors.error_dict |
|
74 | 74 | return htmlfill.render( |
|
75 | 75 | render('admin/users/user_add.html'), |
|
76 | 76 | defaults=errors.value, |
|
77 | 77 | encoding="UTF-8") |
|
78 | 78 | except Exception: |
|
79 | h.flash(_('error occured during creation of user %s') \ | |
|
80 | % form_result['username'], category='error') | |
|
79 | ||
|
80 | h.flash(_('error occured during creation of user') \ | |
|
81 | % request.POST.get('username'), category='error') | |
|
81 | 82 | return redirect(url('users')) |
|
82 | 83 | |
|
83 | 84 | def new(self, format='html'): |
|
84 | 85 | """GET /users/new: Form to create a new item""" |
|
85 | 86 | # url('new_user') |
|
86 | 87 | return render('admin/users/user_add.html') |
|
87 | 88 | |
|
88 | 89 | def update(self, id): |
|
89 | 90 | """PUT /users/id: Update an existing item""" |
|
90 | 91 | # Forms posted to this method should contain a hidden field: |
|
91 | 92 | # <input type="hidden" name="_method" value="PUT" /> |
|
92 | 93 | # Or using helpers: |
|
93 | 94 | # h.form(url('user', id=ID), |
|
94 | 95 | # method='put') |
|
95 | 96 | # url('user', id=ID) |
|
96 | 97 | user_model = UserModel() |
|
97 | 98 | _form = UserForm(edit=True)() |
|
98 | 99 | try: |
|
99 | 100 | form_result = _form.to_python(dict(request.POST)) |
|
100 | 101 | user_model.update(id, form_result) |
|
101 | 102 | h.flash(_('User updated succesfully'), category='success') |
|
102 | 103 | |
|
103 | 104 | except formencode.Invalid as errors: |
|
104 | 105 | c.user = user_model.get_user(id) |
|
105 | 106 | c.form_errors = errors.error_dict |
|
106 | 107 | return htmlfill.render( |
|
107 | 108 | render('admin/users/user_edit.html'), |
|
108 | 109 | defaults=errors.value, |
|
109 | 110 | encoding="UTF-8") |
|
110 | 111 | except Exception: |
|
111 | 112 | h.flash(_('error occured during update of user %s') \ |
|
112 | 113 | % form_result['username'], category='error') |
|
113 | 114 | |
|
114 | 115 | return redirect(url('users')) |
|
115 | 116 | |
|
116 | 117 | def delete(self, id): |
|
117 | 118 | """DELETE /users/id: Delete an existing item""" |
|
118 | 119 | # Forms posted to this method should contain a hidden field: |
|
119 | 120 | # <input type="hidden" name="_method" value="DELETE" /> |
|
120 | 121 | # Or using helpers: |
|
121 | 122 | # h.form(url('user', id=ID), |
|
122 | 123 | # method='delete') |
|
123 | 124 | # url('user', id=ID) |
|
124 | 125 | user_model = UserModel() |
|
125 | 126 | try: |
|
126 | 127 | user_model.delete(id) |
|
127 | 128 | h.flash(_('sucessfully deleted user'), category='success') |
|
128 | 129 | except DefaultUserException as e: |
|
129 | 130 | h.flash(str(e), category='warning') |
|
130 | 131 | except Exception: |
|
131 | 132 | h.flash(_('An error occured during deletion of user'), |
|
132 | 133 | category='error') |
|
133 | 134 | return redirect(url('users')) |
|
134 | 135 | |
|
135 | 136 | def show(self, id, format='html'): |
|
136 | 137 | """GET /users/id: Show a specific item""" |
|
137 | 138 | # url('user', id=ID) |
|
138 | 139 | |
|
139 | 140 | |
|
140 | 141 | def edit(self, id, format='html'): |
|
141 | 142 | """GET /users/id/edit: Form to edit an existing item""" |
|
142 | 143 | # url('edit_user', id=ID) |
|
143 | 144 | c.user = self.sa.query(User).get(id) |
|
144 | 145 | if c.user.username == 'default': |
|
145 | 146 | h.flash(_("You can't edit this user since it's" |
|
146 | 147 | " crucial for entire application"), category='warning') |
|
147 | 148 | return redirect(url('users')) |
|
148 | 149 | |
|
149 | 150 | defaults = c.user.__dict__ |
|
150 | 151 | return htmlfill.render( |
|
151 | 152 | render('admin/users/user_edit.html'), |
|
152 | 153 | defaults=defaults, |
|
153 | 154 | encoding="UTF-8", |
|
154 | 155 | force_defaults=False |
|
155 | 156 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now