def report_routes(self, reportname, docids=None, converter=None, **data): if converter == 'xlsx': report = request.env['ir.actions.report']._get_report_from_name( reportname) context = dict(request.env.context) if docids: docids = [int(i) for i in docids.split(',')] if data.get('options'): data.update(json.loads(data.pop('options'))) if data.get('context'): # Ignore 'lang' here, because the context in data is the one # from the webclient *but* if the user explicitely wants to # change the lang, this mechanism overwrites it. data['context'] = json.loads(data['context']) if data['context'].get('lang'): del data['context']['lang'] context.update(data['context']) xlsx = report.with_context(context).render_xlsx(docids, data=data)[0] xlsxhttpheaders = [ ('Content-Type', 'application/vnd.openxmlformats-' 'officedocument.spreadsheetml.sheet'), ('Content-Length', len(xlsx)), ('Content-Disposition', content_disposition(report.report_file + '.xlsx')) ] return request.make_response(xlsx, headers=xlsxhttpheaders) return super(ReportController, self).report_routes(reportname, docids, converter, **data)
def OAS_json_spec_download(self, namespace_name, **kwargs): ensure_db() namespace = (http.request.env["openapi.namespace"].sudo().search([ ("name", "=", namespace_name) ])) if not namespace: raise werkzeug.exceptions.NotFound() if namespace.token != kwargs.get("token"): raise werkzeug.exceptions.Forbidden() response_params = {"headers": [("Content-Type", "application/json")]} if "download" in kwargs: response_params = { "headers": [ ("Content-Type", "application/octet-stream; charset=binary"), ("Content-Disposition", http.content_disposition("swagger.json")), ], "direct_passthrough": True, } return werkzeug.wrappers.Response(json.dumps( namespace.get_OAS(), default=date_utils.json_default), status=200, **response_params)
def report_routes(self, reportname, docids=None, converter=None, **data): # if it's not Aeroo Reports fall back to original reporting engine if converter != 'aeroo': return super(ReportController, self).report_routes( reportname, docids, converter, **data ) # Aeroo Reports starts here report_obj = request.env['ir.actions.report'] report = report_obj._get_report_from_name(reportname) context = dict(request.env.context) # report_data = {} if docids: docids = [int(i) for i in docids.split(',')] if data.get('options'): data.update(json.loads(data.pop('options'))) if data.get('context'): data['context'] = json.loads(data['context']) if data['context'].get('lang'): del data['context']['lang'] context.update(data['context']) rset = report.with_context(context).render_aeroo(docids, data=data) mimetype = self.MIMETYPES.get(rset[1], 'application/octet-stream') httpheaders = [ ('Content-Disposition', content_disposition(rset[2])), ('Content-Type', mimetype), ('Content-Length', len(rset[0])) ] return request.make_response(rset[0], headers=httpheaders)
def content_common(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename=None, filename_field='datas_fname', mimetype=None, download=None, access_token=None): obj = None if xmlid: obj = request.env.ref(xmlid, False) elif id and model in request.env.registry: obj = request.env[model].browse(int(id)) if not obj or not obj.exists() or field not in obj: return request.not_found() try: last_update = obj['__last_update'] except AccessError: return wrappers.Response(status=403, headers=[]) status, headers, content = None, [], None content = obj.with_context({'stream': True})[field] or b'' if not filename: if filename_field in obj: filename = obj[filename_field] else: filename = "%s-%s-%s" % (obj._name, obj.id, field) mimetype = 'mimetype' in obj and obj.mimetype or False if not mimetype and filename: mimetype = mimetypes.guess_type(filename)[0] headers += [('Content-Type', mimetype), ('X-Content-Type-Options', 'nosniff')] etag = bool(request) and request.httprequest.headers.get('If-None-Match') retag = '"%s"' % hashlib.md5(pycompat.to_text(content).encode('utf-8')).hexdigest() status = status or (304 if etag == retag else 200) headers.append(('ETag', retag)) if download: headers.append(('Content-Disposition', http.content_disposition(filename))) return wrappers.Response(content, headers=headers, direct_passthrough=True, status=status)
def _binary_set_headers(self, status, content, filename, mimetype, unique, filehash=None, download=False): headers = [('Content-Type', mimetype), ('X-Content-Type-Options', 'nosniff')] # cache etag = bool(request) and request.httprequest.headers.get( 'If-None-Match') status = status or 200 if filehash: headers.append(('ETag', filehash)) if etag == filehash and status == 200: status = 304 headers.append( ('Cache-Control', 'max-age=%s' % (http.STATIC_CACHE_LONG if unique else 0))) # content-disposition default name if download: headers.append( ('Content-Disposition', content_disposition(filename))) return (status, headers, content)
def _show_report(self, model, report_type, report_ref, download=False): if report_type not in ('html', 'pdf', 'text'): raise UserError(_("Invalid report type: %s", report_type)) report_sudo = request.env.ref(report_ref).with_user(SUPERUSER_ID) if not isinstance(report_sudo, type(request.env['ir.actions.report'])): raise UserError( _("%s is not the reference of a report", report_ref)) method_name = '_render_qweb_%s' % (report_type) report = getattr(report_sudo, method_name)([model.id], data={ 'report_type': report_type })[0] reporthttpheaders = [ ('Content-Type', 'application/pdf' if report_type == 'pdf' else 'text/html'), ('Content-Length', len(report)), ] if report_type == 'pdf' and download: filename = "%s.pdf" % (re.sub('\W+', '-', model._get_report_base_filename())) reporthttpheaders.append( ('Content-Disposition', content_disposition(filename))) return request.make_response(report, headers=reporthttpheaders)
def event_ics_file(self, event, **kwargs): files = event._get_ics_file() if not event.id in files: return NotFound() content = files[event.id] return request.make_response(content, [ ('Content-Type', 'application/octet-stream'), ('Content-Length', len(content)), ('Content-Disposition', content_disposition('%s.ics' % event.name)) ])
def download(self, generator, modules, **kwargs): generator = request.env[generator] modules = request.env['module.builder.main'].search([ ('id', 'in', modules.split(',')) ]) filename = "{name}.{ext}".format( name=modules[0].name if len( modules) == 1 else 'modules', ext="zip") zip_io = generator.get_zipped(modules) content = zip_io.getvalue() return request.make_response( content, headers=[ ('Content-Type', 'plain/text' or 'application/octet-stream'), ('Content-Disposition', content_disposition(filename)) ] )
def _generate_report(self, user_input, download=True): report = request.env.ref('survey.certification_report').with_user( SUPERUSER_ID)._render_qweb_pdf([user_input.id], data={'report_type': 'pdf'})[0] report_content_disposition = content_disposition('Certification.pdf') if not download: content_split = report_content_disposition.split(';') content_split[0] = 'inline' report_content_disposition = ';'.join(content_split) return request.make_response(report, headers=[ ('Content-Type', 'application/pdf'), ('Content-Length', len(report)), ('Content-Disposition', report_content_disposition), ])
def export_from_app_builder(self, data, token): app_data_id = json.loads(data)[4] app_builder = request.env['ir.module.module'] \ .search([('name', '=', 'web')]) ir_model_data = request.env['ir.model.data'] \ .search( [('is_app_builder', '=', True), ('app_data_id', '=', int(app_data_id))]) app_model_data = request.env['app.creator.data'] \ .search( [('id', '=', int(app_data_id))]) zip_data_content = self.archieve_content(app_builder, ir_model_data, data, app_model_data) return request.make_response( zip_data_content, headers=[ ('Content-Disposition', content_disposition(app_model_data.app_name + '.zip')), ('Content-Type', 'application/zip'), ('Content-Length', len(zip_data_content)), ], cookies={'fileToken': token})
def content_disposition(cls, filename): return content_disposition(filename)