Exemple #1
0
    def identity(self):
        try:
            identity = cherrypy.request.identity
        except AttributeError:
            identity = None

        if not identity:
            if not request_available():
                raise RequestRequiredException()
            raise IdentityManagementNotEnabledException()

        return identity
Exemple #2
0
    def identity(self):
        try:
            identity = cherrypy.request.identity
        except AttributeError:
            identity = None

        if not identity:
            if not request_available():
                raise RequestRequiredException()
            raise IdentityManagementNotEnabledException()

        return identity
Exemple #3
0
def url(tgpath, tgparams=None, **kw):
    """Computes URLs.

    tgpath can be a list or a string. If the path is absolute (starts
    with a "/"), the server.webpath, SCRIPT_NAME and the approot of the
    application are prepended to the path. In order for the approot to
    be detected properly, the root object should extend
    controllers.RootController.

    Query parameters for the URL can be passed in as a dictionary in
    the second argument *or* as keyword parameters.

    Values which are a list or a tuple are used to create multiple
    key-value pairs.

    """
    if not isinstance(tgpath, basestring):
        tgpath = "/".join(list(tgpath))
    webpath = config.server.get("server.webpath", "")
    if tg_util.request_available():
        tgpath = webpath + cp_url(tgpath, relative = 'server')
    elif tgpath.startswith("/"):
        tgpath = webpath + tgpath
    if tgparams is None:
        tgparams = kw
    else:
        try:
            tgparams = tgparams.copy()
            tgparams.update(kw)
        except AttributeError:
            raise TypeError('url() expects a dictionary for query parameters')
    args = []
    for key, value in tgparams.iteritems():
        if value is None:
            continue
        if isinstance(value, (list, tuple)):
            pairs = [(key, v) for v in value]
        else:
            pairs = [(key, value)]
        for k, v in pairs:
            if v is None:
                continue
            if isinstance(v, unicode):
                v = v.encode('utf8')
            args.append((k, str(v)))
    if args:
        query_string = urllib.urlencode(args, True)
        if '?' in tgpath:
            tgpath += '&' + query_string
        else:
            tgpath += '?' + query_string
    return tgpath
Exemple #4
0
def gettext(key, locale=None, domain=None):
    """Get the gettext value for key.

    Added to builtins as '_'. Returns Unicode string.

    @param key: text to be translated
    @param locale: locale code to be used.
        If locale is None, gets the value provided by get_locale.

    """
    if request_available():
        return plain_gettext(key, locale, domain)
    else:
        return lazy_gettext(key, locale, domain)
Exemple #5
0
    def __getattr__(self, name):
        try:
            provider = cherrypy.request.identityProvider
        except AttributeError:
            try:
                provider = create_default_provider()
            except Exception:
                provider = None

        if provider is None:
            if not request_available():
                raise RequestRequiredException()
            raise IdentityManagementNotEnabledException()

        return getattr(provider, name)
Exemple #6
0
    def __getattr__(self, name):
        try:
            provider = cherrypy.request.identityProvider
        except AttributeError:
            try:
                provider = create_default_provider()
            except Exception:
                provider = None

        if provider is None:
            if not request_available():
                raise RequestRequiredException()
            raise IdentityManagementNotEnabledException()

        return getattr(provider, name)
Exemple #7
0
def ngettext(key1, key2, num, locale=None):
    """Translate two possible texts based on whether num is greater than 1.

    @param key1: text if num==1
    @param key2: text if num!=1
    @param num: a number
    @type num: integer
    @param locale: locale code to be used.
        If locale is None, gets the value provided by get_locale.

    """
    if request_available():
        return plain_ngettext(key1, key2, num, locale)
    else:
        return lazy_ngettext(key1, key2, num, locale)
Exemple #8
0
def _get_locale():
    """Default function for returning locale. First looks in session for locale key,
    then checks the HTTP Accept-Language header, and finally checks the config default
    locale setting. This can be replaced by your own function by setting cherrypy
    config setting i18n.get_locale to your function name.
    """
    if not request_available():
        return config.get("i18n.default_locale", "en")

    if config.get("tools.sessions.on", False):
        locale_key = config.get("i18n.session_key", "locale")
        locale = cherrypy.session.get(locale_key)
        if locale:
            return locale
    browser_accept_lang = _get_locale_from_accept_header()
    return browser_accept_lang or config.get("i18n.default_locale", "en")
Exemple #9
0
def _get_locale():
    """Default function for returning locale. First looks in session for locale key,
    then checks the HTTP Accept-Language header, and finally checks the config default
    locale setting. This can be replaced by your own function by setting cherrypy
    config setting i18n.get_locale to your function name.
    """
    if not request_available():
        return config.get("i18n.default_locale", "en")

    if config.get("tools.sessions.on", False):
        locale_key = config.get("i18n.session_key", "locale")
        locale = cherrypy.session.get(locale_key)
        if locale:
            return locale
    browser_accept_lang = _get_locale_from_accept_header()
    return browser_accept_lang or config.get("i18n.default_locale", "en")
Exemple #10
0
 def test_url_without_request_available(self):
     #Stopping the server in tearDown ensures that there's no request
     assert not util.request_available()
     assert url("/foo") == "/foo"
 def test_url_without_request_available(self):
     #Stopping the server in tearDown ensures that there's no request
     assert not util.request_available()
     assert url("/foo") == "/foo"