# HG changeset patch # User Martin Bornhold # Date 2016-06-29 07:49:55 # Node ID d27ab83456b181ba5b7c5c784a01528ef6671b01 # Parent c84fdf50ba189b5eca10bb71900bcc18ea12a794 admin: Add helper to get the registry from requests. diff --git a/rhodecode/admin/navigation.py b/rhodecode/admin/navigation.py --- a/rhodecode/admin/navigation.py +++ b/rhodecode/admin/navigation.py @@ -27,6 +27,7 @@ from pylons.i18n.translation import lazy from zope.interface import implementer from rhodecode.admin.interfaces import IAdminNavigationRegistry +from rhodecode.lib.utils import get_registry log = logging.getLogger(__name__) @@ -35,6 +36,15 @@ NavListEntry = collections.namedtuple('N class NavEntry(object): + """ + Represents an entry in the admin navigation. + + :param key: Unique identifier used to store reference in an OrderedDict. + :param name: Display name, usually a translation string. + :param view_name: Name of the view, used generate the URL. + :param pyramid: Indicator to use pyramid for URL generation. This should + be removed as soon as we are fully migrated to pyramid. + """ def __init__(self, key, name, view_name, pyramid=False): self.key = key @@ -106,7 +116,7 @@ def navigation_registry(request): """ Helper that returns the admin navigation registry. """ - pyramid_registry = request.registry + pyramid_registry = get_registry(request) nav_registry = pyramid_registry.queryUtility(IAdminNavigationRegistry) return nav_registry diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -33,14 +33,14 @@ import tempfile import traceback import tarfile import warnings -from os.path import abspath -from os.path import dirname as dn, join as jn +from os.path import join as jn import paste import pkg_resources from paste.script.command import Command, BadCommand from webhelpers.text import collapse, remove_formatting, strip_tags from mako import exceptions +from pyramid.threadlocal import get_current_registry from rhodecode.lib.fakemod import create_module from rhodecode.lib.vcs.backends.base import Config @@ -975,3 +975,16 @@ def read_opensource_licenses(): _license_cache = json.loads(licenses) return _license_cache + + +def get_registry(request): + """ + Utility to get the pyramid registry from a request. During migration to + pyramid we sometimes want to use the pyramid registry from pylons context. + Therefore this utility returns `request.registry` for pyramid requests and + uses `get_current_registry()` for pylons requests. + """ + try: + return request.registry + except AttributeError: + return get_current_registry()