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打包下载的更多相关文章

  1. ASP.NET五步打包下载Zip文件

    本文版权归博客园和作者吴双共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 首先分享几个振奋人心的新闻: 1.谷歌已经宣布加入.NET基金会 2.微软加入Linux基金会, ...

  2. 射手网字幕打包下载(73.16G)

    射手网陪着我度过15年了. 我所希望射手网所具有的价值,就是能令更多人跨越国家的樊篱,了解世界上不同的文化. 如果这个网站有帮到人,我就已经很满足了. 但是,需要射手网的时代已经走开了. 因此,今天, ...

  3. 2014年最新720多套Android源码2.0GB免费一次性打包下载

    之前发过一个帖子,但是那个帖子有点问题我就重新发一个吧,下面的源码是我从今年3月份开始不断整理源码区和其他网站上的android源码,目前总共有720套左右,根据实现的功能被我分成了100多个类,总共 ...

  4. ASP.NET 打包下载文件

    使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...

  5. C#.NET快速开发框架-企业版V4.0截图打包下载

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) http://www.csframework.com/cs-framework-4.0.htm 其它图片打包下载: ht ...

  6. ASP.NET多文件批量打包下载

    在对多文件打包中用到了 DotNetZip 的方法来实现对多文件压缩打包.需要到http://dotnetzip.codeplex.com/处下载该文件,然后引用即可. Default.aspx: & ...

  7. 开源 & 免费使用 & 打包下载自行部署 :升讯威 周报系统

    这个周报系统大约写于2015年,缘起当时所带的开发团队需要逐步建立或完善一些项目管理方法. 在调研了网上的诸多项目管理或周报/日报管理系统之后,并没有找到符合当时情况的系统,这里最大的问题不是网上既有 ...

  8. java 实现多文件打包下载

    jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...

  9. PHP实现zip压缩打包下载

    先来看PHP实现文件及文件夹的zip压缩 这里使用PHP扩展的ZipArchive类,在使用之前要将php.ini文件中的zlib.output_compression设置为On 代码如下: publ ...

随机推荐

  1. [转载]Javascript 同步异步加载详解

    http://handyxuefeng.blog.163.com/blog/static/4545217220131125022640/ 本文总结一下浏览器在 javascript 的加载方式. 关键 ...

  2. 当python模式遇见cedet

    TAG: emacs, python, cedet, semantic, ctags DATE: 2013-08-20 我用Emacs 24写python程序. 发现屏幕不时有些闪动,MiniBuff ...

  3. 2016-2017-20155329 《Java程序设计》第5周学习总结

    学号 2016-2017-20155329 <Java程序设计>第5周学习总结 教材学习内容总结 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生时显示友好的错误 ...

  4. kafka入门(3)- SpringBoot集成Kafka

    1.引入依赖 <dependency> <groupId>org.springframework.kafka</groupId> <artifactId> ...

  5. sql 存储过程导出指定数据到.txt文件(定时)

    需求:每天生成一份txt文件数据,供第三方通过http方式调用 方法: 1.新建存储过程: USE [LocojoyMicroMessage] GO /****** Object: StoredPro ...

  6. 阿里云CentOS下安装jdk

    首先需要下载jdk: 由于oracle上的下载页面有跳转,直接用wget下载下来的只是html页面.可以用下面的命令: wget --no-cookies --no-check-certificate ...

  7. 【多视图几何】TUM 课程 第4章 同名点匹配

    课程的 YouTube 地址为:https://www.youtube.com/playlist?list=PLTBdjV_4f-EJn6udZ34tht9EVIW7lbeo4 .视频评论区可以找到课 ...

  8. 查看Oracle数据库中的所有用户名

    select username from dba_users"

  9. JavaScript进阶--慕课网学习笔记

                         JAVASCRIPT—进阶篇 给变量取个名字(变量命名) 变量名字可以任意取,只不过取名字要遵循一些规则: 1.必须以字母.下划线或美元符号开头,后面可以跟字 ...

  10. Git管理本地代码(一)【转】

    转自:http://blog.csdn.net/weihan1314/article/details/8677800 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   安 ...