To create a wizard in Odoo that enables file download and automatically closes upon completion, follow the steps below. The solution involves defining a wizard model, its corresponding view, and a server action to handle the file generation and download.
Steps to Create an Odoo Wizard for File Download and Auto Close
1. Define the Wizard Model
Create a new wizard model in a custom Odoo module. The wizard will handle the file download functionality.
from odoo import models, fields, api
import base64
class FileDownloadWizard(models.TransientModel):
_name = 'file.download.wizard'
_description = 'File Download Wizard'
file_data = fields.Binary(string='File', readonly=True)
file_name = fields.Char(string='File Name', readonly=True)
def action_download_file(self):
"""Trigger file download and auto-close the wizard."""
# Example: Generate file content dynamically
content = "This is a sample file content generated in Odoo."
file_data = base64.b64encode(content.encode('utf-8'))
# Set the file data and file name
self.file_data = file_data
self.file_name = 'sample_file.txt'
# Close the wizard by returning an action
return {
'type': 'ir.actions.act_url',
'url': f'/web/content/{self.id}/file_data/{self.file_name}?download=true',
'target': 'self',
}
2. Define the Wizard View
Create an XML file for the wizard form view.
<record id="view_file_download_wizard_form" model="ir.ui.view">
<field name="name">file.download.wizard.form</field>
<field name="model">file.download.wizard</field>
<field name="arch" type="xml">
<form string="File Download">
<group>
<label string="Click 'Download' to get the file."/>
</group>
<footer>
<button string="Download" type="object" name="action_download_file" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_file_download_wizard" model="ir.actions.act_window">
<field name="name">File Download Wizard</field>
<field name="res_model">file.download.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</odoo>
3. Link the Wizard in Your Menu or Action
Add the action to your menu or trigger it programmatically.
<menuitem id="menu_file_download_wizard" name="File Download Wizard"
action="action_file_download_wizard" parent="base.menu_custom"/>
</odoo>
How This Solution Works:
Binary Field (file_data): Used to store the base64-encoded file content for download.
Dynamic Content Generation: In the action_download_file method, the file content is dynamically generated or fetched.
Auto Close: Returning an ir.actions.act_url action automatically closes the wizard after initiating the download.
By implementing this solution, you can integrate a wizard that allows downloading files in Odoo while maintaining a seamless user experience by auto-closing the dialog.
This solution demonstrates advanced features that highlight the capabilities of Odoo development services, ensuring optimal customization and user satisfaction.
Top comments (0)