settings.py
209 lines
| 8.2 KiB
| text/x-python
|
PythonLexer
r861 | # -*- coding: utf-8 -*- | |||
""" | ||||
rhodecode.controllers.settings | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
Settings controller for rhodecode | ||||
:created_on: Jun 30, 2010 | ||||
:author: marcink | ||||
r902 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |||
r861 | :license: GPLv3, see COPYING for more details. | |||
""" | ||||
r547 | # This program is free software; you can redistribute it and/or | |||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; version 2 | ||||
# of the License or (at your opinion) any later version of the license. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||||
# MA 02110-1301, USA. | ||||
r861 | ||||
import logging | ||||
import traceback | ||||
r1067 | import formencode | |||
r861 | ||||
r1067 | from formencode import htmlfill | |||
r861 | ||||
r547 | from pylons import tmpl_context as c, request, url | |||
from pylons.controllers.util import redirect | ||||
from pylons.i18n.translation import _ | ||||
r861 | ||||
import rhodecode.lib.helpers as h | ||||
r1056 | ||||
from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAllDecorator, \ | ||||
r1060 | HasRepoPermissionAnyDecorator, NotAnonymous | |||
r1045 | from rhodecode.lib.base import BaseRepoController, render | |||
r547 | from rhodecode.lib.utils import invalidate_cache, action_logger | |||
r1056 | ||||
r547 | from rhodecode.model.forms import RepoSettingsForm, RepoForkForm | |||
r629 | from rhodecode.model.repo import RepoModel | |||
r1015 | from rhodecode.model.db import User | |||
r547 | ||||
log = logging.getLogger(__name__) | ||||
r1045 | class SettingsController(BaseRepoController): | |||
r547 | ||||
@LoginRequired() | ||||
def __before__(self): | ||||
super(SettingsController, self).__before__() | ||||
r636 | ||||
r1054 | @HasRepoPermissionAllDecorator('repository.admin') | |||
r547 | def index(self, repo_name): | |||
repo_model = RepoModel() | ||||
r735 | c.repo_info = repo = repo_model.get_by_repo_name(repo_name) | |||
r547 | if not repo: | |||
r636 | h.flash(_('%s repository is not mapped to db perhaps' | |||
r860 | ' it was created or renamed from the file system' | |||
r547 | ' please run the application again' | |||
' in order to rescan repositories') % repo_name, | ||||
category='error') | ||||
r636 | ||||
return redirect(url('home')) | ||||
r1015 | ||||
r547 | c.users_array = repo_model.get_users_js() | |||
r1015 | c.users_groups_array = repo_model.get_users_groups_js() | |||
r636 | ||||
r1015 | defaults = c.repo_info.get_dict() | |||
#fill owner | ||||
if c.repo_info.user: | ||||
defaults.update({'user':c.repo_info.user.username}) | ||||
else: | ||||
replacement_user = self.sa.query(User)\ | ||||
.filter(User.admin == True).first().username | ||||
defaults.update({'user':replacement_user}) | ||||
#fill repository users | ||||
r547 | for p in c.repo_info.repo_to_perm: | |||
r1015 | defaults.update({'u_perm_%s' % p.user.username: | |||
p.permission.permission_name}) | ||||
#fill repository groups | ||||
for p in c.repo_info.users_group_to_perm: | ||||
defaults.update({'g_perm_%s' % p.users_group.users_group_name: | ||||
r547 | p.permission.permission_name}) | |||
r636 | ||||
r1067 | return htmlfill.render( | |||
r547 | render('settings/repo_settings.html'), | |||
defaults=defaults, | ||||
encoding="UTF-8", | ||||
force_defaults=False | ||||
r636 | ) | |||
r547 | ||||
r1054 | @HasRepoPermissionAllDecorator('repository.admin') | |||
r547 | def update(self, repo_name): | |||
repo_model = RepoModel() | ||||
changed_name = repo_name | ||||
_form = RepoSettingsForm(edit=True, old_data={'repo_name':repo_name})() | ||||
try: | ||||
form_result = _form.to_python(dict(request.POST)) | ||||
repo_model.update(repo_name, form_result) | ||||
r665 | invalidate_cache('get_repo_cached_%s' % repo_name) | |||
r547 | h.flash(_('Repository %s updated successfully' % repo_name), | |||
category='success') | ||||
r636 | changed_name = form_result['repo_name'] | |||
r660 | action_logger(self.rhodecode_user, 'user_updated_repo', | |||
changed_name, '', self.sa) | ||||
r564 | except formencode.Invalid, errors: | |||
r735 | c.repo_info = repo_model.get_by_repo_name(repo_name) | |||
r547 | c.users_array = repo_model.get_users_js() | |||
errors.value.update({'user':c.repo_info.user.username}) | ||||
r1067 | return htmlfill.render( | |||
r547 | render('settings/repo_settings.html'), | |||
defaults=errors.value, | ||||
errors=errors.error_dict or {}, | ||||
prefix_error=False, | ||||
r636 | encoding="UTF-8") | |||
r547 | except Exception: | |||
log.error(traceback.format_exc()) | ||||
r665 | h.flash(_('error occurred during update of repository %s') \ | |||
r547 | % repo_name, category='error') | |||
r636 | ||||
r547 | return redirect(url('repo_settings_home', repo_name=changed_name)) | |||
r1054 | @HasRepoPermissionAllDecorator('repository.admin') | |||
r636 | def delete(self, repo_name): | |||
r547 | """DELETE /repos/repo_name: Delete an existing item""" | |||
# Forms posted to this method should contain a hidden field: | ||||
# <input type="hidden" name="_method" value="DELETE" /> | ||||
# Or using helpers: | ||||
# h.form(url('repo_settings_delete', repo_name=ID), | ||||
# method='delete') | ||||
# url('repo_settings_delete', repo_name=ID) | ||||
r636 | ||||
r547 | repo_model = RepoModel() | |||
r735 | repo = repo_model.get_by_repo_name(repo_name) | |||
r547 | if not repo: | |||
r636 | h.flash(_('%s repository is not mapped to db perhaps' | |||
r547 | ' it was moved or renamed from the filesystem' | |||
' please run the application again' | ||||
' in order to rescan repositories') % repo_name, | ||||
category='error') | ||||
r636 | ||||
return redirect(url('home')) | ||||
r547 | try: | |||
r564 | action_logger(self.rhodecode_user, 'user_deleted_repo', | |||
r636 | repo_name, '', self.sa) | |||
repo_model.delete(repo) | ||||
r665 | invalidate_cache('get_repo_cached_%s' % repo_name) | |||
r547 | h.flash(_('deleted repository %s') % repo_name, category='success') | |||
except Exception: | ||||
h.flash(_('An error occurred during deletion of %s') % repo_name, | ||||
category='error') | ||||
r636 | ||||
return redirect(url('home')) | ||||
r1056 | @NotAnonymous() | |||
r1060 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |||
'repository.admin') | ||||
r547 | def fork(self, repo_name): | |||
repo_model = RepoModel() | ||||
r735 | c.repo_info = repo = repo_model.get_by_repo_name(repo_name) | |||
r547 | if not repo: | |||
r636 | h.flash(_('%s repository is not mapped to db perhaps' | |||
r860 | ' it was created or renamed from the file system' | |||
r547 | ' please run the application again' | |||
' in order to rescan repositories') % repo_name, | ||||
category='error') | ||||
r636 | ||||
return redirect(url('home')) | ||||
r547 | return render('settings/repo_fork.html') | |||
r636 | ||||
r1056 | @NotAnonymous() | |||
r1060 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |||
'repository.admin') | ||||
r547 | def fork_create(self, repo_name): | |||
repo_model = RepoModel() | ||||
r735 | c.repo_info = repo_model.get_by_repo_name(repo_name) | |||
r659 | _form = RepoForkForm(old_data={'repo_type':c.repo_info.repo_type})() | |||
r547 | form_result = {} | |||
try: | ||||
form_result = _form.to_python(dict(request.POST)) | ||||
form_result.update({'repo_name':repo_name}) | ||||
r1121 | repo_model.create_fork(form_result, self.rhodecode_user) | |||
r718 | h.flash(_('forked %s repository as %s') \ | |||
r547 | % (repo_name, form_result['fork_name']), | |||
category='success') | ||||
r718 | action_logger(self.rhodecode_user, | |||
'user_forked_repo:%s' % form_result['fork_name'], | ||||
repo_name, '', self.sa) | ||||
r564 | except formencode.Invalid, errors: | |||
r547 | c.new_repo = errors.value['fork_name'] | |||
r = render('settings/repo_fork.html') | ||||
r636 | ||||
r1067 | return htmlfill.render( | |||
r547 | r, | |||
defaults=errors.value, | ||||
errors=errors.error_dict or {}, | ||||
prefix_error=False, | ||||
r636 | encoding="UTF-8") | |||
return redirect(url('home')) | ||||