Exemple #1
0
def my_pylons_globals():
    """Create and return a dictionary of global Pylons variables

    Render functions should call this to retrieve a list of global
    Pylons variables that should be included in the global template
    namespace if possible.

    Pylons variables that are returned in the dictionary:
        ``c``, ``g``, ``h``, ``_``, ``N_``, config, request, response,
        translator, ungettext, ``url``

    If SessionMiddleware is being used, ``session`` will also be
    available in the template namespace.

    """

    conf = pylons.config._current_obj()
    c = pylons.tmpl_context._current_obj()
    g = conf.get('pylons.app_globals') or conf['pylons.g']

    try:
        h = conf.package.lib.helpers

    except (AttributeError, KeyError):
        h = Bunch()

    pylons_vars = dict(
        c=c,
        tmpl_context=c,
        config=conf,
        app_globals=g,
        g=g,
        h=h,
        #h=conf.get('pylons.h') or pylons.h._current_obj(),
        request=pylons.request._current_obj(),
        response=pylons.response._current_obj(),
        url=pylons.url._current_obj(),
        translator=pylons.translator._current_obj(),
        ungettext=pylons.i18n.ungettext,
        _=pylons.i18n._,
        N_=pylons.i18n.N_)

    # If the session was overriden to be None, don't populate the session
    # var
    econf = pylons.config['pylons.environ_config']
    if 'beaker.session' in pylons.request.environ or \
        ('session' in econf and econf['session'] in pylons.request.environ):
        pylons_vars['session'] = pylons.session._current_obj()
    templating.log.debug("Created render namespace with pylons vars: %s",
                         pylons_vars)
    return pylons_vars
Exemple #2
0
 def before_render(self, remainder, params, output):
     if not isinstance(output, dict) or not self.name in output:
         return
     collection = output[self.name]
     page = Page(collection,
                 request.paginate_page,
                 request.paginate_items_per_page,
                 controller='/')
     page.kwargs = request.paginate_params
     if self.page_param != 'name':
         page.pager = partial(page.pager, page_param=self.page_param)
     if not getattr(tmpl_context, 'paginators', None):
         tmpl_context.paginators = Bunch()
     tmpl_context.paginators[self.name] = output[self.name] = page
        def _w(*args, **kwargs):
            page = int(kwargs.pop(own_parameters["page"], 1))
            real_items_per_page = int(
                kwargs.pop(own_parameters['items_per_page'], items_per_page))
            argvars = inspect.getargspec(f)[0][1:]
            if argvars:
                args = list(args)
                for i, var in enumerate(args):
                    if i >= len(argvars):
                        break
                    var = argvars[i]
                    if var in kwargs:
                        args[i] = kwargs[var]
                        del kwargs[var]

            res = f(*args, **kwargs)
            if isinstance(res, dict) and name in res:
                additional_parameters = MultiDict()
                for key, value in request.str_params.iteritems():
                    if key not in own_parameters:
                        additional_parameters.add(key, value)

                collection = res[name]
                page = Page(collection,
                            page,
                            items_per_page=real_items_per_page,
                            **additional_parameters.dict_of_lists())
                # wrap the pager so that it will render
                # the proper page-parameter
                page.pager = partial(page.pager,
                                     page_param=own_parameters["page"])
                res[name] = page
                # this is a bit strange - it appears
                # as if c returns an empty
                # string for everything it dosen't know.
                # I didn't find that documented, so I
                # just put this in here and hope it works.
                if not hasattr(c, 'paginators') or type(c.paginators) == str:
                    c.paginators = Bunch()
                c.paginators[name] = page
            return res
Exemple #4
0
        inputs = getattr(tmpl_context, "form_values", {}),
        request = tg.request,
        auth_stack_enabled = 'repoze.who.plugins' in tg.request.environ,
        predicates = predicates,
        )

    try:
        h = config.package.lib.helpers
    except AttributeError, ImportError:
        h = Bunch()

    root_vars = Bunch(
        c = tmpl_context,
        tmpl_context = tmpl_context,
        response = response,
        request = request,
        url = tg.url,
        helpers = h,
        h = h,
        tg = tg_vars
        )
    #Allow users to provide a callable that defines extra vars to be
    #added to the template namespace
    variable_provider = config.get('variable_provider', None)
    if variable_provider:
        root_vars.update(variable_provider())
    return root_vars


def render(template_vars, template_engine=None, template_name=None, **kwargs):

    render_function = None
Exemple #5
0
def _get_tg_vars():
    """Create a Bunch of variables that should be available in all templates.

    These variables are:

    WARNING: This function should not be called from outside of the render()
    code.  Please consider this function as private.

    quote_plus
        the urllib quote_plus function
    url
        the turbogears.url function for creating flexible URLs
    identity
        the current visitor's identity information
    session
        the current beaker.session if the session_filter.on it set
        in the app.cfg configuration file. If it is not set then session
        will be None.
    locale
        the default locale
    inputs
        input values from a form
    errors
        validation errors
    request
        the WebOb Request Object
    config
        the app's config object
    auth_stack_enabled
        A boolean that determines if the auth stack is present in the environment
    predicates
        The :mod:`repoze.what.predicates` module.

    """
    # TODO: Implement user_agent and other missing features.
    tg_vars = Bunch(
        config=tg.config,
        flash_obj=tg.flash,
        flash=DeprecatedFlashVariable(
            lambda: tg.flash.message,
            "flash is deprecated, please use flash_obj.message instead "
            "or use the new flash_obj.render() method"),
        flash_status=DeprecatedFlashVariable(
            lambda: 'status_' + tg.flash.status,
            "flash_status is deprecated, please use flash_obj.status instead "
            "or use the new flash_obj.render() method"),
        quote_plus=quote_plus,
        url=tg.url,
        # this will be None if no identity
        identity=request.environ.get('repoze.who.identity'),
        session=session,
        locale=tg.request.accept_language.best_matches(),
        errors=getattr(tmpl_context, "form_errors", {}),
        inputs=getattr(tmpl_context, "form_values", {}),
        request=tg.request,
        auth_stack_enabled='repoze.who.plugins' in tg.request.environ,
        predicates=predicates,
    )

    # TODO: we should actually just get helpers from the package's helpers
    # module and dump the use of the SOP.
    helpers = config.get('pylons.h') or config.get('pylons.helpers')

    root_vars = Bunch(c=tmpl_context,
                      tmpl_context=tmpl_context,
                      response=response,
                      request=request,
                      url=tg.url,
                      helpers=helpers,
                      tg=tg_vars)
    #Allow users to provide a callable that defines extra vars to be
    #added to the template namespace
    variable_provider = config.get('variable_provider', None)
    if variable_provider:
        root_vars.update(variable_provider())
    return root_vars
Exemple #6
0
def _get_tg_vars():
    """Create a Bunch of variables that should be available in all templates.

    These variables are:

    WARNING: This function should not be called from outside of the render()
    code.  Please consider this function as private.

    quote_plus
        the urllib quote_plus function
    url
        the turbogears.url function for creating flexible URLs
    identity
        the current visitor's identity information
    session
        the current beaker.session if the session_filter.on it set
        in the app.cfg configuration file. If it is not set then session
        will be None.
    locale
        the default locale
    inputs
        input values from a form
    errors
        validation errors
    request
        the WebOb Request Object
    config
        the app's config object
    auth_stack_enabled
        A boolean that determines if the auth stack is present in the environment
    predicates
        The :mod:`repoze.what.predicates` module.

    """
    # TODO: Implement user_agent and other missing features.
    tg_vars = Bunch(
        config=tg.config,
        flash_obj=tg.flash,
        flash=DeprecatedFlashVariable(
            lambda: tg.flash.message,
            "flash is deprecated, please use flash_obj.message instead "
            "or use the new flash_obj.render() method"),
        flash_status=DeprecatedFlashVariable(
            lambda: 'status_' + tg.flash.status,
            "flash_status is deprecated, please use flash_obj.status instead "
            "or use the new flash_obj.render() method"),
        quote_plus=quote_plus,
        url=tg.url,
        # this will be None if no identity
        identity=request.environ.get('repoze.who.identity'),
        session=session,
        locale=tg.request.accept_language.best_matches(),
        errors=getattr(tmpl_context, "form_errors", {}),
        inputs=getattr(tmpl_context, "form_values", {}),
        request=tg.request,
        auth_stack_enabled='repoze.who.plugins' in tg.request.environ,
        predicates=predicates,
    )

    try:
        h = config.package.lib.helpers
    except AttributeError, ImportError:
        h = Bunch()
Exemple #7
0
        errors=getattr(tmpl_context, "form_errors", {}),
        inputs=getattr(tmpl_context, "form_values", {}),
        request=tg.request,
        auth_stack_enabled='repoze.who.plugins' in tg.request.environ,
        predicates=predicates,
    )

    try:
        h = config.package.lib.helpers
    except AttributeError, ImportError:
        h = Bunch()

    root_vars = Bunch(c=tmpl_context,
                      tmpl_context=tmpl_context,
                      response=response,
                      request=request,
                      url=tg.url,
                      helpers=h,
                      h=h,
                      tg=tg_vars)
    #Allow users to provide a callable that defines extra vars to be
    #added to the template namespace
    variable_provider = config.get('variable_provider', None)
    if variable_provider:
        root_vars.update(variable_provider())
    return root_vars


def render(template_vars, template_engine=None, template_name=None, **kwargs):

    render_function = None
    if template_engine is not None:
Exemple #8
0
def _get_tg_vars():
    """Create a Bunch of variables that should be available in all templates.

    These variables are:

    WARNING: This function should not be called from outside of the render()
    code.  Please consider this function as private.

    quote_plus
        the urllib quote_plus function
    url
        the turbogears.url function for creating flexible URLs
    identity
        the current visitor's identity information
    session
        the current beaker.session if the session_filter.on it set
        in the app.cfg configuration file. If it is not set then session
        will be None.
    locale
        the default locale
    inputs
        input values from a form
    errors
        validation errors
    request
        the WebOb Request Object
    config
        the app's config object
    auth_stack_enabled
        A boolean that determines if the auth stack is present in the environment
    predicates
        The :mod:`repoze.what.predicates` module.

    """
    # TODO: Implement user_agent and other missing features.
    tg_vars = Bunch(
        config = tg.config,
        flash_obj = tg.flash,
        flash = DeprecatedFlashVariable(
            lambda: tg.flash.message,
            "flash is deprecated, please use flash_obj.message instead "
            "or use the new flash_obj.render() method"
            ),
        flash_status = DeprecatedFlashVariable(
            lambda: 'status_' + tg.flash.status,
            "flash_status is deprecated, please use flash_obj.status instead "
            "or use the new flash_obj.render() method"
            ),
        quote_plus = quote_plus,
        url = tg.url,
        # this will be None if no identity
        identity = request.environ.get('repoze.who.identity'),
        session = session,
        locale = tg.request.accept_language.best_matches(),
        errors = getattr(tmpl_context, "form_errors", {}),
        inputs = getattr(tmpl_context, "form_values", {}),
        request = tg.request,
        auth_stack_enabled = 'repoze.who.plugins' in tg.request.environ,
        predicates = predicates,
        )

    # TODO in 2.2: we should actually just get helpers from the package's helpers
    # module and dump the use of the SOP.
    
    #########
    #try: 
    #    helpers = config['package'].lib.helpers 
    #except ImportError: 
    #    helpers = Bunch()
    #########

    helpers = config.get('pylons.h') or config.get('pylons.helpers')

    
    root_vars = Bunch(
        c = tmpl_context,
        tmpl_context = tmpl_context,
        response = response,
        request = request,
        url = tg.url,
        helpers = helpers,
        tg = tg_vars
        )
    #Allow users to provide a callable that defines extra vars to be
    #added to the template namespace
    variable_provider = config.get('variable_provider', None)
    if variable_provider:
        root_vars.update(variable_provider())
    return root_vars