odoo打包下载
view 视图中下载按钮的编辑
<record id="action_download_zip" model="ir.actions.server">
<field name="name">附件打包下载</field>
<field name="model_id" ref="model_activity_event"/>
<field name="binding_model_id" ref="model_activity_event"/>
<field name="state">code</field>
<field name="code">
if records:
action = {
'name': 'Visit Webpage',
'type': 'ir.actions.act_url',
'url': '/document/zip/'+str(records.download_zip()),
'target': 'self',
}
</field>
</record>
model 中获取需要下载的文件的ID,并返回字符串集合
# 打包下载的方法
@api.multi
def download_zip(self):
dones = self.env['ir.attachment'].search([('res_model', '=', 'activity.event'), ('res_id', '=', self.id)])
file_ids = ''
for x in dones:
file_ids = file_ids + str(x.id) + ','
print(file_ids)
return {
file_ids
}
controller.py中的打包下载引用和方法,如下代码
# -*- coding: utf-8 -*-
import base64
import io
import jinja2
import json
import logging
import os
import sys
import zipfile
import time
import werkzeug
import werkzeug.exceptions
import werkzeug.utils
import werkzeug.wrappers
import werkzeug.wsgi
from odoo import http
from odoo.http import content_disposition, dispatch_rpc, request, \
serialize_exception as _serialize_exception, Response
from odoo.exceptions import AccessError, UserError, AccessDenied
from odoo.models import check_method_name
from odoo.service import db, security
_logger = logging.getLogger(__name__)
if hasattr(sys, 'frozen'):
# When running on compiled windows binary, we don't have access to package loader.
path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'views'))
loader = jinja2.FileSystemLoader(path)
else:
loader = jinja2.PackageLoader('odoo.addons.web', "views")
env = jinja2.Environment(loader=loader, autoescape=True)
env.filters["json"] = json.dumps
# 1 week cache for asset bundles as advised by Google Page Speed
BUNDLE_MAXAGE = 60 * 60 * 24 * 7
DBNAME_PATTERN = '^[a-zA-Z0-9][a-zA-Z0-9_.-]+$'
#----------------------------------------------------------
# Odoo Web helpers
#----------------------------------------------------------
db_list = http.db_list
db_monodb = http.db_monodb
class DownloadAll(http.Controller):
def _get_file_response(self, id, filename=None, field='datas', share_id=None, share_token=None):
"""
returns the http response to download one file.
"""
status, headers, content = request.registry['ir.http'].binary_content(
id=id, field=field, filename=filename, related_id=share_id,
access_token=share_token, access_mode='documents_share', download=True)
if status == 304:
response = werkzeug.wrappers.Response(status=status, headers=headers)
elif status == 301:
return werkzeug.utils.redirect(content, code=301)
elif status != 200:
response = request.not_found()
else:
content_base64 = base64.b64decode(content)
headers.append(('Content-Length', len(content_base64)))
response = request.make_response(content_base64, headers)
return response
def _make_zip(self, name, attachments):
"""returns zip files for the Document Inspector and the portal.
:param name: the name to give to the zip file.
:param attachments: files (ir.attachment) to be zipped.
:return: a http response to download a zip file.
"""
stream = io.BytesIO()
try:
with zipfile.ZipFile(stream, 'w') as doc_zip:
for attachment in attachments:
if attachment.type in ['url', 'empty']:
continue
filename = attachment.datas_fname
doc_zip.writestr(filename, base64.b64decode(attachment['datas']),
compress_type=zipfile.ZIP_DEFLATED)
except zipfile.BadZipfile:
_logger.exception("BadZipfile exception")
content = stream.getvalue()
headers = [
('Content-Type', 'zip'),
('X-Content-Type-Options', 'nosniff'),
('Content-Length', len(content)),
('Content-Disposition', content_disposition(name))
]
return request.make_response(content, headers)
@http.route(['/document/zip/<string:file_ids>'], type='http', auth="public")
def _get_zip(self, file_ids=None, *args, **kwargs):
"""route to get the zip file of the selection in the document's Kanban view (Document inspector).
:param file_ids: if of the files to zip.
:param zip_name: name of the zip file.
"""
file_ids = file_ids[2:-3]
print(file_ids)
timestamp = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
zip_name = 'activity-' + timestamp + '.zip'
ids_list = [int(x) for x in file_ids.split(',')]
print(ids_list)
env = request.env
return self._make_zip(zip_name, env['ir.attachment'].browse(ids_list))
odoo打包下载的更多相关文章
- ASP.NET五步打包下载Zip文件
本文版权归博客园和作者吴双共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 首先分享几个振奋人心的新闻: 1.谷歌已经宣布加入.NET基金会 2.微软加入Linux基金会, ...
- 射手网字幕打包下载(73.16G)
射手网陪着我度过15年了. 我所希望射手网所具有的价值,就是能令更多人跨越国家的樊篱,了解世界上不同的文化. 如果这个网站有帮到人,我就已经很满足了. 但是,需要射手网的时代已经走开了. 因此,今天, ...
- 2014年最新720多套Android源码2.0GB免费一次性打包下载
之前发过一个帖子,但是那个帖子有点问题我就重新发一个吧,下面的源码是我从今年3月份开始不断整理源码区和其他网站上的android源码,目前总共有720套左右,根据实现的功能被我分成了100多个类,总共 ...
- ASP.NET 打包下载文件
使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...
- C#.NET快速开发框架-企业版V4.0截图打包下载
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) http://www.csframework.com/cs-framework-4.0.htm 其它图片打包下载: ht ...
- ASP.NET多文件批量打包下载
在对多文件打包中用到了 DotNetZip 的方法来实现对多文件压缩打包.需要到http://dotnetzip.codeplex.com/处下载该文件,然后引用即可. Default.aspx: & ...
- 开源 & 免费使用 & 打包下载自行部署 :升讯威 周报系统
这个周报系统大约写于2015年,缘起当时所带的开发团队需要逐步建立或完善一些项目管理方法. 在调研了网上的诸多项目管理或周报/日报管理系统之后,并没有找到符合当时情况的系统,这里最大的问题不是网上既有 ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...
- PHP实现zip压缩打包下载
先来看PHP实现文件及文件夹的zip压缩 这里使用PHP扩展的ZipArchive类,在使用之前要将php.ini文件中的zlib.output_compression设置为On 代码如下: publ ...
随机推荐
- Python核心编程——Chapter9
好久没写过Python了,前一阵子忙这忙那的,都几乎把Python给丢掉了,话不多说,马上开始. 9.1.文件过滤.显示一个文件的所有行,并且忽略以井号开头的行. 其实这个题目比较基础,用shell语 ...
- 【leetcode 简单】 第一百零七题 回旋镖的数量
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的数量.你可以假设 n ...
- Unity3D 程序打包报错(程序是连接数据库进行处理的)
打包这个Unity3D的程序时出现错误(程序是由XML数据改成连接数据库): ArgumentException: The Assembly System.Configuration is refer ...
- 【译】第八篇 SQL Server代理使用外部程序
本篇文章是SQL Server代理系列的第八篇,详细内容请参考原文 在这一系列的上一篇,学习了如何用SQL Server代理作业活动监视器监控作业活动和查看作业历史记录.在实时监控和管理SQL Ser ...
- 浏览器断点调试js
说了一些 Chrome 开发者工具的技巧,其实并没有涉及到开发者工具最核心的功能之一:断点调试.断点可以让程序运行到某一行的时候,把程序的整个运行状态进行冻结.你可以清晰地看到到这一行的所有的作用域变 ...
- [转]大整数算法[11] Karatsuba乘法
★ 引子 前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) ...
- 【bzoj题解】1012 最大数
题目描述 现在请求你维护一个数列,要求提供以下两种操作:1.查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2.插入操作.语法:A ...
- thinkphp报错Call to undefined method app\index\controller\Index::fetch()
因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...
- Interval Minimum Number
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- MySQL分布式集群之MyCAT(二)【转】
在第一部分,有简单的介绍MyCAT的搭建和配置文件的基本情况,这一篇详细介绍schema的一些具体参数,以及实际作用 首先贴上自己测试用的schema文件,双引号之前的反斜杠不会消除,姑 ...