Skip to Content
Menu
This question has been flagged
2 Replies
6541 Views

Hi All,

I am trying to generate excel report using Odoo 10. The code is working fine and I am able to do that. But the file does'nt download on a single click. It saves the file and shows download link on wizard. But I dont want this extra step. I want the file to be downloaded in single click. I am sharing my working code below here. Please have a look and suggest me what should be added to make it work in a single click.

xml  code:

                    <div state="get">

                        <group>

                            <field name="name" colspan="4" invisible="1"/>

                            <field name="report" filename="name" colspan="4"/>

                        </group>

                    </div>

                    <button name="generate_xls_report" string="Export XLS" type="object" class="oe_highlight" />

    <record id="action_cases_wizard" model="ir.actions.act_window">
        <field name="name">Event Details Report</field>
        <field name="res_model">cases.wizard</field>
        <field name="type">ir.actions.act_window</field>
        <field name="view_type">form</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="cases_report_view"/>
        <field name="target">new</field>
    </record>

Python Code:
from odoo import fields, models, api, _

from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import xlwt
import base64
import cStringIO
from datetime import datetime


class CasesWizard(models.TransientModel):
    _name = "cases.wizard"
    _description = "Cases wizard"
    
    case_id = fields.Many2one('project.project', string='Cases')
    event_id = fields.Many2one('calendar.event', string='Events')
    company_id = fields.Many2one('res.company', string='company id', readonly=True,default=lambda self: self.env.user.company_id.id)
    lawyer_id = fields.Many2one('res.users', string='Lawyers')
    #partner_id = fields.Many2one('res.partner', string='Clients')
    date_from = fields.Date(string='Start Date')
    date_to = fields.Date(string='End Date')
    
    
    state = fields.Selection([('choose', 'choose'), ('get', 'get')],default='choose')
    report = fields.Binary('Prepared file', filters='.xls', readonly=True)
    name =  fields.Char('File Name', size=32)
    @api.multi
    def generate_xls_report(self):

        self.ensure_one()

        wb1 = xlwt.Workbook(encoding='utf-8')
        ws1 = wb1.add_sheet('Case Event Details')
        fp = cStringIO.StringIO()


# Here all excel data and calculations


        wb1.save(fp)
        out = base64.encodestring(fp.getvalue())
        self.write({'state': 'get', 'report': out, 'name':'event_details.xls'})
        
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'cases.wizard',
            'view_mode': 'form',
            'view_type': 'form',
            'res_id': self.id,
            'views': [(False, 'form')],
            'target': 'new',
            'name': 'Event Details Report'
        }




Avatar
Discard
Author Best Answer

Dear Hilar if I dont return the action then the wizard gets dismiss and the file also doesnt download. I just tried ur suggestion.

Avatar
Discard
Best Answer

Why are you returning the same action in xls after download? I think that is the problem

Avatar
Discard
Related Posts Replies Views Activity
2
May 16
4283
0
Dec 22
3503
1
Jan 20
8438
0
Sep 24
210
0
Jul 18
1817