Skip to Content
Menu
This question has been flagged
1 Reply
7312 Views

hello guys, i want to know is there a way to download attachment in record model from other applications outside odoo? im aware i have to use controller and im familiar on it, but i didn't know how to download attachments automatically from the controller thanks

Avatar
Discard
Best Answer

Hi,

If the other application is running on Python, you can download the report from the other application using the odoorpc package. See the documentation here: https://pythonhosted.org/OdooRPC/tuto_report.html


Read:

Download reports

Another nice feature is the reports generation with the report property. The list method allows you to list all reports available on your Odoo server (classified by models), while the download method will retrieve a report as a file (in PDF, HTML... depending of the report).

To list available reports:

>>> odoo.report.list()
{u'account.invoice': [{u'name': u'Duplicates', u'report_type': u'qweb-pdf', u'report_name': u'account.account_invoice_report_duplicate_main'}, {u'name': u'Invoices', u'report_type': u'qweb-pdf', u'report_name': u'account.report_invoice'}], u'res.partner': [{u'name': u'Aged Partner Balance', u'report_type': u'qweb-pdf', u'report_name': u'account.report_agedpartnerbalance'}, {u'name': u'Due Payments', u'report_type': u'qweb-pdf', u'report_name': u'account.report_overdue'}], ...}

To download a report:

>>> report = odoo.report.download('account.report_invoice', [1])

The method will return a file-like object, you will have to read its content in order to save it on your file-system:

>>> with open('invoice.pdf', 'w') as report_file:
...     report_file.write(report.read())
...


Thanks

Avatar
Discard
Author

i've found the answer

import logging

try:

from BytesIO import BytesIO

except ImportError:

from io import BytesIO

import zipfile

from datetime import datetime

from odoo import http

from odoo.http import request

from odoo.http import content_disposition

import ast

import json

_logger = logging.getLogger(__name__)

class Binary(http.Controller):

@http.route('/web/aflowz_attachments/download_all_document/<model_name>/<int:res_id>', type='http', auth="public")

def download_document(self, model_name=None, res_id=0, **kw):

attachment_ids = request.env['ir.attachment'].search([('res_model', '=', model_name), ('res_id', '=', res_id)])

file_dict = {}

if attachment_ids:

for attachment_id in attachment_ids:

file_store = attachment_id.store_fname

if file_store:

file_name = attachment_id.name

file_path = attachment_id._full_path(file_store)

file_dict["%s:%s" % (file_store, file_name)] = dict(path=file_path, name=file_name)

zip_filename = datetime.now()

zip_filename = "%s.zip" % zip_filename

bitIO = BytesIO()

zip_file = zipfile.ZipFile(bitIO, "w", zipfile.ZIP_DEFLATED)

for file_info in file_dict.values():

zip_file.write(file_info["path"], file_info["name"])

zip_file.close()

return request.make_response(bitIO.getvalue(),

headers=[('Content-Type', 'application/x-zip-compressed'),

('Content-Disposition', content_disposition(zip_filename))])

else:

return request.make_response(json.dumps({

"error": "Attachments not found",

"message": "There are no attachment",

"code": 404}),

headers={'Content-Type': 'application/json'}

)

Related Posts Replies Views Activity
11
Apr 23
26786
3
Jul 21
19411
1
Apr 20
5509
5
Feb 24
46368
0
Oct 17
3585