コンテンツへスキップ
メニュー
この質問にフラグが付けられました

Hey,


In a function of my TansientModel, i am getting the attribute store_fname from a couple of attachments from ir.attachments. With this, i use shutil.copy to copy the files from the related data/filestore/ folder to a new created folder in tmp/. Afterwards i create a zipfile from all the copied attachments and delete the recently created folder + copied attachments. This works fine for now. However, i want to make the created zipfile available for download, if possible without calling any other function, just in addition to my existing code. Do i need a field.Binary for this, or is there some option to send some kind of ir.actions.act_url to download a specific file on my server?


This is how i execute my python function from a button in my views.xml:

    <record id="bmd.export_all" model="ir.actions.server">
        <field name="name">BMD Export Complete Data</field>
        <field name="model_id" ref="model_bmd_bmd"/>
        <field name="type">ir.actions.server</field>
        <field name="state">code</field>
        <field name="code">model.export_all()</field>
    </record>


I could try to add the created zipfile to ir.attachments as a new entry, create a field.Binary (i think i have to do some encoding/decoding there aswell?) or, the for me best option: i could download the file directly from a specific location of my server.

However, when i add this to the end of my function, it doesnt work, no matter what kind of url i want to call:

        return {
            'type': 'ir.actions.act_url',
            'url': 'https://odoo.com',
            #'url': zipfile_path,
            'target': 'self',
        }


アバター
破棄

Use something such as a Python controller in which you return the ZIP:

http.send_file(zip_folder_location + filename, filename=filename, as_attachment=True)

著作者

Hey yenthe, thanks for your answer! I imported the http controller like this:

from odoo import http

In my function i tried both:

zipfile_path = ''/home/odoo/tmp/bmd_export_09_27112020.zip" # this is created by my shutil.make_archive function, so it should represent the full path on the server

zipfile_path_short = os.sep + "tmp" + os.sep + "bmd_export_09_27112020.zip"

zipfile_name = "bmd_export_09_27112020.zip"

http.send_file(zipfile_path, filename=zipfile_name, as_attachment=True)

http.send_file(zipfile_path_short, filename=zipfile_name, as_attachment=True)

Executing this does not result in an error, but also does not begin my desired download. The reason for zipfile_path_short is because os.getcwd() tells me i am already in the folder /home/odoo. The zipfile name is generated by code, i just wrote it like this for simplicity.

I was considering to program something similar. Could you please be so kind to share your code?

関連投稿 返信 ビュー 活動
0
5月 22
3223
0
6月 19
3280
0
3月 15
3958
1
8月 23
7333
0
8月 24
3591