This question has been flagged
1 Reply
5025 Views

I created a custom module with the following code, everything works except for the fact that I do not want the report to be downloaded by the client when I run the function. How do I stop a report from being downloaded automatically?

def cover_print(self, cr, uid, ids, context=None):
    for numbers in self.browse(cr, uid, ids, context=context):
         record = numbers
    ir_actions_report = self.pool.get('ir.actions.report.xml')
    matching_reports = ir_actions_report.search(cr, uid, [('id','=',record.report_id.id)])
    if matching_reports:
        report = ir_actions_report.browse(cr, uid, matching_reports[0])
        report_service = 'report.' + report.report_name
        service = netsvc.LocalService(report_service)
        (result, format) = service.create(cr, uid, [record.id], {'model': self._name}, context=context)
        eval_context = {'time': time, 'object': record}
        if not report.attachment or not eval(report.attachment, eval_context):
            # no auto-saving of report as attachment, need to do it manually
            result = base64.b64encode(result)
            file_name = record.name_get()[0][1]
            file_name = re.sub(r'[^a-zA-Z0-9_-]', '_', file_name)
            file_name += ".pdf"
            self.pool.get('ir.attachment').create(cr, uid,
                                                  {
                                                   'name': file_name,
                                                   'datas': result,
                                                   'datas_fname': file_name,
                                                   'res_model': self._name,
                                                   'res_id': record.id,
                                                   'type': 'binary'
                                                  },
                                                  context=context)
    return True
Avatar
Discard
Author Best Answer

I found that if i chage the code to be:

result = self.pool.get('ir.attachment').create(cr, uid,
                                                  {
                                                   'name': file_name,
                                                   'datas': result,
                                                   'datas_fname': file_name,
                                                   'res_model': self._name,
                                                   'res_id': record.id,
                                                   'type': 'binary'
                                                  },
                                                  context=context)

Then the report is not automatically downloaded, which is fine for my customer modules but does not really help me for base/ addon modules. Over writing those would make updates over complicated.

Is there a better solution?

Avatar
Discard