如何在 qWeb 报告、Odoo 中设置 PDF 名称?

2023-11-25

我正在 Odoo 8 中使用 qWeb 制作报告。这些生成的 PDF 文件以“默认”名称保存。我想为每个生成的文件设置一个特定的名称(不是在保存文件之后,而是在“生成”时间)。

那可能吗?如果是的话,该怎么办呢?

提前致谢。


在 Odoo 8 中,您可以像下面一样修补 addons/report/controllers/main.py 的 report_download 方法(在 FIX START 和 END 之间)。然后,它将使用报表操作定义中附件属性的代码。由于系统始终会将文件作为附件保存在数据库中,因此您可以进一步更改行为,仅在attachment_use 设置为 True 时才保存在数据库中。这样就不需要添加额外的字段和更改视图。

@route(['/report/download'], type='http', auth="user")
def report_download(self, data, token):
    """This function is used by 'qwebactionmanager.js' in order to trigger the download of
    a pdf/controller report.

    :param data: a javascript array JSON.stringified containg report internal url ([0]) and
    type [1]
    :returns: Response with a filetoken cookie and an attachment header
    """
    requestcontent = simplejson.loads(data)
    url, type = requestcontent[0], requestcontent[1]
    try:
        if type == 'qweb-pdf':
            reportname = url.split('/report/pdf/')[1].split('?')[0]

            docids = None
            if '/' in reportname:
                reportname, docids = reportname.split('/')

            if docids:
                # Generic report:
                response = self.report_routes(reportname, docids=docids, converter='pdf')
                ##### FIX START: switch reportname with the evaluated attachment attribute of the action if available
                docids = [int(i) for i in docids.split(',')]
                report_obj = request.registry['report']
                cr, uid, context = request.cr, request.uid, request.context
                report = report_obj._get_report_from_name(cr, uid, reportname)
                if report.attachment:
                    obj = report_obj.pool[report.model].browse(cr, uid, docids[0])
                    reportname=eval(report.attachment, {'object': obj, 'time': time}).split('.pdf')[0]
                ##### FIX END    
            else:
                # Particular report:
                data = url_decode(url.split('?')[1]).items()  # decoding the args represented in JSON
                response = self.report_routes(reportname, converter='pdf', **dict(data))

            response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname)
            response.set_cookie('fileToken', token)
            return response
        elif type =='controller':
            reqheaders = Headers(request.httprequest.headers)
            response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True)
            response.set_cookie('fileToken', token)
            return response
        else:
            return
    except Exception, e:
        se = _serialize_exception(e)
        error = {
            'code': 200,
            'message': "Odoo Server Error",
            'data': se
        }
        return request.make_response(html_escape(simplejson.dumps(error))) 

您可以通过以下方式设置报告操作的附件属性:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
  <data>
    <!-- rename the file names of the standard rfq report -->
        <record id="purchase.report_purchase_quotation" model="ir.actions.report.xml">
            <field name="attachment">'RFQ_'+object.name+'.pdf'</field>
        </record>    
   </data>
</openerp> 

这是 addons/report/report.py 中 Report 对象的 _check_attachment 的补丁,用于修改 Attachment_use 行为:

@api.v7
def _check_attachment_use(self, cr, uid, ids, report):
    """ Check attachment_use field. If set to true and an existing pdf is already saved, load
    this one now. Else, mark save it.
    """
    save_in_attachment = {}
    save_in_attachment['model'] = report.model
    save_in_attachment['loaded_documents'] = {}

    if report.attachment:
        for record_id in ids:
            obj = self.pool[report.model].browse(cr, uid, record_id)
            filename = eval(report.attachment, {'object': obj, 'time': time})

            # If the user has checked 'Reload from Attachment'
            if report.attachment_use:
                alreadyindb = [('datas_fname', '=', filename),
                               ('res_model', '=', report.model),
                               ('res_id', '=', record_id)]
                attach_ids = self.pool['ir.attachment'].search(cr, uid, alreadyindb)
                if attach_ids:
                    # Add the loaded pdf in the loaded_documents list
                    pdf = self.pool['ir.attachment'].browse(cr, uid, attach_ids[0]).datas
                    pdf = base64.decodestring(pdf)
                    save_in_attachment['loaded_documents'][record_id] = pdf
                    _logger.info('The PDF document %s was loaded from the database' % filename)

                    continue  # Do not save this document as we already ignore it

            # FIX START (commenting out below lines and indenting the else clause one level down)
            # If the user has checked 'Save as Attachment Prefix'
            #~ if filename is False:
                #~ # May be false if, for instance, the 'attachment' field contains a condition
                #~ # preventing to save the file.
                #~ continue
                else:
                    save_in_attachment[record_id] = filename  # Mark current document to be saved
            # FIX END
    return save_in_attachment  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 qWeb 报告、Odoo 中设置 PDF 名称? 的相关文章

随机推荐

  • 在 Chrome 中自动打印/保存网页为 pdf - python 2.7

    我正在尝试在 Chrome 中自动打印保存网页为 pdf 我已经检查了网络浏览器模块 但它似乎并不是用于此目的 我探索了 wkhtmltopdf 作为替代方案 但下载文件时它似乎被病毒感染了 感谢你的建议 这对我在 Windows 7 x6
  • Kubernetes Pod 收到 SIGTERM 后还会收到请求吗?

    我想在 Kubernetes Pod 中实现正常关闭 我知道我需要监听 SIGTERM 它表示关闭过程的开始 但当我收到它时 我到底该怎么做呢 至少我必须等待所有正在运行的请求完成才能退出 但是pod收到SIGTERM后还能接收新的请求吗
  • 在多个 Android 设备上同步记录数据的时间

    这个问题可能有点冗长 但可以提供很多建议 问题陈述 我们有几个 API 级别 8 Android 2 2 MyTouch 设备 将用于记录游乐园 即过山车 的加速度数据 我们还有一个可视化功能 允许我们绘制和查看加速度计点作为记录时间的函数
  • 如何从地点选择器的地址获取国家、城市?

    我正在使用地点选择器的意图来获取该地点 现在我想以单独的形式保存地址 如国家 城市 密码 州 我如何从地点选择器的地址获取所有这些信息 Code public class NameOfBusinessFragment extends Fra
  • 具有映射诊断上下文的 Golang 日志记录

    我怎样才能实现MDC 日志记录 Java 在 Go 语言中 我需要在所有服务器日志中添加 UUID 以便能够跟踪并发请求 Java MDC 依赖于线程本地存储 这是 Go 所没有的 最接近的是线程aContext通过你的堆栈 这就是越来越多
  • Windows 应用程序安装程序框架

    可下载的应用程序会提供多种类型的安装程序 这对我来说总是很奇怪 例如 有时您可以选择 exe 或 msi 某些类型的安装人员是否比其他类型有优势 你选择哪一个很重要吗 作为开发人员 为什么我要向我的用户提供不同的安装程序 exe 和 msi
  • 如何使用 C# 在单个请求中从 Azure Blob 存储下载多个文件?

    我需要从 Azure Blob 存储下载 1000 个小图像 我不想为每个文件提出单独的请求 在 C 中如何做到这一点 现在我正在使用Azure Storage Blobs and Azure Storage Blobs Batch但他们都
  • LDAP协议是否限制DN的长度

    LDAP 协议是否指定 DN 可以采用的最大长度 我已经看过了https www rfc editor org rfc rfc4514但我找不到它施加的任何限制 大多数 LDAP DN 的实现通常达到 256 个字符 我认为这仍然来自 X
  • 如何创建一个可以带参数或不带参数使用的装饰器?

    我想创建一个可以与参数一起使用的 Python 装饰器 redirect output somewhere log def foo 或没有它们 例如默认将输出重定向到 stderr redirect output def foo 这有可能吗
  • css 不透明度在 IE7 中不起作用

    我有这个测试页面 http jsfiddle net VWnm9 7 在我所有运行 IE7 或 IE8 的计算机上 图像都正确褪色 除了一台运行 IE7 的计算机 并且即使在 noext 模式下 花朵也不会褪色 该页面是
  • NodeJS:处理 TCP 套接字流的正确方法是什么?我应该使用哪个分隔符?

    据我了解here V8 有一个分代垃圾收集器 随机移动对象 节点无法获取指向原始字符串数据的指针以写入套接字 所以我不应该将来自 TCP 流的数据存储在字符串中 特别是如果该字符串变得大于Math pow 2 16 字节 希望到目前为止我都
  • NSUserDefaults 不保存

    我的精灵套件应用程序遇到问题 我的NSUserDefaults变量不起作用 在createSceneContents 我知道正在被调用 if defaults objectForKey obj difficultyLabel text Di
  • 如何在 DOM 中移动 iFrame 而不丢失其状态?

    看一下这个简单的 HTML div div div div 假设我想移动包装 以便 wrap2将在之前 wrap1 iframe 被 JavaScript 污染了 我知道 jQuery insertAfter and insertBefor
  • C# 中跨类的静态变量初始化顺序是什么?

    DependencyProperty AddOwner MSDN 页面提供了一个示例 其中两个类具有静态成员 其中一个类的成员依赖于另一个类的成员进行初始化 我认为MSDN是错误的 静态变量的初始化顺序在C 中不可靠就像 C 中一样或其他任
  • 如何在jquery中replaceWith('something')后获取对象的实际内容

    我有这个代码 document ready function selector click function obj this obj replaceWith div class size whats up man div alert ob
  • Bash 函数中 return 和 exit 的区别

    两者有什么区别return and exitBash 函数中关于退出代码的声明 From man bash on return n 导致函数停止执行并将 n 指定的值返回给其调用者 如果省略 n 则返回状态为函数体中最后执行的命令的状态 o
  • 如何使用 Angular 2 组件动态添加innerHTML

    我正在为组件库创建文档 我想要 1 个 html 字符串来生成页面上的组件及其文档 我想要的是 我拥有的 当我检查 HTML 时 my button 标签不存在 当我使用innerHTML 时 它们被删除 我的组件代码 private fl
  • 连接到远程sqlite3数据库

    我可以使用以下命令创建到本地 sqlite3 数据库的连接 使用 Mac OS X 10 5 和 Python 2 5 1 conn sqlite3 connect db MyDb 如果该数据库位于服务器上 例如 运行 Ubuntu 8 0
  • 在nodejs中解析JSON

    嗨 我有下面的 json id 12 data 123556 details name alan age 12 我用下面的代码来解析 var chunk id 12 data 123556 details name alan age 12
  • 如何在 qWeb 报告、Odoo 中设置 PDF 名称?

    我正在 Odoo 8 中使用 qWeb 制作报告 这些生成的 PDF 文件以 默认 名称保存 我想为每个生成的文件设置一个特定的名称 不是在保存文件之后 而是在 生成 时间 那可能吗 如果是的话 该怎么办呢 提前致谢 在 Odoo 8 中