示例#1
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf['pylons.app_globals']
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    return {
        'config': conf,
        'c': c,
        'tmpl_context': c,
        'g': g,
        'app_globals': g,
        'h': conf['pylons.h'],
        'request': request._current_obj(),
        'response': response, # don't eval the SOP because this is rarely used
        'translator': t,
        'ngettext': t.ngettext,
        'ungettext': t.ungettext, # compat with standard pylons_globals()
        '_': t.gettext,
        'N_': N_,
        'XML': XML,
    }
示例#2
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf['pylons.app_globals']
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    req = request._current_obj()
    return {
        'config': conf,
        'c': c,
        'tmpl_context': c,
        'g': g,
        'app_globals': g,
        'h': conf['pylons.h'],
        'request': req,
        'settings': req.settings,
        'response': response, # don't eval the SOP because this is rarely used
        'translator': t,
        'ngettext': t.ngettext,
        'ungettext': t.ungettext, # compat with standard pylons_globals()
        '_': t.gettext,
        'N_': N_,
        'XML': XML,
    }
示例#3
0
def tmpl_globals():
    """Create and return a dictionary of global variables for all templates.

    This function was adapted from :func:`pylons.templating.pylons_globals`
    to better suite our needs. In particular we inject our own gettext
    functions and get a performance boost from following the translator SOP
    once here instead of on every gettext call.

    """
    conf = config._current_obj()
    g = conf["pylons.app_globals"]
    c = tmpl_context._current_obj()
    t = translator._current_obj()
    req = request._current_obj()
    return {
        "config": conf,
        "c": c,
        "tmpl_context": c,
        "g": g,
        "app_globals": g,
        "h": conf["pylons.h"],
        "request": req,
        "settings": req.settings,
        "response": response,  # don't eval the SOP because this is rarely used
        "translator": t,
        "ngettext": t.ngettext,
        "ungettext": t.ungettext,  # compat with standard pylons_globals()
        "_": t.gettext,
        "N_": N_,
        "XML": XML,
    }
示例#4
0
def get_s3_bucket_url():
    """Get the currently configured S3 bucket URL and cache it."""
    c = tmpl_context._current_obj()
    if getattr(c, '_s3_bucket_url', ''):
        if c._s3_bucket_url == 'None':
            return None
        return c._s3_bucket_url
    storage = get_s3_storage()
    if storage:
        c._s3_bucket_url = url = storage.bucket_url
        return url
    else:
        c._s3_bucket_url = 'None'
        return None
示例#5
0
def get_s3_storage():
    """Helper for retrieving the current S3 Storage engine.

    We use this to get a boto connection to the configured bucket."""
    c = tmpl_context._current_obj()
    if getattr(c, '_s3_engine', ''):
        if c._s3_engine == 'None':
            return None
        return c._s3_engine
    engine = DBSession.query(AmazonS3Storage)\
        .filter(AmazonS3Storage.enabled == True)\
        .first()
    c._s3_engine = engine or 'None'
    return engine
示例#6
0
    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # WSGIController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        from rhodecode.lib import helpers as h

        # Provide the Pylons context to Pyramid's debugtoolbar if it asks
        if environ.get('debugtoolbar.wants_pylons_context', False):
            environ['debugtoolbar.pylons_context'] = c._current_obj()

        _route_name = '.'.join([environ['pylons.routes_dict']['controller'],
                                environ['pylons.routes_dict']['action']])

        self.rc_config = SettingsModel().get_all_settings(cache=True)
        self.ip_addr = get_ip_addr(environ)

        # The rhodecode auth user is looked up and passed through the
        # environ by the pylons compatibility tween in pyramid.
        # So we can just grab it from there.
        auth_user = environ['rc_auth_user']

        # set globals for auth user
        request.user = auth_user
        c.rhodecode_user = self._rhodecode_user = auth_user

        log.info('IP: %s User: %s accessed %s [%s]' % (
            self.ip_addr, auth_user, safe_unicode(get_access_path(environ)),
            _route_name)
        )

        # TODO: Maybe this should be move to pyramid to cover all views.
        # check user attributes for password change flag
        user_obj = auth_user.get_instance()
        if user_obj and user_obj.user_data.get('force_password_change'):
            h.flash('You are required to change your password', 'warning',
                    ignore_duplicate=True)

            skip_user_check_urls = [
                'error.document', 'login.logout', 'login.index',
                'admin/my_account.my_account_password',
                'admin/my_account.my_account_password_update'
            ]
            if _route_name not in skip_user_check_urls:
                return self._dispatch_redirect(
                    url('my_account_password'), environ, start_response)

        return WSGIController.__call__(self, environ, start_response)
    def _handle_validation_errors(self, args, kwargs, exception):
        """
        Sets up tmpl_context.form_values and tmpl_context.form_errors to assist
        generating a form with given values and the validation failure
        messages.
        """
        c = tmpl_context._current_obj()
        c.validation_exception = exception

        # Set up the tmpl_context.form_values dict with the invalid values
        c.form_values = exception.value

        # Set up the tmpl_context.form_errors dict
        c.form_errors = exception.unpack_errors()
        if not isinstance(c.form_errors, dict):
            c.form_errors = {'_the_form': c.form_errors}

        return self._call_error_handler(args, kwargs)
    def _handle_validation_errors(self, args, kwargs, exception):
        """
        Sets up tmpl_context.form_values and tmpl_context.form_errors to assist
        generating a form with given values and the validation failure
        messages.
        """
        c = tmpl_context._current_obj()
        c.validation_exception = exception

        # Set up the tmpl_context.form_values dict with the invalid values
        c.form_values = exception.value

        # Set up the tmpl_context.form_errors dict
        c.form_errors = exception.unpack_errors()
        if not isinstance(c.form_errors, dict):
            c.form_errors = {'_the_form': c.form_errors}

        return self._call_error_handler(args, kwargs)
示例#9
0
文件: media.py 项目: knyar/mediadrop
 def json_error(self, *args, **kwargs):
     validation_exception = tmpl_context._current_obj().validation_exception
     return dict(success=False, message=validation_exception.msg)
示例#10
0
 def json_error(self, *args, **kwargs):
     validation_exception = tmpl_context._current_obj().validation_exception
     return dict(success=False, message=validation_exception.msg)