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