comparison rhodecode/controllers/admin/users.py @ 673:dd532af216d9 beta

#49 Enabled anonymous access for web interface controllable from permissions pannel
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 11 Nov 2010 01:05:43 +0100
parents 7e536d1af60d
children cb0d9ce6ac5c
comparison
equal deleted inserted replaced
670:e7c670cc03cb 673:dd532af216d9
43 class UsersController(BaseController): 43 class UsersController(BaseController):
44 """REST Controller styled on the Atom Publishing Protocol""" 44 """REST Controller styled on the Atom Publishing Protocol"""
45 # To properly map this controller, ensure your config/routing.py 45 # To properly map this controller, ensure your config/routing.py
46 # file has a resource setup: 46 # file has a resource setup:
47 # map.resource('user', 'users') 47 # map.resource('user', 'users')
48 48
49 @LoginRequired() 49 @LoginRequired()
50 @HasPermissionAllDecorator('hg.admin') 50 @HasPermissionAllDecorator('hg.admin')
51 def __before__(self): 51 def __before__(self):
52 c.admin_user = session.get('admin_user') 52 c.admin_user = session.get('admin_user')
53 c.admin_username = session.get('admin_username') 53 c.admin_username = session.get('admin_username')
54 super(UsersController, self).__before__() 54 super(UsersController, self).__before__()
55 55
56 56
57 def index(self, format='html'): 57 def index(self, format='html'):
58 """GET /users: All items in the collection""" 58 """GET /users: All items in the collection"""
59 # url('users') 59 # url('users')
60 60
61 c.users_list = self.sa.query(User).all() 61 c.users_list = self.sa.query(User).all()
62 return render('admin/users/users.html') 62 return render('admin/users/users.html')
63 63
64 def create(self): 64 def create(self):
65 """POST /users: Create a new item""" 65 """POST /users: Create a new item"""
66 # url('users') 66 # url('users')
67 67
68 user_model = UserModel() 68 user_model = UserModel()
69 login_form = UserForm()() 69 login_form = UserForm()()
70 try: 70 try:
71 form_result = login_form.to_python(dict(request.POST)) 71 form_result = login_form.to_python(dict(request.POST))
72 user_model.create(form_result) 72 user_model.create(form_result)
77 return htmlfill.render( 77 return htmlfill.render(
78 render('admin/users/user_add.html'), 78 render('admin/users/user_add.html'),
79 defaults=errors.value, 79 defaults=errors.value,
80 errors=errors.error_dict or {}, 80 errors=errors.error_dict or {},
81 prefix_error=False, 81 prefix_error=False,
82 encoding="UTF-8") 82 encoding="UTF-8")
83 except Exception: 83 except Exception:
84 log.error(traceback.format_exc()) 84 log.error(traceback.format_exc())
85 h.flash(_('error occured during creation of user %s') \ 85 h.flash(_('error occured during creation of user %s') \
86 % request.POST.get('username'), category='error') 86 % request.POST.get('username'), category='error')
87 return redirect(url('users')) 87 return redirect(url('users'))
88 88
89 def new(self, format='html'): 89 def new(self, format='html'):
90 """GET /users/new: Form to create a new item""" 90 """GET /users/new: Form to create a new item"""
91 # url('new_user') 91 # url('new_user')
92 return render('admin/users/user_add.html') 92 return render('admin/users/user_add.html')
93 93
99 # h.form(url('user', id=ID), 99 # h.form(url('user', id=ID),
100 # method='put') 100 # method='put')
101 # url('user', id=ID) 101 # url('user', id=ID)
102 user_model = UserModel() 102 user_model = UserModel()
103 c.user = user_model.get(id) 103 c.user = user_model.get(id)
104 104
105 _form = UserForm(edit=True, old_data={'user_id':id, 105 _form = UserForm(edit=True, old_data={'user_id':id,
106 'email':c.user.email})() 106 'email':c.user.email})()
107 form_result = {} 107 form_result = {}
108 try: 108 try:
109 form_result = _form.to_python(dict(request.POST)) 109 form_result = _form.to_python(dict(request.POST))
110 user_model.update(id, form_result) 110 user_model.update(id, form_result)
111 h.flash(_('User updated succesfully'), category='success') 111 h.flash(_('User updated succesfully'), category='success')
112 112
113 except formencode.Invalid, errors: 113 except formencode.Invalid, errors:
114 return htmlfill.render( 114 return htmlfill.render(
115 render('admin/users/user_edit.html'), 115 render('admin/users/user_edit.html'),
116 defaults=errors.value, 116 defaults=errors.value,
117 errors=errors.error_dict or {}, 117 errors=errors.error_dict or {},
118 prefix_error=False, 118 prefix_error=False,
119 encoding="UTF-8") 119 encoding="UTF-8")
120 except Exception: 120 except Exception:
121 log.error(traceback.format_exc()) 121 log.error(traceback.format_exc())
122 h.flash(_('error occured during update of user %s') \ 122 h.flash(_('error occured during update of user %s') \
123 % form_result.get('username'), category='error') 123 % form_result.get('username'), category='error')
124 124
125 return redirect(url('users')) 125 return redirect(url('users'))
126 126
127 def delete(self, id): 127 def delete(self, id):
128 """DELETE /users/id: Delete an existing item""" 128 """DELETE /users/id: Delete an existing item"""
129 # Forms posted to this method should contain a hidden field: 129 # Forms posted to this method should contain a hidden field:
130 # <input type="hidden" name="_method" value="DELETE" /> 130 # <input type="hidden" name="_method" value="DELETE" />
131 # Or using helpers: 131 # Or using helpers:
138 h.flash(_('sucessfully deleted user'), category='success') 138 h.flash(_('sucessfully deleted user'), category='success')
139 except DefaultUserException, e: 139 except DefaultUserException, e:
140 h.flash(str(e), category='warning') 140 h.flash(str(e), category='warning')
141 except Exception: 141 except Exception:
142 h.flash(_('An error occured during deletion of user'), 142 h.flash(_('An error occured during deletion of user'),
143 category='error') 143 category='error')
144 return redirect(url('users')) 144 return redirect(url('users'))
145 145
146 def show(self, id, format='html'): 146 def show(self, id, format='html'):
147 """GET /users/id: Show a specific item""" 147 """GET /users/id: Show a specific item"""
148 # url('user', id=ID) 148 # url('user', id=ID)
149 149
150 150
151 def edit(self, id, format='html'): 151 def edit(self, id, format='html'):
152 """GET /users/id/edit: Form to edit an existing item""" 152 """GET /users/id/edit: Form to edit an existing item"""
153 # url('edit_user', id=ID) 153 # url('edit_user', id=ID)
154 c.user = self.sa.query(User).get(id) 154 c.user = self.sa.query(User).get(id)
155 if not c.user: 155 if not c.user:
156 return redirect(url('users')) 156 return redirect(url('users'))
157 if c.user.username == 'default': 157 if c.user.username == 'default':
158 h.flash(_("You can't edit this user since it's" 158 h.flash(_("You can't edit this user"), category='warning')
159 " crucial for entire application"), category='warning')
160 return redirect(url('users')) 159 return redirect(url('users'))
161 160
162 defaults = c.user.__dict__ 161 defaults = c.user.__dict__
163 return htmlfill.render( 162 return htmlfill.render(
164 render('admin/users/user_edit.html'), 163 render('admin/users/user_edit.html'),
165 defaults=defaults, 164 defaults=defaults,
166 encoding="UTF-8", 165 encoding="UTF-8",
167 force_defaults=False 166 force_defaults=False
168 ) 167 )