This question has been flagged
7 Replies
5532 Views

when clicking on button in tree view i am getting blank. please somebody help me.


<record model = "ir.ui.view" id = "view_order_form_dbt_task" > 
<field
name = "name" > sale.order.inherit </field>
<field
name = "model" > sale.order </field>
< field
name = "inherit_id" ref = "sale.view_order_form" />
<field
name = "arch" type = "xml" >
<field
name = "payment_term_id"position = "after" >
<field
name ="attachment_ids" />
</field>
</field>
</record>
<record
id = "view_order_tree_dbt_task" model = "ir.ui.view" >
<field
name = "name" > sale.order.tree </field>
<field
name = "model" > sale.order </field>
<field
name = "inherit_id" ref = "sale.view_order_tree" />
<field
name = "arch" type = "xml" >
<field
name = "invoice_status " position = " after " >
<button
name = "action_attachment_down" type = "object" string = "Download" />
</field>
</field>
</record>


in python file
class dbt_task (models.Model): 
    _inherit = 'sale.order'
    attachment_ids = fields.Binary ( 'Attachment' , attachment = True )

    @ api.multi
    def action_attachment_down ( self ):
        for rec in self :
        with io.BytesIO (base64 .b64decode (rec.attachment_ids)) as output:
            result = output.read ()
            base_url = self .env [ 'ir.config_parameter' ] .get_param ( 'web.base.url' )
            attachment_obj = self .env [ 'ir.attachment' ]
            attachment_id = attachment_obj.sudo (). create ({ 'name' : "name" , 'datas_fname' : 'name.file_ext' , 'datas' : result})
            download_url = '/ web / content /' + str (attachment_id.id) + '? download = true '
            return {
            'name' : 'Report' ,
        'type': 'ir.actions.act_url',
         "url": str(base_url) + str(download_url),
        'target': 'new',
            }
    
Avatar
Discard
Best Answer

Here you are doing .b64decode(rec.attachment_ids) and attachments_ids will be a pool of records but you need a Base64 encoded bytes-like object or ASCII string.

for eg: I am doing like this

content = base64.b64decode(attachment.datas)
headers.append(('Content-Length', len(content)))
response = request.make_response(content, headers)
return response
Avatar
Discard
Author

thanks for your response

using below code i am getting only last attachment of my model how do i get all attachments in single pdf. note(all attachments here in pdf format)

my code:

def action_attachment_down(self):

sale_orders = self.env['sale.order'].search([])

result = b' '

for rec in sale_orders:

if rec.attachment_ids:

result.write(rec.attachment_ids)

_logger = logging.info("record id Here.........................")

_logger = logging.info(rec.id)

base_url = self.env['ir.config_parameter'].get_param('web.base.url')

attachment_obj = self.env['ir.attachment']

attachment_id = attachment_obj.sudo().create(

{'name': "name", 'datas_fname': 'sale_attachments.pdf', 'datas': result})

download_url = '/web/content/' + str(attachment_id.id) + '?download=true'

return {

'name': 'Report',

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

'url': str(base_url) + str(download_url),

'target': 'new',

}

Great, please check the http route defined for making this download response in class binary, that is web, controllers, main.py, class binary. 
You will get more detailed view here

On Wed, 12 Aug 2020, 4:52 pm paidy kumar, <mpaidykumar@gmail.com> wrote:

thanks for your response

using below code i am getting only last attachment of my model how do i get all attachments in single pdf. note(all attachments here in pdf format)

my code:

def action_attachment_down(self):

sale_orders = self.env['sale.order'].search([])

result = b' '

for rec in sale_orders:

if rec.attachment_ids:

result.write(rec.attachment_ids)

_logger = logging.info("record id Here.........................")

_logger = logging.info(rec.id)

base_url = self.env['ir.config_parameter'].get_param('web.base.url')

attachment_obj = self.env['ir.attachment']

attachment_id = attachment_obj.sudo().create(

{'name': "name", 'datas_fname': 'sale_attachments.pdf', 'datas': result})

download_url = '/web/content/' + str(attachment_id.id) + '?download=true'

return {

'name': 'Report',

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

'url': str(base_url) + str(download_url),

'target': 'new',

}

Envoyé par Odoo S.A. utilisant Odoo.

Author

for this i need to merge all attachments in single byte array.

but i am not able to do this do you have any idea to do this

Sorry, I can't check right now, but you can do one thing, zip all attachments and pass zip file in response. I will test tomorrow for other methods and will give you the feedback.

Author

Thanks hilar thankyou so much.