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. 推荐一些我所用的firefox 附加组件。

    firefox之所以强大,很大程度上是因为它有着超多的扩展组件,来实现许多有趣的功能.这几天把我装的firefox附加组件整理下,个人认为是一般上网常用或者可以说是必备的组件,o(∩_∩)o ,晒晒. ...

  2. R7—左右内全连接详解

    在SQL查询中,经常会用到左连接.右连接.内连接.全连接,那么在R中如何实现这些功能,今天来讲一讲! SQL回顾 原理 # 连接可分为以下几类: 内连接.(典型的连接运算,使用像   =   或   ...

  3. centos6安装elasticsearch6.0

    环境准备 1台centos6操作系统主机,关闭selinux及iptables官方下载elasticsearch6.0软件包:https://artifacts.elastic.co/...官方下载j ...

  4. 基本控件文档-UILabel属性

    CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...

  5. Go net/http获取body中json格式数据

    Go net/http获取body中json格式数据 package main import ( "encoding/json" "fmt" "io/ ...

  6. 转载-SVN常用命令

    SVN(Subversion)是一个自由.开源的项目源代码版本控制工具.目前,绝大多数开源软件和企业代码管理,都使用SVN作为代码版本管理软件. Subversion将文件存放在中心版本库里,这个版本 ...

  7. Codeforces Round #505

    Codeforces Round #505 A. Doggo Recoloring 题目描述:给定一个字符串,每次选择一个在字符串里面出现至少两次的字符,然后将这种字符变成那一种指定的字符,问最终这个 ...

  8. TCP/IP详解(整理)

    1.概述 路由器是在网络层进行联通,而网桥是在链路层联通不同的网络. IP层用ICMP来与其他主机或路由器交换错误报文和其他的重要信息.应用程序也可以访问ICMP,两个诊断工具:Ping和Tracer ...

  9. Python构造方法、析构方法和单例模式

    一.__init__()方法 __init__()通常在初始化一个类实例的时候调用,如: class Student(object): def __init__(self,name,age): sel ...

  10. tmux 使用

    tmux命令参数 tmux new -s name //创建一个新会话 tmux ls //列出所有会话 tmux a -t name //返回某一个会话 tmux内部命令(ctrl+b之后按) s ...