Skip to Content
Menu
This question has been flagged
3 Replies
11627 Views

While select multiple records in tree view and click button, need to download respective attachments simultaneously. I'm working in Odoo10.0

def generate_renewal_pdf(self):
 """ Print renewal pdf"""
    attachment_ids = {}
     for mail in self:
         attachments = mail.attachment_ids
         for attach in attachments:
               attachment_ids[attach.id] = str(attach.datas_fname)
              download = True
              return {
                   'type': 'ir.actions.act_url',
                    'url': '/web/content/%s/%s/%s' % (attach.id,   # How to pass multiple id here to download multiple
                                                                                attach.datas_fname, 
                                                                                download), 
                     'target': 'self', 
                   }

Anyone have solution?

Avatar
Discard
Best Answer

Hello,  Girija Subramaniyan,

What's inside the method: 'download_attachments' where you pass multiples id's from files??.

It works for me if i have a single record, but i have multiples files, so i need the answer please

Thanks!

Avatar
Discard
Author

@api.multi
def download_attachments(self, attachment_ids):
""" Create attachment renewal tar file
@params: attachemnt_ids
"""
config_obj = self.env['ir.config_parameter']
attachment_obj = self.env['ir.attachment']

attachment_ids = attachment_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 = 'Renewal'
if isinstance(ids, int):
ids = [ids]

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'
tar_dir = os.path.join(attachment_dir, file_name)
#tFile = tarfile.open(tar_dir, 'w:gz')
with ZipFile(tar_dir,'w') as zip:

if value 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 UserError(_("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)
mail_id = self.env['mail.mail'].sudo().search(
[('attachment_ids', '=', attachment.id)])
partner_name = mail_id.customer_id.name
if partner_name:
partner_name = partner_name.replace('/','')
else:
partner_name = str(random.randint(1, 999999))
attachment_name = str(attachment.datas_fname)
new_file = os.path.join(attachment_dir,
partner_name+"-"+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:
zip.write(tail)
except:
_logger.error("No such file was found : %s" %tail)
# tFile.close()
os.chdir(original_dir)
values = {
'name': file_name + '.zip',
'datas_fname': file_name + '.zip',
'res_model': 'event.registration',
# 'res_id': ids[0],
'res_name': 'Renewal',
'type': 'binary',
'store_fname': 'attachments/Renewal',
'active': False,
}
# Create selected all attachment with tar file
attachment_id = self.env['ir.attachment'].create(values)
url = "%s/web/content/%s?download=true" % (value,
attachment_id.id)
return {
'type': 'ir.actions.act_url',
'url': url,
'nodestroy': False,
}

Author

In below code i have create zip file and updated attachements in that

Best Answer

Hello Girija,
Please try like this, i think it's helpful for you.
Ex.
def generate_renewal_pdf(self):
 """ Print renewal pdf"""
    attachment_ids = []
     for mail in self:
         attachments = mail.attachment_ids
         for attach in attachments:
               attachment_ids.append(attach.id)
              url = '/web/content/download_document?tab_id=%s' % attachment_ids

              return {
                   'type': 'ir.actions.act_url',
                    'url': url
                     'target': 'self', 
                   }


And please check https://apps.odoo.com/apps/modules/10.0/download_multiple_attachments/

Avatar
Discard
Best Answer

Hi Girija can you help me to do this .i am also struggling to do this

if you completed this please tell me.

Thanks in advance

Avatar
Discard
Author

Hi Paidy Kumar.

i did like this, worked for me

def generate_renewal_pdf(self):

""" Print renewal pdf"""

attachment_ids = []

for mail in self:

attachments = mail.attachment_ids

for attach in attachments:

attachment_ids.append(attach.id)

if (len(attachment_ids) > 1):

# To download multiple records attachments

download = self.download_attachments(attachment_ids)

else:

# download single record

config_obj = self.env['ir.config_parameter']

config_ids = config_obj.search([('key', '=', 'web.base.url')])

if len(config_ids):

value = config_ids[0].value

url = "%s/web/content/%s?download=true" % (value,

attachment_ids[0])

download = {

'type': 'ir.actions.act_url',

'url': url,

'target': 'self',

}

return download

Related Posts Replies Views Activity
0
Jun 19
2334
1
Nov 24
18020
1
Sep 23
1206
3
May 23
4095
5
May 23
13465