# HG changeset patch # User Marcin Kuzminski # Date 2017-09-08 09:48:48 # Node ID 5470f1cf0c5b6ce37cf6cb0438f863f53c71767d # Parent 0e2995435c4853d39f1c125ea6792cefefa23c2b admin-defaults: ported views to pyramid. diff --git a/rhodecode/apps/admin/__init__.py b/rhodecode/apps/admin/__init__.py --- a/rhodecode/apps/admin/__init__.py +++ b/rhodecode/apps/admin/__init__.py @@ -71,6 +71,14 @@ def admin_routes(config): name='admin_settings_process_management_signal', pattern='/settings/process_management/signal') + # default settings + config.add_route( + name='admin_defaults_repositories', + pattern='/defaults/repositories') + config.add_route( + name='admin_defaults_repositories_update', + pattern='/defaults/repositories/update') + # global permissions config.add_route( diff --git a/rhodecode/tests/functional/test_admin_defaults.py b/rhodecode/apps/admin/tests/test_admin_defaults.py rename from rhodecode/tests/functional/test_admin_defaults.py rename to rhodecode/apps/admin/tests/test_admin_defaults.py --- a/rhodecode/tests/functional/test_admin_defaults.py +++ b/rhodecode/apps/admin/tests/test_admin_defaults.py @@ -20,15 +20,31 @@ import pytest -from rhodecode.tests import assert_session_flash, url +from rhodecode.tests import assert_session_flash from rhodecode.model.settings import SettingsModel +def route_path(name, params=None, **kwargs): + import urllib + from rhodecode.apps._base import ADMIN_PREFIX + + base_url = { + 'admin_defaults_repositories': + ADMIN_PREFIX + '/defaults/repositories', + 'admin_defaults_repositories_update': + ADMIN_PREFIX + '/defaults/repositories/update', + }[name].format(**kwargs) + + if params: + base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) + return base_url + + @pytest.mark.usefixtures("app") -class TestDefaultsController: +class TestDefaultsController(object): def test_index(self, autologin_user): - response = self.app.get(url('admin_defaults_repositories')) + response = self.app.get(route_path('admin_defaults_repositories')) response.mustcontain('default_repo_private') response.mustcontain('default_repo_enable_statistics') response.mustcontain('default_repo_enable_downloads') @@ -44,7 +60,7 @@ class TestDefaultsController: 'csrf_token': csrf_token, } response = self.app.post( - url('admin_defaults_repositories'), params=params) + route_path('admin_defaults_repositories_update'), params=params) assert_session_flash(response, 'Default settings updated successfully') defs = SettingsModel().get_default_repo_settings() @@ -61,8 +77,9 @@ class TestDefaultsController: 'csrf_token': csrf_token, } response = self.app.post( - url('admin_defaults_repositories'), params=params) + route_path('admin_defaults_repositories_update'), params=params) assert_session_flash(response, 'Default settings updated successfully') + defs = SettingsModel().get_default_repo_settings() del params['csrf_token'] assert params == defs diff --git a/rhodecode/apps/admin/views/defaults.py b/rhodecode/apps/admin/views/defaults.py new file mode 100644 --- /dev/null +++ b/rhodecode/apps/admin/views/defaults.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2016-2017 RhodeCode GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3 +# (only), as published by the Free Software Foundation. +# +# 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 Affero General Public License +# along with this program. If not, see . +# +# This program is dual-licensed. If you wish to learn more about the +# RhodeCode Enterprise Edition, including its added features, Support services, +# and proprietary license terms, please see https://rhodecode.com/licenses/ + +import logging + +import formencode +import formencode.htmlfill + +from pyramid.view import view_config +from pyramid.httpexceptions import HTTPFound +from pyramid.renderers import render +from pyramid.response import Response + +from rhodecode.apps._base import BaseAppView +from rhodecode.lib.auth import ( + LoginRequired, HasPermissionAllDecorator, CSRFRequired) +from rhodecode.lib import helpers as h +from rhodecode.model.forms import DefaultsForm +from rhodecode.model.meta import Session +from rhodecode import BACKENDS +from rhodecode.model.settings import SettingsModel + +log = logging.getLogger(__name__) + + +class AdminDefaultSettingsView(BaseAppView): + def load_default_context(self): + c = self._get_local_tmpl_context() + + self._register_global_c(c) + return c + + @LoginRequired() + @HasPermissionAllDecorator('hg.admin') + @view_config( + route_name='admin_defaults_repositories', request_method='GET', + renderer='rhodecode:templates/admin/defaults/defaults.mako') + def defaults_repository_show(self): + c = self.load_default_context() + c.backends = BACKENDS.keys() + c.active = 'repositories' + defaults = SettingsModel().get_default_repo_settings() + + data = render( + 'rhodecode:templates/admin/defaults/defaults.mako', + self._get_template_context(c), self.request) + html = formencode.htmlfill.render( + data, + defaults=defaults, + encoding="UTF-8", + force_defaults=False + ) + return Response(html) + + @LoginRequired() + @HasPermissionAllDecorator('hg.admin') + @CSRFRequired() + @view_config( + route_name='admin_defaults_repositories_update', request_method='POST', + renderer='rhodecode:templates/admin/defaults/defaults.mako') + def defaults_repository_update(self): + _ = self.request.translate + c = self.load_default_context() + c.active = 'repositories' + form = DefaultsForm()() + + try: + form_result = form.to_python(dict(self.request.POST)) + for k, v in form_result.iteritems(): + setting = SettingsModel().create_or_update_setting(k, v) + Session().add(setting) + Session().commit() + h.flash(_('Default settings updated successfully'), + category='success') + + except formencode.Invalid as errors: + data = render( + 'rhodecode:templates/admin/defaults/defaults.mako', + self._get_template_context(c), self.request) + html = formencode.htmlfill.render( + data, + defaults=errors.value, + errors=errors.error_dict or {}, + prefix_error=False, + encoding="UTF-8", + force_defaults=False + ) + return Response(html) + except Exception: + log.exception('Exception in update action') + h.flash(_('Error occurred during update of default values'), + category='error') + + raise HTTPFound(h.route_path('admin_defaults_repositories')) diff --git a/rhodecode/config/routing.py b/rhodecode/config/routing.py --- a/rhodecode/config/routing.py +++ b/rhodecode/config/routing.py @@ -254,14 +254,6 @@ def make_map(config): m.connect('edit_user_global_perms', '/users/{user_id}/edit/global_permissions', action='update_global_perms', conditions={'method': ['PUT']}) - # ADMIN DEFAULTS REST ROUTES - with rmap.submapper(path_prefix=ADMIN_PREFIX, - controller='admin/defaults') as m: - m.connect('admin_defaults_repositories', '/defaults/repositories', - action='update_repository_defaults', conditions={'method': ['POST']}) - m.connect('admin_defaults_repositories', '/defaults/repositories', - action='index', conditions={'method': ['GET']}) - # ADMIN SETTINGS ROUTES with rmap.submapper(path_prefix=ADMIN_PREFIX, controller='admin/settings') as m: diff --git a/rhodecode/controllers/admin/defaults.py b/rhodecode/controllers/admin/defaults.py deleted file mode 100644 --- a/rhodecode/controllers/admin/defaults.py +++ /dev/null @@ -1,102 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (C) 2010-2017 RhodeCode GmbH -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License, version 3 -# (only), as published by the Free Software Foundation. -# -# 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 Affero General Public License -# along with this program. If not, see . -# -# This program is dual-licensed. If you wish to learn more about the -# RhodeCode Enterprise Edition, including its added features, Support services, -# and proprietary license terms, please see https://rhodecode.com/licenses/ - -""" -default settings controller for RhodeCode Enterprise -""" - -import logging -import formencode -from formencode import htmlfill - -from pylons import request, tmpl_context as c, url -from pylons.controllers.util import redirect -from pylons.i18n.translation import _ - -from rhodecode.lib import auth -from rhodecode.lib import helpers as h -from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator -from rhodecode.lib.base import BaseController, render -from rhodecode.model.forms import DefaultsForm -from rhodecode.model.meta import Session -from rhodecode import BACKENDS -from rhodecode.model.settings import SettingsModel - -log = logging.getLogger(__name__) - - -class DefaultsController(BaseController): - - @LoginRequired() - def __before__(self): - super(DefaultsController, self).__before__() - - @HasPermissionAllDecorator('hg.admin') - def index(self): - """GET /defaults: All items in the collection""" - # url('admin_defaults_repositories') - c.backends = BACKENDS.keys() - c.active = 'repositories' - defaults = SettingsModel().get_default_repo_settings() - - return htmlfill.render( - render('admin/defaults/defaults.mako'), - defaults=defaults, - encoding="UTF-8", - force_defaults=False - ) - - @HasPermissionAllDecorator('hg.admin') - @auth.CSRFRequired() - def update_repository_defaults(self): - """PUT /defaults/repositories: Update an existing item""" - # Forms posted to this method should contain a hidden field: - # Or using helpers: - # h.form(url('admin_defaults_repositories'), - # method='post') - # url('admin_defaults_repositories') - c.active = 'repositories' - _form = DefaultsForm()() - - try: - form_result = _form.to_python(dict(request.POST)) - for k, v in form_result.iteritems(): - setting = SettingsModel().create_or_update_setting(k, v) - Session().add(setting) - Session().commit() - h.flash(_('Default settings updated successfully'), - category='success') - - except formencode.Invalid as errors: - defaults = errors.value - - return htmlfill.render( - render('admin/defaults/defaults.mako'), - defaults=defaults, - errors=errors.error_dict or {}, - prefix_error=False, - encoding="UTF-8", - force_defaults=False) - except Exception: - log.exception('Exception in update action') - h.flash(_('Error occurred during update of default values'), - category='error') - - return redirect(url('admin_defaults_repositories')) diff --git a/rhodecode/public/js/rhodecode/routes.js b/rhodecode/public/js/rhodecode/routes.js --- a/rhodecode/public/js/rhodecode/routes.js +++ b/rhodecode/public/js/rhodecode/routes.js @@ -47,6 +47,8 @@ function registerRCRoutes() { pyroutes.register('admin_settings_sessions_cleanup', '/_admin/settings/sessions/cleanup', []); pyroutes.register('admin_settings_process_management', '/_admin/settings/process_management', []); pyroutes.register('admin_settings_process_management_signal', '/_admin/settings/process_management/signal', []); + pyroutes.register('admin_defaults_repositories', '/_admin/defaults/repositories', []); + pyroutes.register('admin_defaults_repositories_update', '/_admin/defaults/repositories/update', []); pyroutes.register('admin_permissions_application', '/_admin/permissions/application', []); pyroutes.register('admin_permissions_application_update', '/_admin/permissions/application/update', []); pyroutes.register('admin_permissions_global', '/_admin/permissions/global', []); diff --git a/rhodecode/templates/admin/defaults/defaults.mako b/rhodecode/templates/admin/defaults/defaults.mako --- a/rhodecode/templates/admin/defaults/defaults.mako +++ b/rhodecode/templates/admin/defaults/defaults.mako @@ -28,7 +28,7 @@
- ${h.secure_form(h.url('admin_defaults_repositories'), method='post')} + ${h.secure_form(h.route_path('admin_defaults_repositories_update'), method='POST', request=request)}
diff --git a/rhodecode/templates/base/base.mako b/rhodecode/templates/base/base.mako --- a/rhodecode/templates/base/base.mako +++ b/rhodecode/templates/base/base.mako @@ -80,7 +80,7 @@
  • ${_('Permissions')}
  • ${_('Authentication')}
  • ${_('Integrations')}
  • -
  • ${_('Defaults')}
  • +
  • ${_('Defaults')}
  • ${_('Settings')}