Mercurial > kallithea
comparison rhodecode/controllers/admin/repos.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/repos.py@48be953851fc |
children | b75b77ef649d |
comparison
equal
deleted
inserted
replaced
546:7c2f5e4d7bbf | 547:1e757ac98988 |
---|---|
1 #!/usr/bin/env python | |
2 # encoding: utf-8 | |
3 # repos 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 """ | |
21 Created on April 7, 2010 | |
22 admin controller for pylons | |
23 @author: marcink | |
24 """ | |
25 from formencode import htmlfill | |
26 from operator import itemgetter | |
27 from paste.httpexceptions import HTTPInternalServerError | |
28 from pylons import request, response, 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 HasPermissionAnyDecorator | |
34 from rhodecode.lib.base import BaseController, render | |
35 from rhodecode.lib.utils import invalidate_cache, action_logger | |
36 from rhodecode.model.db import User | |
37 from rhodecode.model.forms import RepoForm | |
38 from rhodecode.model.hg_model import HgModel | |
39 from rhodecode.model.repo_model import RepoModel | |
40 import formencode | |
41 import logging | |
42 import traceback | |
43 | |
44 log = logging.getLogger(__name__) | |
45 | |
46 class ReposController(BaseController): | |
47 """REST Controller styled on the Atom Publishing Protocol""" | |
48 # To properly map this controller, ensure your config/routing.py | |
49 # file has a resource setup: | |
50 # map.resource('repo', 'repos') | |
51 | |
52 @LoginRequired() | |
53 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
54 def __before__(self): | |
55 c.admin_user = session.get('admin_user') | |
56 c.admin_username = session.get('admin_username') | |
57 super(ReposController, self).__before__() | |
58 | |
59 @HasPermissionAllDecorator('hg.admin') | |
60 def index(self, format='html'): | |
61 """GET /repos: All items in the collection""" | |
62 # url('repos') | |
63 cached_repo_list = HgModel().get_repos() | |
64 c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort')) | |
65 return render('admin/repos/repos.html') | |
66 | |
67 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
68 def create(self): | |
69 """POST /repos: Create a new item""" | |
70 # url('repos') | |
71 repo_model = RepoModel() | |
72 _form = RepoForm()() | |
73 form_result = {} | |
74 try: | |
75 form_result = _form.to_python(dict(request.POST)) | |
76 repo_model.create(form_result, c.hg_app_user) | |
77 invalidate_cache('cached_repo_list') | |
78 h.flash(_('created repository %s') % form_result['repo_name'], | |
79 category='success') | |
80 | |
81 if request.POST.get('user_created'): | |
82 action_logger(self.hg_app_user, 'user_created_repo', | |
83 form_result['repo_name'], '', self.sa) | |
84 else: | |
85 action_logger(self.hg_app_user, 'admin_created_repo', | |
86 form_result['repo_name'], '', self.sa) | |
87 | |
88 except formencode.Invalid as errors: | |
89 c.new_repo = errors.value['repo_name'] | |
90 | |
91 if request.POST.get('user_created'): | |
92 r = render('admin/repos/repo_add_create_repository.html') | |
93 else: | |
94 r = render('admin/repos/repo_add.html') | |
95 | |
96 return htmlfill.render( | |
97 r, | |
98 defaults=errors.value, | |
99 errors=errors.error_dict or {}, | |
100 prefix_error=False, | |
101 encoding="UTF-8") | |
102 | |
103 except Exception: | |
104 log.error(traceback.format_exc()) | |
105 msg = _('error occured during creation of repository %s') \ | |
106 % form_result.get('repo_name') | |
107 h.flash(msg, category='error') | |
108 if request.POST.get('user_created'): | |
109 return redirect(url('hg_home')) | |
110 return redirect(url('repos')) | |
111 | |
112 @HasPermissionAllDecorator('hg.admin') | |
113 def new(self, format='html'): | |
114 """GET /repos/new: Form to create a new item""" | |
115 new_repo = request.GET.get('repo', '') | |
116 c.new_repo = h.repo_name_slug(new_repo) | |
117 | |
118 return render('admin/repos/repo_add.html') | |
119 | |
120 @HasPermissionAllDecorator('hg.admin') | |
121 def update(self, repo_name): | |
122 """PUT /repos/repo_name: Update an existing item""" | |
123 # Forms posted to this method should contain a hidden field: | |
124 # <input type="hidden" name="_method" value="PUT" /> | |
125 # Or using helpers: | |
126 # h.form(url('repo', repo_name=ID), | |
127 # method='put') | |
128 # url('repo', repo_name=ID) | |
129 repo_model = RepoModel() | |
130 changed_name = repo_name | |
131 _form = RepoForm(edit=True, old_data={'repo_name':repo_name})() | |
132 | |
133 try: | |
134 form_result = _form.to_python(dict(request.POST)) | |
135 repo_model.update(repo_name, form_result) | |
136 invalidate_cache('cached_repo_list') | |
137 h.flash(_('Repository %s updated succesfully' % repo_name), | |
138 category='success') | |
139 changed_name = form_result['repo_name'] | |
140 except formencode.Invalid as errors: | |
141 c.repo_info = repo_model.get(repo_name) | |
142 c.users_array = repo_model.get_users_js() | |
143 errors.value.update({'user':c.repo_info.user.username}) | |
144 return htmlfill.render( | |
145 render('admin/repos/repo_edit.html'), | |
146 defaults=errors.value, | |
147 errors=errors.error_dict or {}, | |
148 prefix_error=False, | |
149 encoding="UTF-8") | |
150 | |
151 except Exception: | |
152 log.error(traceback.format_exc()) | |
153 h.flash(_('error occured during update of repository %s') \ | |
154 % repo_name, category='error') | |
155 | |
156 return redirect(url('edit_repo', repo_name=changed_name)) | |
157 | |
158 @HasPermissionAllDecorator('hg.admin') | |
159 def delete(self, repo_name): | |
160 """DELETE /repos/repo_name: Delete an existing item""" | |
161 # Forms posted to this method should contain a hidden field: | |
162 # <input type="hidden" name="_method" value="DELETE" /> | |
163 # Or using helpers: | |
164 # h.form(url('repo', repo_name=ID), | |
165 # method='delete') | |
166 # url('repo', repo_name=ID) | |
167 | |
168 repo_model = RepoModel() | |
169 repo = repo_model.get(repo_name) | |
170 if not repo: | |
171 h.flash(_('%s repository is not mapped to db perhaps' | |
172 ' it was moved or renamed from the filesystem' | |
173 ' please run the application again' | |
174 ' in order to rescan repositories') % repo_name, | |
175 category='error') | |
176 | |
177 return redirect(url('repos')) | |
178 try: | |
179 action_logger(self.hg_app_user, 'admin_deleted_repo', | |
180 repo_name, '', self.sa) | |
181 repo_model.delete(repo) | |
182 invalidate_cache('cached_repo_list') | |
183 h.flash(_('deleted repository %s') % repo_name, category='success') | |
184 | |
185 except Exception, e: | |
186 log.error(traceback.format_exc()) | |
187 h.flash(_('An error occured during deletion of %s') % repo_name, | |
188 category='error') | |
189 | |
190 return redirect(url('repos')) | |
191 | |
192 @HasPermissionAllDecorator('hg.admin') | |
193 def delete_perm_user(self, repo_name): | |
194 """ | |
195 DELETE an existing repository permission user | |
196 @param repo_name: | |
197 """ | |
198 | |
199 try: | |
200 repo_model = RepoModel() | |
201 repo_model.delete_perm_user(request.POST, repo_name) | |
202 except Exception as e: | |
203 h.flash(_('An error occured during deletion of repository user'), | |
204 category='error') | |
205 raise HTTPInternalServerError() | |
206 | |
207 @HasPermissionAllDecorator('hg.admin') | |
208 def show(self, repo_name, format='html'): | |
209 """GET /repos/repo_name: Show a specific item""" | |
210 # url('repo', repo_name=ID) | |
211 | |
212 @HasPermissionAllDecorator('hg.admin') | |
213 def edit(self, repo_name, format='html'): | |
214 """GET /repos/repo_name/edit: Form to edit an existing item""" | |
215 # url('edit_repo', repo_name=ID) | |
216 repo_model = RepoModel() | |
217 c.repo_info = repo = repo_model.get(repo_name) | |
218 if not repo: | |
219 h.flash(_('%s repository is not mapped to db perhaps' | |
220 ' it was created or renamed from the filesystem' | |
221 ' please run the application again' | |
222 ' in order to rescan repositories') % repo_name, | |
223 category='error') | |
224 | |
225 return redirect(url('repos')) | |
226 defaults = c.repo_info.__dict__ | |
227 if c.repo_info.user: | |
228 defaults.update({'user':c.repo_info.user.username}) | |
229 else: | |
230 replacement_user = self.sa.query(User)\ | |
231 .filter(User.admin == True).first().username | |
232 defaults.update({'user':replacement_user}) | |
233 | |
234 c.users_array = repo_model.get_users_js() | |
235 | |
236 for p in c.repo_info.repo_to_perm: | |
237 defaults.update({'perm_%s' % p.user.username: | |
238 p.permission.permission_name}) | |
239 | |
240 return htmlfill.render( | |
241 render('admin/repos/repo_edit.html'), | |
242 defaults=defaults, | |
243 encoding="UTF-8", | |
244 force_defaults=False | |
245 ) |