# HG changeset patch # User Marcin Kuzminski # Date 2017-07-17 21:08:07 # Node ID d7d26d760e98d4649a1c251a7d0865b30a1f3679 # Parent e6df2b71564e8dddac1d7498afb00872f2450342 pyramid: moved add_route_requirements into pyramid base app. - use it integrations, and simplify the code. diff --git a/rhodecode/apps/_base/__init__.py b/rhodecode/apps/_base/__init__.py --- a/rhodecode/apps/_base/__init__.py +++ b/rhodecode/apps/_base/__init__.py @@ -37,6 +37,19 @@ log = logging.getLogger(__name__) ADMIN_PREFIX = '/_admin' STATIC_FILE_PREFIX = '/_static' +URL_NAME_REQUIREMENTS = { + # group name can have a slash in them, but they must not end with a slash + 'group_name': r'.*?[^/]', + 'repo_group_name': r'.*?[^/]', + # repo names can have a slash in them, but they must not end with a slash + 'repo_name': r'.*?[^/]', + # file path eats up everything at the end + 'f_path': r'.*', + # reference types + 'source_ref_type': '(branch|book|tag|rev|\%\(source_ref_type\)s)', + 'target_ref_type': '(branch|book|tag|rev|\%\(target_ref_type\)s)', +} + def add_route_with_slash(config,name, pattern, **kw): config.add_route(name, pattern, **kw) @@ -44,6 +57,17 @@ def add_route_with_slash(config,name, pa config.add_route(name + '_slash', pattern + '/', **kw) +def add_route_requirements(route_path, requirements=URL_NAME_REQUIREMENTS): + """ + Adds regex requirements to pyramid routes using a mapping dict + e.g:: + add_route_requirements('{repo_name}/settings') + """ + for key, regex in requirements.items(): + route_path = route_path.replace('{%s}' % key, '{%s:%s}' % (key, regex)) + return route_path + + def get_format_ref_id(repo): """Returns a `repo` specific reference formatter function""" if h.is_svn(repo): diff --git a/rhodecode/config/routing.py b/rhodecode/config/routing.py --- a/rhodecode/config/routing.py +++ b/rhodecode/config/routing.py @@ -51,19 +51,6 @@ URL_NAME_REQUIREMENTS = { } -def add_route_requirements(route_path, requirements): - """ - Adds regex requirements to pyramid routes using a mapping dict - - >>> add_route_requirements('/{action}/{id}', {'id': r'\d+'}) - '/{action}/{id:\d+}' - - """ - for key, regex in requirements.items(): - route_path = route_path.replace('{%s}' % key, '{%s:%s}' % (key, regex)) - return route_path - - class JSRoutesMapper(Mapper): """ Wrapper for routes.Mapper to make pyroutes compatible url definitions diff --git a/rhodecode/integrations/routes.py b/rhodecode/integrations/routes.py --- a/rhodecode/integrations/routes.py +++ b/rhodecode/integrations/routes.py @@ -20,9 +20,8 @@ import logging +from rhodecode.apps._base import ADMIN_PREFIX, add_route_requirements from rhodecode.model.db import Repository, Integration, RepoGroup -from rhodecode.config.routing import ( - ADMIN_PREFIX, add_route_requirements, URL_NAME_REQUIREMENTS) from rhodecode.integrations import integration_type_registry log = logging.getLogger(__name__) @@ -76,14 +75,12 @@ def includeme(config): config.add_route('repo_group_integrations_home', add_route_requirements( '{repo_group_name}/settings/integrations', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo_group,) ) config.add_route('repo_group_integrations_list', add_route_requirements( '{repo_group_name}/settings/integrations/{integration}', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo_group, valid_integration)) for route_name in ['repo_group_integrations_home', 'repo_group_integrations_list']: @@ -96,7 +93,6 @@ def includeme(config): config.add_route('repo_group_integrations_new', add_route_requirements( '{repo_group_name}/settings/integrations/new', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo_group,)) config.add_view('rhodecode.integrations.views.RepoGroupIntegrationsView', @@ -108,13 +104,11 @@ def includeme(config): config.add_route('repo_group_integrations_create', add_route_requirements( '{repo_group_name}/settings/integrations/{integration}/new', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo_group, valid_integration)) config.add_route('repo_group_integrations_edit', add_route_requirements( '{repo_group_name}/settings/integrations/{integration}/{integration_id}', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo_group, valid_integration)) for route_name in ['repo_group_integrations_edit', 'repo_group_integrations_create']: @@ -134,13 +128,11 @@ def includeme(config): config.add_route('repo_integrations_home', add_route_requirements( '{repo_name}/settings/integrations', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo,)) config.add_route('repo_integrations_list', add_route_requirements( '{repo_name}/settings/integrations/{integration}', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo, valid_integration)) for route_name in ['repo_integrations_home', 'repo_integrations_list']: @@ -153,7 +145,6 @@ def includeme(config): config.add_route('repo_integrations_new', add_route_requirements( '{repo_name}/settings/integrations/new', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo,)) config.add_view('rhodecode.integrations.views.RepoIntegrationsView', @@ -165,13 +156,11 @@ def includeme(config): config.add_route('repo_integrations_create', add_route_requirements( '{repo_name}/settings/integrations/{integration}/new', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo, valid_integration)) config.add_route('repo_integrations_edit', add_route_requirements( '{repo_name}/settings/integrations/{integration}/{integration_id}', - URL_NAME_REQUIREMENTS ), custom_predicates=(valid_repo, valid_integration)) for route_name in ['repo_integrations_edit', 'repo_integrations_create']: @@ -223,7 +212,6 @@ def valid_integration(info, request): if repo_name and repo_group: raise Exception('Either repo or repo_group can be set, not both') - if integration_id: integration = Integration.get(integration_id) if not integration: