Beispiel #1
0
 def test_no_expression(self):
     text = 'a b c'
     self.assertEqual(parse_inline_template(text), [('a b c', '')])
Beispiel #2
0
    def _render_template_inline_template(self,
                                         template_txt,
                                         model,
                                         res_ids,
                                         add_context=None,
                                         options=None):
        """ Render a string-based template on records given by a model and a list
        of IDs, using inline_template.

        In addition to the generic evaluation context available, some other
        variables are added:
          * ``object``: record based on which the template is rendered;

        :param str template_txt: template text to render
        :param str model: see ``MailRenderMixin._render_template()``;
        :param list res_ids: see ``MailRenderMixin._render_template()``;

        :param dict add_context: additional context to give to renderer. It
          allows to add or update values to base rendering context generated
          by ``MailRenderMixin._render_inline_template_eval_context()``;
        :param dict options: options for rendering;

        :return dict: {res_id: string of rendered template based on record}
        """
        # prevent wrong values (rendering on a void record set, ...)
        if any(r is None for r in res_ids):
            raise ValueError(
                _('Template rendering should be called on a valid record IDs.')
            )

        results = dict.fromkeys(res_ids, u"")
        if not template_txt:
            return results

        template_instructions = parse_inline_template(str(template_txt))
        is_dynamic = len(
            template_instructions) > 1 or template_instructions[0][1]

        if (not self._unrestricted_rendering and is_dynamic
                and not self.env.is_admin() and
                not self.env.user.has_group('mail.group_mail_template_editor')
            ):
            group = self.env.ref('mail.group_mail_template_editor')
            raise AccessError(
                _(
                    'Only users belonging to the "%s" group can modify dynamic templates.',
                    group.name))

        if not is_dynamic:
            # Either the content is a raw text without placeholders, either we fail to
            # detect placeholders code. In both case we skip the rendering and return
            # the raw content, so even if we failed to detect dynamic code,
            # non "mail_template_editor" users will not gain rendering tools available
            # only for template specific group users
            return {
                record_id: template_instructions[0][0]
                for record_id in res_ids
            }

        # prepare template variables
        variables = self._render_eval_context()
        if add_context:
            variables.update(**add_context)

        for record in self.env[model].browse(res_ids):
            variables['object'] = record

            try:
                results[record.id] = render_inline_template(
                    template_instructions, variables)
            except Exception as e:
                _logger.info("Failed to render inline_template: \n%s",
                             str(template_txt),
                             exc_info=True)
                raise UserError(
                    _("Failed to render inline_template template : %s)", e))

        return results
Beispiel #3
0
 def test_expression2(self):
     text = 'a {{b}} c'
     self.assertEqual(parse_inline_template(text), [('a ', 'b'),
                                                    (' c', '')])