##// END OF EJS Templates
fixed error for empty revisions on admin repo edit
marcink -
r711:cda88b3e beta
parent child Browse files
Show More
@@ -1,288 +1,290 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 # repos 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 7, 2010
22 22 admin controller for pylons
23 23 @author: marcink
24 24 """
25 25 from formencode import htmlfill
26 26 from operator import itemgetter
27 27 from paste.httpexceptions import HTTPInternalServerError
28 28 from pylons import request, response, session, tmpl_context as c, url
29 29 from pylons.controllers.util import abort, redirect
30 30 from pylons.i18n.translation import _
31 31 from rhodecode.lib import helpers as h
32 32 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \
33 33 HasPermissionAnyDecorator
34 34 from rhodecode.lib.base import BaseController, render
35 35 from rhodecode.lib.utils import invalidate_cache, action_logger
36 36 from rhodecode.model.db import User
37 37 from rhodecode.model.forms import RepoForm
38 38 from rhodecode.model.scm import ScmModel
39 39 from rhodecode.model.repo import RepoModel
40 40 import formencode
41 41 import logging
42 42 import traceback
43 43
44 44 log = logging.getLogger(__name__)
45 45
46 46 class ReposController(BaseController):
47 47 """REST Controller styled on the Atom Publishing Protocol"""
48 48 # To properly map this controller, ensure your config/routing.py
49 49 # file has a resource setup:
50 50 # map.resource('repo', 'repos')
51 51
52 52 @LoginRequired()
53 53 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
54 54 def __before__(self):
55 55 c.admin_user = session.get('admin_user')
56 56 c.admin_username = session.get('admin_username')
57 57 super(ReposController, self).__before__()
58 58
59 59 @HasPermissionAllDecorator('hg.admin')
60 60 def index(self, format='html'):
61 61 """GET /repos: All items in the collection"""
62 62 # url('repos')
63 63 cached_repo_list = ScmModel().get_repos()
64 64 c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort'))
65 65 return render('admin/repos/repos.html')
66 66
67 67 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
68 68 def create(self):
69 69 """POST /repos: Create a new item"""
70 70 # url('repos')
71 71 repo_model = RepoModel()
72 72 _form = RepoForm()()
73 73 form_result = {}
74 74 try:
75 75 form_result = _form.to_python(dict(request.POST))
76 76 repo_model.create(form_result, c.rhodecode_user)
77 77 h.flash(_('created repository %s') % form_result['repo_name'],
78 78 category='success')
79 79
80 80 if request.POST.get('user_created'):
81 81 action_logger(self.rhodecode_user, 'user_created_repo',
82 82 form_result['repo_name'], '', self.sa)
83 83 else:
84 84 action_logger(self.rhodecode_user, 'admin_created_repo',
85 85 form_result['repo_name'], '', self.sa)
86 86
87 87 except formencode.Invalid, errors:
88 88 c.new_repo = errors.value['repo_name']
89 89
90 90 if request.POST.get('user_created'):
91 91 r = render('admin/repos/repo_add_create_repository.html')
92 92 else:
93 93 r = render('admin/repos/repo_add.html')
94 94
95 95 return htmlfill.render(
96 96 r,
97 97 defaults=errors.value,
98 98 errors=errors.error_dict or {},
99 99 prefix_error=False,
100 100 encoding="UTF-8")
101 101
102 102 except Exception:
103 103 log.error(traceback.format_exc())
104 104 msg = _('error occured during creation of repository %s') \
105 105 % form_result.get('repo_name')
106 106 h.flash(msg, category='error')
107 107 if request.POST.get('user_created'):
108 108 return redirect(url('home'))
109 109 return redirect(url('repos'))
110 110
111 111 @HasPermissionAllDecorator('hg.admin')
112 112 def new(self, format='html'):
113 113 """GET /repos/new: Form to create a new item"""
114 114 new_repo = request.GET.get('repo', '')
115 115 c.new_repo = h.repo_name_slug(new_repo)
116 116
117 117 return render('admin/repos/repo_add.html')
118 118
119 119 @HasPermissionAllDecorator('hg.admin')
120 120 def update(self, repo_name):
121 121 """PUT /repos/repo_name: Update an existing item"""
122 122 # Forms posted to this method should contain a hidden field:
123 123 # <input type="hidden" name="_method" value="PUT" />
124 124 # Or using helpers:
125 125 # h.form(url('repo', repo_name=ID),
126 126 # method='put')
127 127 # url('repo', repo_name=ID)
128 128 repo_model = RepoModel()
129 129 changed_name = repo_name
130 130 _form = RepoForm(edit=True, old_data={'repo_name':repo_name})()
131 131
132 132 try:
133 133 form_result = _form.to_python(dict(request.POST))
134 134 repo_model.update(repo_name, form_result)
135 135 invalidate_cache('get_repo_cached_%s' % repo_name)
136 136 h.flash(_('Repository %s updated successfully' % repo_name),
137 137 category='success')
138 138 changed_name = form_result['repo_name']
139 139 action_logger(self.rhodecode_user, 'admin_updated_repo',
140 140 changed_name, '', self.sa)
141 141
142 142 except formencode.Invalid, errors:
143 143 c.repo_info = repo_model.get(repo_name)
144 144 c.users_array = repo_model.get_users_js()
145 145 errors.value.update({'user':c.repo_info.user.username})
146 146 return htmlfill.render(
147 147 render('admin/repos/repo_edit.html'),
148 148 defaults=errors.value,
149 149 errors=errors.error_dict or {},
150 150 prefix_error=False,
151 151 encoding="UTF-8")
152 152
153 153 except Exception:
154 154 log.error(traceback.format_exc())
155 155 h.flash(_('error occurred during update of repository %s') \
156 156 % repo_name, category='error')
157 157
158 158 return redirect(url('edit_repo', repo_name=changed_name))
159 159
160 160 @HasPermissionAllDecorator('hg.admin')
161 161 def delete(self, repo_name):
162 162 """DELETE /repos/repo_name: Delete an existing item"""
163 163 # Forms posted to this method should contain a hidden field:
164 164 # <input type="hidden" name="_method" value="DELETE" />
165 165 # Or using helpers:
166 166 # h.form(url('repo', repo_name=ID),
167 167 # method='delete')
168 168 # url('repo', repo_name=ID)
169 169
170 170 repo_model = RepoModel()
171 171 repo = repo_model.get(repo_name)
172 172 if not repo:
173 173 h.flash(_('%s repository is not mapped to db perhaps'
174 174 ' it was moved or renamed from the filesystem'
175 175 ' please run the application again'
176 176 ' in order to rescan repositories') % repo_name,
177 177 category='error')
178 178
179 179 return redirect(url('repos'))
180 180 try:
181 181 action_logger(self.rhodecode_user, 'admin_deleted_repo',
182 182 repo_name, '', self.sa)
183 183 repo_model.delete(repo)
184 184 invalidate_cache('get_repo_cached_%s' % repo_name)
185 185 h.flash(_('deleted repository %s') % repo_name, category='success')
186 186
187 187 except Exception, e:
188 188 log.error(traceback.format_exc())
189 189 h.flash(_('An error occured during deletion of %s') % repo_name,
190 190 category='error')
191 191
192 192 return redirect(url('repos'))
193 193
194 194 @HasPermissionAllDecorator('hg.admin')
195 195 def delete_perm_user(self, repo_name):
196 196 """
197 197 DELETE an existing repository permission user
198 198 :param repo_name:
199 199 """
200 200
201 201 try:
202 202 repo_model = RepoModel()
203 203 repo_model.delete_perm_user(request.POST, repo_name)
204 204 except Exception, e:
205 205 h.flash(_('An error occured during deletion of repository user'),
206 206 category='error')
207 207 raise HTTPInternalServerError()
208 208
209 209 @HasPermissionAllDecorator('hg.admin')
210 210 def repo_stats(self, repo_name):
211 211 """
212 212 DELETE an existing repository statistics
213 213 :param repo_name:
214 214 """
215 215
216 216 try:
217 217 repo_model = RepoModel()
218 218 repo_model.delete_stats(repo_name)
219 219 except Exception, e:
220 220 h.flash(_('An error occured during deletion of repository stats'),
221 221 category='error')
222 222 return redirect(url('edit_repo', repo_name=repo_name))
223 223
224 224 @HasPermissionAllDecorator('hg.admin')
225 225 def repo_cache(self, repo_name):
226 226 """
227 227 INVALIDATE exisitings repository cache
228 228 :param repo_name:
229 229 """
230 230
231 231 try:
232 232 ScmModel().mark_for_invalidation(repo_name)
233 233 except Exception, e:
234 h.flash(_('An error occured during cache invalidation'),
234 h.flash(_('An error occurred during cache invalidation'),
235 235 category='error')
236 236 return redirect(url('edit_repo', repo_name=repo_name))
237 237
238 238 @HasPermissionAllDecorator('hg.admin')
239 239 def show(self, repo_name, format='html'):
240 240 """GET /repos/repo_name: Show a specific item"""
241 241 # url('repo', repo_name=ID)
242 242
243 243 @HasPermissionAllDecorator('hg.admin')
244 244 def edit(self, repo_name, format='html'):
245 245 """GET /repos/repo_name/edit: Form to edit an existing item"""
246 246 # url('edit_repo', repo_name=ID)
247 247 repo_model = RepoModel()
248 248 c.repo_info = repo = repo_model.get(repo_name)
249 249 if repo.stats:
250 250 last_rev = repo.stats.stat_on_revision
251 251 else:
252 252 last_rev = 0
253 253 c.stats_revision = last_rev
254 c.repo_last_rev = ScmModel().get(repo_name).revisions[-1]
254 r = ScmModel().get(repo_name)
255 c.repo_last_rev = r.revisions[-1] if r.revisions else 0
256
255 257 if last_rev == 0:
256 258 c.stats_percentage = 0
257 259 else:
258 260 c.stats_percentage = '%.2f' % ((float((last_rev)) / c.repo_last_rev) * 100)
259 261
260 262
261 263 if not repo:
262 264 h.flash(_('%s repository is not mapped to db perhaps'
263 265 ' it was created or renamed from the filesystem'
264 266 ' please run the application again'
265 267 ' in order to rescan repositories') % repo_name,
266 268 category='error')
267 269
268 270 return redirect(url('repos'))
269 271 defaults = c.repo_info.__dict__
270 272 if c.repo_info.user:
271 273 defaults.update({'user':c.repo_info.user.username})
272 274 else:
273 275 replacement_user = self.sa.query(User)\
274 276 .filter(User.admin == True).first().username
275 277 defaults.update({'user':replacement_user})
276 278
277 279 c.users_array = repo_model.get_users_js()
278 280
279 281 for p in c.repo_info.repo_to_perm:
280 282 defaults.update({'perm_%s' % p.user.username:
281 283 p.permission.permission_name})
282 284
283 285 return htmlfill.render(
284 286 render('admin/repos/repo_edit.html'),
285 287 defaults=defaults,
286 288 encoding="UTF-8",
287 289 force_defaults=False
288 290 )
General Comments 0
You need to be logged in to leave comments. Login now