Exemplo n.º 1
0
    def _render(self, template, locals_dict, tsession):
        """Render / process data for sending"""
        t0 = time.time()
        dom = etree.fromstring(template)
        qcontext = self._qcontext(locals_dict, tsession)
        html = QWeb().render(dom, qcontext)
        html = html and html.strip()
        render_time = time.time() - t0
        _logger.debug('Render in %.2fs\n qcontext:\n%s \nTemplate:\n%s\n',
                      render_time, qcontext, template)
        options = locals_dict['options']
        ret = {'photos': [], 'options': options, 'html': html}
        reply_markup = options.get('reply_markup')
        if reply_markup:
            ret['markup'] = _convert_markup(reply_markup)

        for photo in options.get('photos', []):
            if photo.get('type') == 'file':
                f = photo['data']
            else:
                # type is 'base64' by default'
                f = StringIO(base64.b64decode(photo['data']))
                f.name = photo.get('filename', 'item.png')
            ret['photos'].append({'file': f})
        return ret
Exemplo n.º 2
0
    def _render(self, template, locals_dict, tsession):
        """Render / process data for sending.
        Result can be cached and sent later.
        """
        t0 = time.time()
        dom = etree.fromstring(template)
        qcontext = self._qcontext(locals_dict, tsession)
        html = QWeb().render(dom, qcontext)
        html = html and html.strip()
        render_time = time.time() - t0
        _logger.debug('Render in %.2fs\n qcontext:\n%s \nTemplate:\n%s\n',
                      render_time, qcontext, template)
        options = locals_dict['options']
        handle_reply = options.get('handle_reply') or None
        if handle_reply:
            handle_reply = simplejson.dumps(handle_reply)

        res = {
            'photos': [],
            'editMessageText': options.get('editMessageText'),
            'handle_reply_dump': handle_reply,
            'reply_keyboard': False,
            'context_dump': simplejson.dumps(locals_dict.get('context', {})),
            'html': html.decode('utf-8')
        }
        reply_markup = options.get('reply_markup')
        if reply_markup and not len(reply_markup.keyboard):
            # remove reply_markup if it doesn't have buttons
            reply_markup = None
        if reply_markup:
            res['markup'] = _convert_markup(reply_markup)
            if isinstance(reply_markup, types.ReplyKeyboardMarkup):
                res['reply_keyboard'] = True
            elif isinstance(reply_markup, types.InlineKeyboardMarkup):
                res['inline_keyboard'] = True
        res['keep_reply_keyboard'] = options.get('keep_reply_keyboard')

        for photo in options.get('photos', []):
            if photo.get('type') == 'file':
                f = photo['data']
            else:
                # type is 'base64' by default
                f = io.StringIO(base64.b64decode(photo['data']))
                f.name = photo.get('filename', 'item.png')
            res['photos'].append({'file': f})

        return res