Exemple #1
0
 def render(cls, template_name, template_values=None, suffix="html"):
     if template_values is None: template_values = {}        
     template_values['template_name'] = template_name
     log.info("TEMPLATE %s: %s" % (template_name, template_values))        
     renderer = render_jinja(os.path.dirname(__file__) + '/../templates/')      
     renderer._lookup.filters.update(custom_filters.filters)
     return (renderer[template_name + "." + suffix](template_values)).encode('utf-8')
 def render(cls, template_name, template_values=None, suffix="html"):
     if template_values is None: template_values = {}
     template_values['template_name'] = template_name
     renderer = render_jinja(os.path.dirname(__file__) + '/../templates/')
     renderer._lookup.filters.update(custom_filters.filters)
     return (renderer[template_name + "." +
                      suffix](template_values)).encode('utf-8')
Exemple #3
0
    def render(self, template_name, template_values=None, suffix="html", content_type = "text/html", status="200 OK"):
        """
        Custom renderer for Change by Us templates.

        @type   template_name: string
        @param  template_name: Name of template (without extension)
        @type   template_values: dict
        @param  template_values: Values to include in the template.
        @type   suffix: string
        @param  suffix: Extension of template file.
        @type   content_type: string
        @param  content_type: HTTP header content type to output.

        @rtype: ?
        @returns: ?

        """
        if template_values is None:
            template_values = {}

        # Set the user object in case it's been created since we initialized.
        self.setUserObject()

        # Hand all config values to the template.  This method is deprecated
        # but around until all templates have been updated.
        config = Config.get_all()

        config['base_url'] = Config.base_url()
        for key in config:
            if type(config[key]) is dict:
                for param in config[key]:
                    template_values["%s_%s" % (key, param)] = config[key][param]
            else:
                template_values[key] = config[key]

        # Give all config values as a dict in a config space.
        template_values['config'] = config

        # Send user data to template
        if self.user:
            template_values['user'] = self.user
            template_values['sqla_user'] = self.sqla_user

        # Add template data object
        if self.template_data:
            template_values['template_data'] = self.template_data

        # Create full URL from web.py values
        template_values['full_url'] = web.ctx.home + web.ctx.fullpath

        # Check for "flash"?
        if hasattr(self.session, 'flash') and self.session.flash is not None:
            template_values['flash'] = self.session.flash
            log.info('showing flash message: "' + self.session.flash + '"')
            self.session.flash = None
            self.session.invalidate()

        template_values['session_id'] = self.session.session_id

        # Put session values into template ??
        keys = self.session.keys()
        for key in keys:
            template_values[key] = self.session[key]

        # Set up template and Jinja
        template_values['template_name'] = template_name
        renderer = render_jinja(os.path.dirname(__file__) + '/../templates/',
            extensions=['jinja2.ext.i18n',
                        'jinja2.ext.with_',])
        renderer._lookup.filters.update(custom_filters.filters)

        # Install the translation
        translation = self.get_gettext_translation(self.get_language())
        renderer._lookup.install_gettext_translations(translation)

        # Insert HTML for the language chooser
        curr_lang = self.get_language()
        all_langs = self.get_supported_languages()

        template_values['language'] = {"current": curr_lang, "list":
                all_langs.iteritems()}

        template_values['language_selector'] = self.choice_list(
            all_langs, curr_lang)

        # Set HTTP header
        web.header("Content-Type", content_type)

        # Debug data.
        log.info("%s: %s (%s)" % (status, content_type, template_name))
        log.info("*** session  = %s" % self.session)

        # Set status
        web.ctx.status = status

        # Return template and data.
        return (renderer[template_name + "." + suffix](dict(d=template_values))).encode('utf-8')
Exemple #4
0
    def render(self,
               template_name,
               template_values=None,
               suffix="html",
               content_type="text/html",
               status="200 OK"):
        """
        Custom renderer for Change by Us templates.

        @type   template_name: string
        @param  template_name: Name of template (without extension)
        @type   template_values: dict
        @param  template_values: Values to include in the template.
        @type   suffix: string
        @param  suffix: Extension of template file.
        @type   content_type: string
        @param  content_type: HTTP header content type to output.

        @rtype: ?
        @returns: ?

        """
        if template_values is None:
            template_values = {}

        # Set the user object in case it's been created since we initialized.
        self.setUserObject()

        # Hand all config values to the template.  This method is deprecated
        # but around until all templates have been updated.
        config = Config.get_all()

        config['base_url'] = Config.base_url()
        for key in config:
            if type(config[key]) is dict:
                for param in config[key]:
                    template_values["%s_%s" %
                                    (key, param)] = config[key][param]
            else:
                template_values[key] = config[key]

        # Give all config values as a dict in a config space.
        template_values['config'] = config

        # Send user data to template
        if self.user:
            template_values['user'] = self.user
            template_values['sqla_user'] = self.sqla_user

        # Add template data object
        if self.template_data:
            template_values['template_data'] = self.template_data

        # Create full URL from web.py values
        template_values['full_url'] = web.ctx.home + web.ctx.fullpath

        # Check for "flash"?
        if hasattr(self.session, 'flash') and self.session.flash is not None:
            template_values['flash'] = self.session.flash
            log.info('showing flash message: "' + self.session.flash + '"')
            self.session.flash = None
            self.session.invalidate()

        template_values['session_id'] = self.session.session_id

        # Put session values into template ??
        keys = self.session.keys()
        for key in keys:
            template_values[key] = self.session[key]

        # Set up template and Jinja
        template_values['template_name'] = template_name
        renderer = render_jinja(os.path.dirname(__file__) + '/../templates/',
                                extensions=[
                                    'jinja2.ext.i18n',
                                    'jinja2.ext.with_',
                                ])
        renderer._lookup.filters.update(custom_filters.filters)

        # Install the translation
        translation = self.get_gettext_translation(self.get_language())
        renderer._lookup.install_gettext_translations(translation)

        # Insert HTML for the language chooser
        curr_lang = self.get_language()
        all_langs = self.get_supported_languages()

        template_values['language'] = {
            "current": curr_lang,
            "list": all_langs.iteritems()
        }

        template_values['language_selector'] = self.choice_list(
            all_langs, curr_lang)

        # Set HTTP header
        web.header("Content-Type", content_type)

        # Debug data.
        log.info("%s: %s (%s)" % (status, content_type, template_name))
        log.info("*** session  = %s" % self.session)

        # Set status
        web.ctx.status = status

        # Return template and data.
        return (renderer[template_name + "." + suffix](
            dict(d=template_values))).encode('utf-8')