Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tko_base_download_multiple_attachments/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# Thinkopen - Portugal & Brasil
# Copyright (C) Thinkopen Solutions (<http://www.thinkopensolutions.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import wizard
47 changes: 47 additions & 0 deletions tko_base_download_multiple_attachments/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# Thinkopen - Portugal & Brasil
# Copyright (C) Thinkopen Solutions (<http://www.thinkopensolutions.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

{
'name': 'Download all Attachments',
'version': '0.009.1',
'category': 'Email',
'sequence': 38,
'complexity': 'normal',
'description': ''' This module all attchments in tar file.''',
'author': 'ThinkOpen Solutions',
'website': 'http://www.thinkopensolution.com',
'images': ['images/oerp61.jpeg', ],
'depends': [
'mail',
],
'init_xml': [],
'data': [
'wizard/attachments_wizard_view.xml',
],
'demo_xml': [],
'installable': True,
'application': False,
'certificate': '',
'auto_install': False,
}
1 change: 1 addition & 0 deletions tko_base_download_multiple_attachments/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import attachments_wizard_view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import ntpath
import os
import shutil
import tarfile
from random import randint

from openerp.exceptions import ValidationError
from openerp import api, fields, models, _
import logging
_logger = logging.getLogger(__name__)

class Attachments(models.Model):
_inherit = "ir.attachment"

active = fields.Boolean('Active?', default=True)


class download_attachments(models.TransientModel):
_name = 'download.attachments'

attachment = fields.Binary('Attachement', readonly=True)
filename = fields.Char('File Name')
active_model = fields.Char('Active Model')
active_id = fields.Char('Active ID')
attachment_ids = fields.Many2many('ir.attachment', string='Attachments')

@api.model
def default_get(self, fields):
active_model = self._context.get('active_model')
active_id = self._context.get('active_id')
if active_id and active_model:
try:
fname = self.env[active_model].browse(active_id).name
except ValueError:
pass
if not fname:
fname = 'odoo'
res = super(download_attachments, self).default_get(fields)
return res

@api.multi
def download_attachments(self):
config_obj = self.env['ir.config_parameter']
attachment_obj = self.env['ir.attachment']

active_ids = self._context.get('active_ids')
attachment_ids = active_ids
ids = self._ids
filestore_path = os.path.join(attachment_obj._filestore(), '')
attachment_dir = filestore_path + 'attachments'
# create directory and remove its content
if not os.path.exists(attachment_dir):
os.makedirs(attachment_dir)
else:
shutil.rmtree(attachment_dir)
os.makedirs(attachment_dir)
file_name = 'attachments'
if isinstance(ids, int):
ids = [ids]
wzrd_obj = self
config_ids = config_obj.search([('key', '=', 'web.base.url')])
self.env['ir.attachment'].search([('active','=',False)]).unlink()


if len(config_ids):
value = config_ids[0].value
active_model = 'ir.attachment' # wzrd_obj.active_model
active_id = wzrd_obj.id
# tar_dir = attachment_dir + '/' + file_name
tar_dir = os.path.join(attachment_dir, file_name)
tFile = tarfile.open(tar_dir, 'w:gz')
if value and active_id and active_model:
# change working directory otherwise file is tared with all its parent directories
original_dir = os.getcwd()
filter_attachments = []
for attach in attachment_obj.browse(attachment_ids):
if attach.active:
filter_attachments.append(attach.id)
if not filter_attachments:
raise ValidationError(_("No attachment to download"))
for attachment in attachment_obj.browse(filter_attachments):
# to get full path of file
full_path = attachment_obj._full_path(attachment.store_fname)
attachment_name = attachment.datas_fname
new_file = os.path.join(attachment_dir, attachment_name)
# copying in a new directory with a new name
# shutil.copyfile(full_path, new_file)
try:
shutil.copy2(full_path, new_file)
except:
pass
#raise UserError(_("Not Proper file name to download"))
head, tail = ntpath.split(new_file)
# change working directory otherwise it tars all parent directory
os.chdir(head)
try:
tFile.add(tail)
except:
_logger.error("No such file was found : %s" %tail)
tFile.close()
os.chdir(original_dir)
values = {
'name': file_name + '.tar.gz',
'datas_fname': file_name + '.tar.gz',
'res_model': 'download.attachments',
'res_id': ids[0],
'res_name': 'test....',
'type': 'binary',
'store_fname': 'attachments/attachments',
'active' : False,
}
attachment_id = self.env['ir.attachment'].create(values)
url = "%s/web/binary/saveas?model=ir.attachment&field=datas&filename_field=name&id=%s" % (value, attachment_id.id)
return {
'type': 'ir.actions.act_url',
'url': url,
'nodestroy': False,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="download_attachments_wizard_form">
<field name="name">Download Attachments</field>
<field name="model">download.attachments</field>
<field name="arch" type="xml">
<form>
<p>Cliquer sur Télécharger pour télécharger les documents sélectionnés</p>
<group invisible="1">
<field name="attachment" filename="filess.tar" invisible="1"/>
<field name="active_model" invisible="1"/>
<field name="active_id" invisible="1"/>
</group>
<field name="attachment_ids" invisible="1"/>
<footer>
<button string="Télécharger" type="object" name="download_attachments" class="oe_highlight"/>
or
<button string="Annuler" special="cancel" class="oe_link"/>
</footer>
</form>


</field>
</record>

<record model="ir.actions.act_window" id="download_attachments_wizard_actoin">
<field name="name">Télécharger les documents</field>
<field name="res_model">download.attachments</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

<act_window
id="action_download_wizard1"
multi="True"
key2="client_action_multi" name="Download"
res_model="download.attachments" src_model="ir.attachment"
view_mode="form" target="new" view_type="form"
view_id="download_attachments_wizard_form"
/>
</data>
</openerp>