admin: Add helper to get the registry from requests.

This commit is contained in:
Martin Bornhold 2016-06-29 09:49:55 +02:00
parent 670d237f39
commit 2b5bd1c3a9
2 changed files with 26 additions and 3 deletions

View file

@ -27,6 +27,7 @@ from pylons.i18n.translation import lazy_ugettext
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('NavListEntry', ['key', 'name', 'url'])
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

View file

@ -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()