comparison rhodecode/controllers/admin/users.py @ 547:1e757ac98988

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