Show More
@@ -1,422 +1,423 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.admin.repos |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Admin controller for RhodeCode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 7, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software; you can redistribute it and/or |
|
14 | 14 | # modify it under the terms of the GNU General Public License |
|
15 | 15 | # as published by the Free Software Foundation; version 2 |
|
16 | 16 | # of the License or (at your opinion) any later version of the license. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program; if not, write to the Free Software |
|
25 | 25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | 26 | # MA 02110-1301, USA. |
|
27 | 27 | |
|
28 | 28 | import logging |
|
29 | 29 | import traceback |
|
30 | 30 | import formencode |
|
31 | 31 | from operator import itemgetter |
|
32 | 32 | from formencode import htmlfill |
|
33 | 33 | |
|
34 | 34 | from paste.httpexceptions import HTTPInternalServerError |
|
35 | 35 | from pylons import request, response, session, tmpl_context as c, url |
|
36 | 36 | from pylons.controllers.util import abort, redirect |
|
37 | 37 | from pylons.i18n.translation import _ |
|
38 | 38 | |
|
39 | 39 | from rhodecode.lib import helpers as h |
|
40 | 40 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ |
|
41 | 41 | HasPermissionAnyDecorator |
|
42 | 42 | from rhodecode.lib.base import BaseController, render |
|
43 | 43 | from rhodecode.lib.utils import invalidate_cache, action_logger, repo_name_slug |
|
44 | 44 | from rhodecode.lib.helpers import get_token |
|
45 | 45 | from rhodecode.model.db import User, Repository, UserFollowing, Group |
|
46 | 46 | from rhodecode.model.forms import RepoForm |
|
47 | 47 | from rhodecode.model.scm import ScmModel |
|
48 | 48 | from rhodecode.model.repo import RepoModel |
|
49 | 49 | |
|
50 | 50 | log = logging.getLogger(__name__) |
|
51 | 51 | |
|
52 | 52 | class ReposController(BaseController): |
|
53 | 53 | """ |
|
54 | 54 | REST Controller styled on the Atom Publishing Protocol""" |
|
55 | 55 | # To properly map this controller, ensure your config/routing.py |
|
56 | 56 | # file has a resource setup: |
|
57 | 57 | # map.resource('repo', 'repos') |
|
58 | 58 | |
|
59 | 59 | @LoginRequired() |
|
60 | 60 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
61 | 61 | def __before__(self): |
|
62 | 62 | c.admin_user = session.get('admin_user') |
|
63 | 63 | c.admin_username = session.get('admin_username') |
|
64 | 64 | super(ReposController, self).__before__() |
|
65 | 65 | |
|
66 | 66 | def __load_defaults(self): |
|
67 | 67 | repo_model = RepoModel() |
|
68 | 68 | |
|
69 | 69 | c.repo_groups = [('', '')] |
|
70 | 70 | parents_link = lambda k:h.literal('»'.join( |
|
71 | 71 | map(lambda k:k.group_name, |
|
72 | 72 | k.parents + [k]) |
|
73 | 73 | ) |
|
74 | 74 | ) |
|
75 | 75 | |
|
76 | 76 | c.repo_groups.extend([(x.group_id, parents_link(x)) for \ |
|
77 | 77 | x in self.sa.query(Group).all()]) |
|
78 | 78 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) |
|
79 | 79 | c.users_array = repo_model.get_users_js() |
|
80 | 80 | c.users_groups_array = repo_model.get_users_groups_js() |
|
81 | 81 | |
|
82 | 82 | def __load_data(self, repo_name=None): |
|
83 | 83 | """ |
|
84 | 84 | Load defaults settings for edit, and update |
|
85 | 85 | |
|
86 | 86 | :param repo_name: |
|
87 | 87 | """ |
|
88 | 88 | self.__load_defaults() |
|
89 | 89 | |
|
90 | 90 | repo, dbrepo = ScmModel().get(repo_name, retval='repo') |
|
91 | 91 | |
|
92 | 92 | repo_model = RepoModel() |
|
93 | 93 | c.repo_info = repo_model.get_by_repo_name(repo_name) |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | if c.repo_info is None: |
|
97 | 97 | h.flash(_('%s repository is not mapped to db perhaps' |
|
98 | 98 | ' it was created or renamed from the filesystem' |
|
99 | 99 | ' please run the application again' |
|
100 | 100 | ' in order to rescan repositories') % repo_name, |
|
101 | 101 | category='error') |
|
102 | 102 | |
|
103 | 103 | return redirect(url('repos')) |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | c.default_user_id = User.by_username('default').user_id |
|
107 | 107 | c.in_public_journal = self.sa.query(UserFollowing)\ |
|
108 | 108 | .filter(UserFollowing.user_id == c.default_user_id)\ |
|
109 | 109 | .filter(UserFollowing.follows_repository == c.repo_info).scalar() |
|
110 | 110 | |
|
111 | 111 | if c.repo_info.stats: |
|
112 | 112 | last_rev = c.repo_info.stats.stat_on_revision |
|
113 | 113 | else: |
|
114 | 114 | last_rev = 0 |
|
115 | 115 | c.stats_revision = last_rev |
|
116 | 116 | |
|
117 | 117 | c.repo_last_rev = repo.count() - 1 if repo.revisions else 0 |
|
118 | 118 | |
|
119 | 119 | if last_rev == 0 or c.repo_last_rev == 0: |
|
120 | 120 | c.stats_percentage = 0 |
|
121 | 121 | else: |
|
122 | 122 | c.stats_percentage = '%.2f' % ((float((last_rev)) / |
|
123 | 123 | c.repo_last_rev) * 100) |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | defaults = c.repo_info.get_dict() |
|
128 | 128 | group, repo_name = c.repo_info.groups_and_repo |
|
129 | 129 | defaults['repo_name'] = repo_name |
|
130 |
defaults['repo_group'] = getattr(group[-1] if group else None, |
|
|
130 | defaults['repo_group'] = getattr(group[-1] if group else None, | |
|
131 | 'group_id', None) | |
|
131 | 132 | |
|
132 | 133 | #fill owner |
|
133 | 134 | if c.repo_info.user: |
|
134 | 135 | defaults.update({'user':c.repo_info.user.username}) |
|
135 | 136 | else: |
|
136 | 137 | replacement_user = self.sa.query(User)\ |
|
137 | 138 | .filter(User.admin == True).first().username |
|
138 | 139 | defaults.update({'user':replacement_user}) |
|
139 | 140 | |
|
140 | 141 | |
|
141 | 142 | #fill repository users |
|
142 | 143 | for p in c.repo_info.repo_to_perm: |
|
143 | 144 | defaults.update({'u_perm_%s' % p.user.username: |
|
144 | 145 | p.permission.permission_name}) |
|
145 | 146 | |
|
146 | 147 | #fill repository groups |
|
147 | 148 | for p in c.repo_info.users_group_to_perm: |
|
148 | 149 | defaults.update({'g_perm_%s' % p.users_group.users_group_name: |
|
149 | 150 | p.permission.permission_name}) |
|
150 | 151 | |
|
151 | 152 | |
|
152 | 153 | return defaults |
|
153 | 154 | |
|
154 | 155 | |
|
155 | 156 | @HasPermissionAllDecorator('hg.admin') |
|
156 | 157 | def index(self, format='html'): |
|
157 | 158 | """GET /repos: All items in the collection""" |
|
158 | 159 | # url('repos') |
|
159 | 160 | cached_repo_list = ScmModel().get_repos() |
|
160 | 161 | c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort')) |
|
161 | 162 | return render('admin/repos/repos.html') |
|
162 | 163 | |
|
163 | 164 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
164 | 165 | def create(self): |
|
165 | 166 | """ |
|
166 | 167 | POST /repos: Create a new item""" |
|
167 | 168 | # url('repos') |
|
168 | 169 | repo_model = RepoModel() |
|
169 | 170 | self.__load_defaults() |
|
170 | 171 | form_result = {} |
|
171 | 172 | try: |
|
172 | 173 | form_result = RepoForm(repo_groups=c.repo_groups_choices)()\ |
|
173 | 174 | .to_python(dict(request.POST)) |
|
174 | 175 | repo_model.create(form_result, self.rhodecode_user) |
|
175 | 176 | if form_result['clone_uri']: |
|
176 | 177 | h.flash(_('created repository %s from %s') \ |
|
177 | 178 | % (form_result['repo_name'], form_result['clone_uri']), |
|
178 | 179 | category='success') |
|
179 | 180 | else: |
|
180 | 181 | h.flash(_('created repository %s') % form_result['repo_name'], |
|
181 | 182 | category='success') |
|
182 | 183 | |
|
183 | 184 | if request.POST.get('user_created'): |
|
184 | 185 | action_logger(self.rhodecode_user, 'user_created_repo', |
|
185 | 186 | form_result['repo_name'], '', self.sa) |
|
186 | 187 | else: |
|
187 | 188 | action_logger(self.rhodecode_user, 'admin_created_repo', |
|
188 | 189 | form_result['repo_name'], '', self.sa) |
|
189 | 190 | |
|
190 | 191 | except formencode.Invalid, errors: |
|
191 | 192 | |
|
192 | 193 | c.new_repo = errors.value['repo_name'] |
|
193 | 194 | |
|
194 | 195 | if request.POST.get('user_created'): |
|
195 | 196 | r = render('admin/repos/repo_add_create_repository.html') |
|
196 | 197 | else: |
|
197 | 198 | r = render('admin/repos/repo_add.html') |
|
198 | 199 | |
|
199 | 200 | return htmlfill.render( |
|
200 | 201 | r, |
|
201 | 202 | defaults=errors.value, |
|
202 | 203 | errors=errors.error_dict or {}, |
|
203 | 204 | prefix_error=False, |
|
204 | 205 | encoding="UTF-8") |
|
205 | 206 | |
|
206 | 207 | except Exception: |
|
207 | 208 | log.error(traceback.format_exc()) |
|
208 | 209 | msg = _('error occurred during creation of repository %s') \ |
|
209 | 210 | % form_result.get('repo_name') |
|
210 | 211 | h.flash(msg, category='error') |
|
211 | 212 | if request.POST.get('user_created'): |
|
212 | 213 | return redirect(url('home')) |
|
213 | 214 | return redirect(url('repos')) |
|
214 | 215 | |
|
215 | 216 | @HasPermissionAllDecorator('hg.admin') |
|
216 | 217 | def new(self, format='html'): |
|
217 | 218 | """GET /repos/new: Form to create a new item""" |
|
218 | 219 | new_repo = request.GET.get('repo', '') |
|
219 | 220 | c.new_repo = repo_name_slug(new_repo) |
|
220 | 221 | self.__load_defaults() |
|
221 | 222 | return render('admin/repos/repo_add.html') |
|
222 | 223 | |
|
223 | 224 | @HasPermissionAllDecorator('hg.admin') |
|
224 | 225 | def update(self, repo_name): |
|
225 | 226 | """ |
|
226 | 227 | PUT /repos/repo_name: Update an existing item""" |
|
227 | 228 | # Forms posted to this method should contain a hidden field: |
|
228 | 229 | # <input type="hidden" name="_method" value="PUT" /> |
|
229 | 230 | # Or using helpers: |
|
230 | 231 | # h.form(url('repo', repo_name=ID), |
|
231 | 232 | # method='put') |
|
232 | 233 | # url('repo', repo_name=ID) |
|
233 | 234 | self.__load_defaults() |
|
234 | 235 | repo_model = RepoModel() |
|
235 | 236 | changed_name = repo_name |
|
236 | 237 | _form = RepoForm(edit=True, old_data={'repo_name':repo_name}, |
|
237 | 238 | repo_groups=c.repo_groups_choices)() |
|
238 | 239 | try: |
|
239 | 240 | form_result = _form.to_python(dict(request.POST)) |
|
240 | 241 | repo_model.update(repo_name, form_result) |
|
241 | 242 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
242 | 243 | h.flash(_('Repository %s updated successfully' % repo_name), |
|
243 | 244 | category='success') |
|
244 | 245 | changed_name = form_result['repo_name'] |
|
245 | 246 | action_logger(self.rhodecode_user, 'admin_updated_repo', |
|
246 | 247 | changed_name, '', self.sa) |
|
247 | 248 | |
|
248 | 249 | except formencode.Invalid, errors: |
|
249 | 250 | defaults = self.__load_data(repo_name) |
|
250 | 251 | defaults.update(errors.value) |
|
251 | 252 | return htmlfill.render( |
|
252 | 253 | render('admin/repos/repo_edit.html'), |
|
253 | 254 | defaults=defaults, |
|
254 | 255 | errors=errors.error_dict or {}, |
|
255 | 256 | prefix_error=False, |
|
256 | 257 | encoding="UTF-8") |
|
257 | 258 | |
|
258 | 259 | except Exception: |
|
259 | 260 | log.error(traceback.format_exc()) |
|
260 | 261 | h.flash(_('error occurred during update of repository %s') \ |
|
261 | 262 | % repo_name, category='error') |
|
262 | 263 | return redirect(url('edit_repo', repo_name=changed_name)) |
|
263 | 264 | |
|
264 | 265 | @HasPermissionAllDecorator('hg.admin') |
|
265 | 266 | def delete(self, repo_name): |
|
266 | 267 | """ |
|
267 | 268 | DELETE /repos/repo_name: Delete an existing item""" |
|
268 | 269 | # Forms posted to this method should contain a hidden field: |
|
269 | 270 | # <input type="hidden" name="_method" value="DELETE" /> |
|
270 | 271 | # Or using helpers: |
|
271 | 272 | # h.form(url('repo', repo_name=ID), |
|
272 | 273 | # method='delete') |
|
273 | 274 | # url('repo', repo_name=ID) |
|
274 | 275 | |
|
275 | 276 | repo_model = RepoModel() |
|
276 | 277 | repo = repo_model.get_by_repo_name(repo_name) |
|
277 | 278 | if not repo: |
|
278 | 279 | h.flash(_('%s repository is not mapped to db perhaps' |
|
279 | 280 | ' it was moved or renamed from the filesystem' |
|
280 | 281 | ' please run the application again' |
|
281 | 282 | ' in order to rescan repositories') % repo_name, |
|
282 | 283 | category='error') |
|
283 | 284 | |
|
284 | 285 | return redirect(url('repos')) |
|
285 | 286 | try: |
|
286 | 287 | action_logger(self.rhodecode_user, 'admin_deleted_repo', |
|
287 | 288 | repo_name, '', self.sa) |
|
288 | 289 | repo_model.delete(repo) |
|
289 | 290 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
290 | 291 | h.flash(_('deleted repository %s') % repo_name, category='success') |
|
291 | 292 | |
|
292 | 293 | except Exception, e: |
|
293 | 294 | log.error(traceback.format_exc()) |
|
294 | 295 | h.flash(_('An error occurred during deletion of %s') % repo_name, |
|
295 | 296 | category='error') |
|
296 | 297 | |
|
297 | 298 | return redirect(url('repos')) |
|
298 | 299 | |
|
299 | 300 | @HasPermissionAllDecorator('hg.admin') |
|
300 | 301 | def delete_perm_user(self, repo_name): |
|
301 | 302 | """ |
|
302 | 303 | DELETE an existing repository permission user |
|
303 | 304 | |
|
304 | 305 | :param repo_name: |
|
305 | 306 | """ |
|
306 | 307 | |
|
307 | 308 | try: |
|
308 | 309 | repo_model = RepoModel() |
|
309 | 310 | repo_model.delete_perm_user(request.POST, repo_name) |
|
310 | 311 | except Exception, e: |
|
311 | 312 | h.flash(_('An error occurred during deletion of repository user'), |
|
312 | 313 | category='error') |
|
313 | 314 | raise HTTPInternalServerError() |
|
314 | 315 | |
|
315 | 316 | @HasPermissionAllDecorator('hg.admin') |
|
316 | 317 | def delete_perm_users_group(self, repo_name): |
|
317 | 318 | """ |
|
318 | 319 | DELETE an existing repository permission users group |
|
319 | 320 | |
|
320 | 321 | :param repo_name: |
|
321 | 322 | """ |
|
322 | 323 | try: |
|
323 | 324 | repo_model = RepoModel() |
|
324 | 325 | repo_model.delete_perm_users_group(request.POST, repo_name) |
|
325 | 326 | except Exception, e: |
|
326 | 327 | h.flash(_('An error occurred during deletion of repository' |
|
327 | 328 | ' users groups'), |
|
328 | 329 | category='error') |
|
329 | 330 | raise HTTPInternalServerError() |
|
330 | 331 | |
|
331 | 332 | @HasPermissionAllDecorator('hg.admin') |
|
332 | 333 | def repo_stats(self, repo_name): |
|
333 | 334 | """ |
|
334 | 335 | DELETE an existing repository statistics |
|
335 | 336 | |
|
336 | 337 | :param repo_name: |
|
337 | 338 | """ |
|
338 | 339 | |
|
339 | 340 | try: |
|
340 | 341 | repo_model = RepoModel() |
|
341 | 342 | repo_model.delete_stats(repo_name) |
|
342 | 343 | except Exception, e: |
|
343 | 344 | h.flash(_('An error occurred during deletion of repository stats'), |
|
344 | 345 | category='error') |
|
345 | 346 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
346 | 347 | |
|
347 | 348 | @HasPermissionAllDecorator('hg.admin') |
|
348 | 349 | def repo_cache(self, repo_name): |
|
349 | 350 | """ |
|
350 | 351 | INVALIDATE existing repository cache |
|
351 | 352 | |
|
352 | 353 | :param repo_name: |
|
353 | 354 | """ |
|
354 | 355 | |
|
355 | 356 | try: |
|
356 | 357 | ScmModel().mark_for_invalidation(repo_name) |
|
357 | 358 | except Exception, e: |
|
358 | 359 | h.flash(_('An error occurred during cache invalidation'), |
|
359 | 360 | category='error') |
|
360 | 361 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
361 | 362 | |
|
362 | 363 | @HasPermissionAllDecorator('hg.admin') |
|
363 | 364 | def repo_public_journal(self, repo_name): |
|
364 | 365 | """ |
|
365 | 366 | Set's this repository to be visible in public journal, |
|
366 | 367 | in other words assing default user to follow this repo |
|
367 | 368 | |
|
368 | 369 | :param repo_name: |
|
369 | 370 | """ |
|
370 | 371 | |
|
371 | 372 | cur_token = request.POST.get('auth_token') |
|
372 | 373 | token = get_token() |
|
373 | 374 | if cur_token == token: |
|
374 | 375 | try: |
|
375 | 376 | repo_id = Repository.by_repo_name(repo_name).repo_id |
|
376 | 377 | user_id = User.by_username('default').user_id |
|
377 | 378 | self.scm_model.toggle_following_repo(repo_id, user_id) |
|
378 | 379 | h.flash(_('Updated repository visibility in public journal'), |
|
379 | 380 | category='success') |
|
380 | 381 | except: |
|
381 | 382 | h.flash(_('An error occurred during setting this' |
|
382 | 383 | ' repository in public journal'), |
|
383 | 384 | category='error') |
|
384 | 385 | |
|
385 | 386 | else: |
|
386 | 387 | h.flash(_('Token mismatch'), category='error') |
|
387 | 388 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
388 | 389 | |
|
389 | 390 | @HasPermissionAllDecorator('hg.admin') |
|
390 | 391 | def repo_pull(self, repo_name): |
|
391 | 392 | """ |
|
392 | 393 | Runs task to update given repository with remote changes, |
|
393 | 394 | ie. make pull on remote location |
|
394 | 395 | |
|
395 | 396 | :param repo_name: |
|
396 | 397 | """ |
|
397 | 398 | try: |
|
398 | 399 | ScmModel().pull_changes(repo_name, self.rhodecode_user.username) |
|
399 | 400 | h.flash(_('Pulled from remote location'), category='success') |
|
400 | 401 | except Exception, e: |
|
401 | 402 | h.flash(_('An error occurred during pull from remote location'), |
|
402 | 403 | category='error') |
|
403 | 404 | |
|
404 | 405 | return redirect(url('edit_repo', repo_name=repo_name)) |
|
405 | 406 | |
|
406 | 407 | @HasPermissionAllDecorator('hg.admin') |
|
407 | 408 | def show(self, repo_name, format='html'): |
|
408 | 409 | """GET /repos/repo_name: Show a specific item""" |
|
409 | 410 | # url('repo', repo_name=ID) |
|
410 | 411 | |
|
411 | 412 | @HasPermissionAllDecorator('hg.admin') |
|
412 | 413 | def edit(self, repo_name, format='html'): |
|
413 | 414 | """GET /repos/repo_name/edit: Form to edit an existing item""" |
|
414 | 415 | # url('edit_repo', repo_name=ID) |
|
415 | 416 | defaults = self.__load_data(repo_name) |
|
416 | 417 | |
|
417 | 418 | return htmlfill.render( |
|
418 | 419 | render('admin/repos/repo_edit.html'), |
|
419 | 420 | defaults=defaults, |
|
420 | 421 | encoding="UTF-8", |
|
421 | 422 | force_defaults=False |
|
422 | 423 | ) |
@@ -1,348 +1,359 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.admin.settings |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | settings controller for rhodecode admin |
|
7 | 7 | |
|
8 | 8 | :created_on: Jul 14, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software; you can redistribute it and/or |
|
14 | 14 | # modify it under the terms of the GNU General Public License |
|
15 | 15 | # as published by the Free Software Foundation; version 2 |
|
16 | 16 | # of the License or (at your opinion) any later version of the license. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program; if not, write to the Free Software |
|
25 | 25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | 26 | # MA 02110-1301, USA. |
|
27 | 27 | |
|
28 | 28 | import logging |
|
29 | 29 | import traceback |
|
30 | 30 | import formencode |
|
31 | 31 | |
|
32 | 32 | from sqlalchemy import func |
|
33 | 33 | from formencode import htmlfill |
|
34 | 34 | from pylons import request, session, tmpl_context as c, url, config |
|
35 | 35 | from pylons.controllers.util import abort, redirect |
|
36 | 36 | from pylons.i18n.translation import _ |
|
37 | 37 | |
|
38 | 38 | from rhodecode.lib import helpers as h |
|
39 | 39 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ |
|
40 | 40 | HasPermissionAnyDecorator, NotAnonymous |
|
41 | 41 | from rhodecode.lib.base import BaseController, render |
|
42 | 42 | from rhodecode.lib.celerylib import tasks, run_task |
|
43 | 43 | from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \ |
|
44 | 44 | set_rhodecode_config, repo_name_slug |
|
45 | 45 | from rhodecode.model.db import RhodeCodeUi, Repository, Group |
|
46 | 46 | from rhodecode.model.forms import UserForm, ApplicationSettingsForm, \ |
|
47 | 47 | ApplicationUiSettingsForm |
|
48 | 48 | from rhodecode.model.scm import ScmModel |
|
49 | 49 | from rhodecode.model.settings import SettingsModel |
|
50 | 50 | from rhodecode.model.user import UserModel |
|
51 | 51 | |
|
52 | 52 | log = logging.getLogger(__name__) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | class SettingsController(BaseController): |
|
56 | 56 | """REST Controller styled on the Atom Publishing Protocol""" |
|
57 | 57 | # To properly map this controller, ensure your config/routing.py |
|
58 | 58 | # file has a resource setup: |
|
59 | 59 | # map.resource('setting', 'settings', controller='admin/settings', |
|
60 | 60 | # path_prefix='/admin', name_prefix='admin_') |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | @LoginRequired() |
|
64 | 64 | def __before__(self): |
|
65 | 65 | c.admin_user = session.get('admin_user') |
|
66 | 66 | c.admin_username = session.get('admin_username') |
|
67 | 67 | super(SettingsController, self).__before__() |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | @HasPermissionAllDecorator('hg.admin') |
|
71 | 71 | def index(self, format='html'): |
|
72 | 72 | """GET /admin/settings: All items in the collection""" |
|
73 | 73 | # url('admin_settings') |
|
74 | 74 | |
|
75 | 75 | defaults = SettingsModel().get_app_settings() |
|
76 | 76 | defaults.update(self.get_hg_ui_settings()) |
|
77 | 77 | return htmlfill.render( |
|
78 | 78 | render('admin/settings/settings.html'), |
|
79 | 79 | defaults=defaults, |
|
80 | 80 | encoding="UTF-8", |
|
81 | 81 | force_defaults=False |
|
82 | 82 | ) |
|
83 | 83 | |
|
84 | 84 | @HasPermissionAllDecorator('hg.admin') |
|
85 | 85 | def create(self): |
|
86 | 86 | """POST /admin/settings: Create a new item""" |
|
87 | 87 | # url('admin_settings') |
|
88 | 88 | |
|
89 | 89 | @HasPermissionAllDecorator('hg.admin') |
|
90 | 90 | def new(self, format='html'): |
|
91 | 91 | """GET /admin/settings/new: Form to create a new item""" |
|
92 | 92 | # url('admin_new_setting') |
|
93 | 93 | |
|
94 | 94 | @HasPermissionAllDecorator('hg.admin') |
|
95 | 95 | def update(self, setting_id): |
|
96 | 96 | """PUT /admin/settings/setting_id: Update an existing item""" |
|
97 | 97 | # Forms posted to this method should contain a hidden field: |
|
98 | 98 | # <input type="hidden" name="_method" value="PUT" /> |
|
99 | 99 | # Or using helpers: |
|
100 | 100 | # h.form(url('admin_setting', setting_id=ID), |
|
101 | 101 | # method='put') |
|
102 | 102 | # url('admin_setting', setting_id=ID) |
|
103 | 103 | if setting_id == 'mapping': |
|
104 | 104 | rm_obsolete = request.POST.get('destroy', False) |
|
105 | 105 | log.debug('Rescanning directories with destroy=%s', rm_obsolete) |
|
106 | 106 | initial = ScmModel().repo_scan() |
|
107 | 107 | log.debug('invalidating all repositories') |
|
108 | 108 | for repo_name in initial.keys(): |
|
109 | 109 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
110 | 110 | |
|
111 | 111 | added, removed = repo2db_mapper(initial, rm_obsolete) |
|
112 | 112 | |
|
113 | 113 | h.flash(_('Repositories successfully' |
|
114 | 114 | ' rescanned added: %s,removed: %s') % (added, removed) |
|
115 | 115 | , category='success') |
|
116 | 116 | |
|
117 | 117 | if setting_id == 'whoosh': |
|
118 | 118 | repo_location = self.get_hg_ui_settings()['paths_root_path'] |
|
119 | 119 | full_index = request.POST.get('full_index', False) |
|
120 | 120 | task = run_task(tasks.whoosh_index, repo_location, full_index) |
|
121 | 121 | |
|
122 | 122 | h.flash(_('Whoosh reindex task scheduled'), category='success') |
|
123 | 123 | if setting_id == 'global': |
|
124 | 124 | |
|
125 | 125 | application_form = ApplicationSettingsForm()() |
|
126 | 126 | try: |
|
127 | 127 | form_result = application_form.to_python(dict(request.POST)) |
|
128 | 128 | settings_model = SettingsModel() |
|
129 | 129 | |
|
130 | 130 | try: |
|
131 | 131 | hgsettings1 = settings_model.get('title') |
|
132 | 132 | hgsettings1.app_settings_value = form_result['rhodecode_title'] |
|
133 | 133 | |
|
134 | 134 | hgsettings2 = settings_model.get('realm') |
|
135 | 135 | hgsettings2.app_settings_value = form_result['rhodecode_realm'] |
|
136 | 136 | |
|
137 | 137 | hgsettings3 = settings_model.get('ga_code') |
|
138 | 138 | hgsettings3.app_settings_value = form_result['rhodecode_ga_code'] |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | self.sa.add(hgsettings1) |
|
143 | 143 | self.sa.add(hgsettings2) |
|
144 | 144 | self.sa.add(hgsettings3) |
|
145 | 145 | self.sa.commit() |
|
146 | 146 | set_rhodecode_config(config) |
|
147 | 147 | h.flash(_('Updated application settings'), |
|
148 | 148 | category='success') |
|
149 | 149 | |
|
150 | 150 | except: |
|
151 | 151 | log.error(traceback.format_exc()) |
|
152 | 152 | h.flash(_('error occurred during updating application settings'), |
|
153 | 153 | category='error') |
|
154 | 154 | |
|
155 | 155 | self.sa.rollback() |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | except formencode.Invalid, errors: |
|
159 | 159 | return htmlfill.render( |
|
160 | 160 | render('admin/settings/settings.html'), |
|
161 | 161 | defaults=errors.value, |
|
162 | 162 | errors=errors.error_dict or {}, |
|
163 | 163 | prefix_error=False, |
|
164 | 164 | encoding="UTF-8") |
|
165 | 165 | |
|
166 | 166 | if setting_id == 'mercurial': |
|
167 | 167 | application_form = ApplicationUiSettingsForm()() |
|
168 | 168 | try: |
|
169 | 169 | form_result = application_form.to_python(dict(request.POST)) |
|
170 | 170 | |
|
171 | 171 | try: |
|
172 | 172 | |
|
173 | 173 | hgsettings1 = self.sa.query(RhodeCodeUi)\ |
|
174 | 174 | .filter(RhodeCodeUi.ui_key == 'push_ssl').one() |
|
175 | 175 | hgsettings1.ui_value = form_result['web_push_ssl'] |
|
176 | 176 | |
|
177 | 177 | hgsettings2 = self.sa.query(RhodeCodeUi)\ |
|
178 | 178 | .filter(RhodeCodeUi.ui_key == '/').one() |
|
179 | 179 | hgsettings2.ui_value = form_result['paths_root_path'] |
|
180 | 180 | |
|
181 | 181 | |
|
182 | 182 | #HOOKS |
|
183 | 183 | hgsettings3 = self.sa.query(RhodeCodeUi)\ |
|
184 | 184 | .filter(RhodeCodeUi.ui_key == 'changegroup.update').one() |
|
185 | 185 | hgsettings3.ui_active = bool(form_result['hooks_changegroup_update']) |
|
186 | 186 | |
|
187 | 187 | hgsettings4 = self.sa.query(RhodeCodeUi)\ |
|
188 | 188 | .filter(RhodeCodeUi.ui_key == 'changegroup.repo_size').one() |
|
189 | 189 | hgsettings4.ui_active = bool(form_result['hooks_changegroup_repo_size']) |
|
190 | 190 | |
|
191 | 191 | hgsettings5 = self.sa.query(RhodeCodeUi)\ |
|
192 | 192 | .filter(RhodeCodeUi.ui_key == 'pretxnchangegroup.push_logger').one() |
|
193 | 193 | hgsettings5.ui_active = bool(form_result['hooks_pretxnchangegroup_push_logger']) |
|
194 | 194 | |
|
195 | 195 | hgsettings6 = self.sa.query(RhodeCodeUi)\ |
|
196 | 196 | .filter(RhodeCodeUi.ui_key == 'preoutgoing.pull_logger').one() |
|
197 | 197 | hgsettings6.ui_active = bool(form_result['hooks_preoutgoing_pull_logger']) |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | self.sa.add(hgsettings1) |
|
201 | 201 | self.sa.add(hgsettings2) |
|
202 | 202 | self.sa.add(hgsettings3) |
|
203 | 203 | self.sa.add(hgsettings4) |
|
204 | 204 | self.sa.add(hgsettings5) |
|
205 | 205 | self.sa.add(hgsettings6) |
|
206 | 206 | self.sa.commit() |
|
207 | 207 | |
|
208 | 208 | h.flash(_('Updated mercurial settings'), |
|
209 | 209 | category='success') |
|
210 | 210 | |
|
211 | 211 | except: |
|
212 | 212 | log.error(traceback.format_exc()) |
|
213 | 213 | h.flash(_('error occurred during updating application settings'), |
|
214 | 214 | category='error') |
|
215 | 215 | |
|
216 | 216 | self.sa.rollback() |
|
217 | 217 | |
|
218 | 218 | |
|
219 | 219 | except formencode.Invalid, errors: |
|
220 | 220 | return htmlfill.render( |
|
221 | 221 | render('admin/settings/settings.html'), |
|
222 | 222 | defaults=errors.value, |
|
223 | 223 | errors=errors.error_dict or {}, |
|
224 | 224 | prefix_error=False, |
|
225 | 225 | encoding="UTF-8") |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | |
|
229 | 229 | return redirect(url('admin_settings')) |
|
230 | 230 | |
|
231 | 231 | @HasPermissionAllDecorator('hg.admin') |
|
232 | 232 | def delete(self, setting_id): |
|
233 | 233 | """DELETE /admin/settings/setting_id: Delete an existing item""" |
|
234 | 234 | # Forms posted to this method should contain a hidden field: |
|
235 | 235 | # <input type="hidden" name="_method" value="DELETE" /> |
|
236 | 236 | # Or using helpers: |
|
237 | 237 | # h.form(url('admin_setting', setting_id=ID), |
|
238 | 238 | # method='delete') |
|
239 | 239 | # url('admin_setting', setting_id=ID) |
|
240 | 240 | |
|
241 | 241 | @HasPermissionAllDecorator('hg.admin') |
|
242 | 242 | def show(self, setting_id, format='html'): |
|
243 | 243 | """GET /admin/settings/setting_id: Show a specific item""" |
|
244 | 244 | # url('admin_setting', setting_id=ID) |
|
245 | 245 | |
|
246 | 246 | @HasPermissionAllDecorator('hg.admin') |
|
247 | 247 | def edit(self, setting_id, format='html'): |
|
248 | 248 | """GET /admin/settings/setting_id/edit: Form to edit an existing item""" |
|
249 | 249 | # url('admin_edit_setting', setting_id=ID) |
|
250 | 250 | |
|
251 | 251 | @NotAnonymous() |
|
252 | 252 | def my_account(self): |
|
253 | 253 | """ |
|
254 | 254 | GET /_admin/my_account Displays info about my account |
|
255 | 255 | """ |
|
256 | 256 | # url('admin_settings_my_account') |
|
257 | 257 | |
|
258 | 258 | c.user = UserModel().get(self.rhodecode_user.user_id, cache=False) |
|
259 | 259 | all_repos = [r.repo_name for r in self.sa.query(Repository)\ |
|
260 | 260 | .filter(Repository.user_id == c.user.user_id)\ |
|
261 | 261 | .order_by(func.lower(Repository.repo_name)).all()] |
|
262 | 262 | c.user_repos = ScmModel().get_repos(all_repos) |
|
263 | 263 | |
|
264 | 264 | if c.user.username == 'default': |
|
265 | 265 | h.flash(_("You can't edit this user since it's" |
|
266 | 266 | " crucial for entire application"), category='warning') |
|
267 | 267 | return redirect(url('users')) |
|
268 | 268 | |
|
269 | 269 | defaults = c.user.get_dict() |
|
270 | 270 | return htmlfill.render( |
|
271 | 271 | render('admin/users/user_edit_my_account.html'), |
|
272 | 272 | defaults=defaults, |
|
273 | 273 | encoding="UTF-8", |
|
274 | 274 | force_defaults=False |
|
275 | 275 | ) |
|
276 | 276 | |
|
277 | 277 | def my_account_update(self): |
|
278 | 278 | """PUT /_admin/my_account_update: Update an existing item""" |
|
279 | 279 | # Forms posted to this method should contain a hidden field: |
|
280 | 280 | # <input type="hidden" name="_method" value="PUT" /> |
|
281 | 281 | # Or using helpers: |
|
282 | 282 | # h.form(url('admin_settings_my_account_update'), |
|
283 | 283 | # method='put') |
|
284 | 284 | # url('admin_settings_my_account_update', id=ID) |
|
285 | 285 | user_model = UserModel() |
|
286 | 286 | uid = self.rhodecode_user.user_id |
|
287 | 287 | _form = UserForm(edit=True, old_data={'user_id':uid, |
|
288 | 288 | 'email':self.rhodecode_user.email})() |
|
289 | 289 | form_result = {} |
|
290 | 290 | try: |
|
291 | 291 | form_result = _form.to_python(dict(request.POST)) |
|
292 | 292 | user_model.update_my_account(uid, form_result) |
|
293 | 293 | h.flash(_('Your account was updated successfully'), |
|
294 | 294 | category='success') |
|
295 | 295 | |
|
296 | 296 | except formencode.Invalid, errors: |
|
297 | 297 | c.user = user_model.get(self.rhodecode_user.user_id, cache=False) |
|
298 | 298 | c.user = UserModel().get(self.rhodecode_user.user_id, cache=False) |
|
299 | 299 | all_repos = self.sa.query(Repository)\ |
|
300 | 300 | .filter(Repository.user_id == c.user.user_id)\ |
|
301 | 301 | .order_by(func.lower(Repository.repo_name))\ |
|
302 | 302 | .all() |
|
303 | 303 | c.user_repos = ScmModel().get_repos(all_repos) |
|
304 | 304 | |
|
305 | 305 | return htmlfill.render( |
|
306 | 306 | render('admin/users/user_edit_my_account.html'), |
|
307 | 307 | defaults=errors.value, |
|
308 | 308 | errors=errors.error_dict or {}, |
|
309 | 309 | prefix_error=False, |
|
310 | 310 | encoding="UTF-8") |
|
311 | 311 | except Exception: |
|
312 | 312 | log.error(traceback.format_exc()) |
|
313 | 313 | h.flash(_('error occurred during update of user %s') \ |
|
314 | 314 | % form_result.get('username'), category='error') |
|
315 | 315 | |
|
316 | 316 | return redirect(url('my_account')) |
|
317 | 317 | |
|
318 | 318 | @NotAnonymous() |
|
319 | 319 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
320 | 320 | def create_repository(self): |
|
321 | 321 | """GET /_admin/create_repository: Form to create a new item""" |
|
322 | ||
|
323 | c.repo_groups = [('', '')] | |
|
324 | parents_link = lambda k:h.literal('»'.join( | |
|
325 | map(lambda k:k.group_name, | |
|
326 | k.parents + [k]) | |
|
327 | ) | |
|
328 | ) | |
|
329 | ||
|
330 | c.repo_groups.extend([(x.group_id, parents_link(x)) for \ | |
|
331 | x in self.sa.query(Group).all()]) | |
|
332 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) | |
|
333 | ||
|
322 | 334 | new_repo = request.GET.get('repo', '') |
|
323 | 335 | c.new_repo = repo_name_slug(new_repo) |
|
324 | c.repo_groups = [('', '')] | |
|
325 | c.repo_groups.extend([(x.group_id, x.group_name) for x in self.sa.query(Group).all()]) | |
|
336 | ||
|
326 | 337 | return render('admin/repos/repo_add_create_repository.html') |
|
327 | 338 | |
|
328 | 339 | def get_hg_ui_settings(self): |
|
329 | 340 | ret = self.sa.query(RhodeCodeUi).all() |
|
330 | 341 | |
|
331 | 342 | if not ret: |
|
332 | 343 | raise Exception('Could not get application ui settings !') |
|
333 | 344 | settings = {} |
|
334 | 345 | for each in ret: |
|
335 | 346 | k = each.ui_key |
|
336 | 347 | v = each.ui_value |
|
337 | 348 | if k == '/': |
|
338 | 349 | k = 'root_path' |
|
339 | 350 | |
|
340 | 351 | if k.find('.') != -1: |
|
341 | 352 | k = k.replace('.', '_') |
|
342 | 353 | |
|
343 | 354 | if each.ui_section == 'hooks': |
|
344 | 355 | v = each.ui_active |
|
345 | 356 | |
|
346 | 357 | settings[each.ui_section + '_' + k] = v |
|
347 | 358 | |
|
348 | 359 | return settings |
General Comments 0
You need to be logged in to leave comments.
Login now