def course_calendar_pdf(self, file=None, **post): if file: status, headers, content = binary_content( model='registration.files', id=file.id, field='registration_file', filename_field='filename', env=request.env(user=SUPERUSER_ID)) content_base64 = base64.b64decode(content) if content else '' headers.append(('Content-Length', len(content_base64))) return request.make_response(content_base64, headers) return werkzeug.wrappers.Response(status=404) classes = request.env['class.class'].sudo().search([ ('date_start', '>=', fields.Datetime.now()) ]) class_list = dict() for c in classes: if c.course_id not in class_list: class_list.update({c.course_id: request.env['class.class']}) class_list[c.course_id] = class_list[c.course_id] + c pdf = request.env['report'].sudo().with_context( set_viewport_size=True, class_list=class_list).get_pdf( classes.ids, 'theme_atts.report_course_calendar') pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] return request.make_response(pdf, headers=pdfhttpheaders)
def downloadCatalog(self): result = request.env['website_cstm.product_download_catelog'].search( [('status', '=', 'active')], limit=1) if result: id = result.id status, headers, content = binary_content( model='website_cstm.product_download_catelog', id=id, field='file', filename_field='filename', download=True, env=request.env(user=SUPERUSER_ID)) if not content: img_path = modules.get_module_resource('web', 'static/src/img', 'placeholder.png') with open(img_path, 'rb') as f: image = f.read() content = base64.b64encode(image) if status == 304: return werkzeug.wrappers.Response(status=304) image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response raise werkzeug.exceptions.NotFound()
def content_common(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename=None, filename_field='datas_fname', unique=None, mimetype=None, download=None, data=None, token=None, access_token=None, related_id=None, access_mode=None, **kw): status, headers, content = binary_content( xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype, access_token=access_token, related_id=related_id, access_mode=access_mode) if status == 304: response = werkzeug.wrappers.Response( status=status, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200: response = http.request.not_found() else: content_base64 = base64.b64decode(content) headers.append(('Content-Length', len(content_base64))) buf = BytesIO(content_base64) data = wrap_file(http.request.httprequest.environ, buf) response = http.Response( data, headers=headers, direct_passthrough=True) if token: response.set_cookie('fileToken', token) return response
def avatar(self, res_model, res_id, partner_id): headers = [("Content-Type", "image/png")] status = 200 content = "R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" # default image is one white pixel if res_model in request.env: try: # if the current user has access to the document, get the partner avatar as sudo() request.env[res_model].browse(res_id).check_access_rule("read") if ( partner_id in request.env[res_model].browse(res_id).sudo().exists().message_ids.mapped("author_id").ids ): status, headers, _content = binary_content( model="res.partner", id=partner_id, field="image_medium", default_mimetype="image/png", env=request.env(user=SUPERUSER_ID), ) # binary content return an empty string and not a placeholder if obj[field] is False if _content != "": content = _content if status == 304: return werkzeug.wrappers.Response(status=304) except AccessError: pass image_base64 = base64.b64decode(content) headers.append(("Content-Length", len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def get_entity_logo(self, entity_id=None, entity_code=None): try: entity_id = int(entity_id) except: entity_id = -1 entity_model = request.env['s2u.crm.entity'] if entity_id > 0: entity = entity_model.sudo().search([('id', '=', entity_id), ('entity_code', '=', entity_code)]) if not entity: entity_id = -1 status, headers, content = binary_content( model='s2u.crm.entity', id=entity_id, field='image', default_mimetype='image/jpeg', env=request.env(user=SUPERUSER_ID)) if not content: return request.render("website.404") if status == 304: return werkzeug.wrappers.Response(status=304) image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def content_common(self, slug_name_with_id, token=None, download=None, **kw): storage_file = request.env["storage.file"].get_from_slug_name_with_id( slug_name_with_id) status, headers, content = binary_content( model=storage_file._name, id=storage_file.id, field="data", filename_field="name", download=download, ) if status == 304: response = werkzeug.wrappers.Response(status=status, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200: response = request.not_found() else: content_base64 = base64.b64decode(content) headers.append(("Content-Length", len(content_base64))) response = request.make_response(content_base64, headers) if token: response.set_cookie("fileToken", token) return response
def avatar(self, res_model, res_id, partner_id): headers = [('Content-Type', 'image/png')] status = 200 content = 'R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' # default image is one white pixel if res_model in request.env: try: # if the current user has access to the document, get the partner avatar as sudo() request.env[res_model].browse(res_id).check_access_rule('read') if partner_id in request.env[res_model].browse(res_id).sudo( ).exists().message_ids.mapped('author_id').ids: status, headers, _content = binary_content( model='res.partner', id=partner_id, field='image_medium', default_mimetype='image/png', env=request.env(user=SUPERUSER_ID)) # binary content return an empty string and not a placeholder if obj[field] is False if _content != '': content = _content if status == 304: return werkzeug.wrappers.Response(status=304) except AccessError: pass image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def avatar(self, res_model, res_id, partner_id): headers = [('Content-Type', 'image/png')] status = 200 content = 'R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' # default image is one white pixel if res_model in request.env: try: # if the current user has access to the document, get the partner avatar as sudo() request.env[res_model].browse(res_id).check_access_rule('read') if partner_id in request.env[res_model].browse(res_id).sudo().exists().message_ids.mapped('author_id').ids: status, headers, _content = binary_content(model='res.partner', id=partner_id, field='image_medium', default_mimetype='image/png', env=request.env(user=SUPERUSER_ID)) # binary content return an empty string and not a placeholder if obj[field] is False if _content != '': content = _content if status == 304: return werkzeug.wrappers.Response(status=304) except AccessError: pass if status == 301 and is_url(content): r = requests.get(content) image_base64 = r.content else: image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def extra_image(self, db_name, attachment_id): http.request.session.db = db_name env = http.request.env(http.request.cr, SUPERUSER_ID, http.request.env.context) status, headers, content = binary_content(model='ir.attachment', id=attachment_id, field='datas', env=env) if status == 304: return werkzeug.wrappers.Response(status=304, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200: return http.request.not_found() if content: image_base64 = base64.b64decode(content) else: image_base64 = self.placeholder(image='placeholder.png') headers = self.force_contenttype(headers, contenttype='image/png') headers.append(('Content-Length', len(image_base64))) response = http.request.make_response(image_base64, headers) response.status_code = status return response
def download_po_excel_report(self, file=None, **post): if file: status, headers, content = binary_content( model='purchase.order.excel.report', id=file.id, field='excel_file', filename_field=file.file_name, env=request.env(user=SUPERUSER_ID)) content_base64 = base64.b64decode(content) if content else '' headers.append(('Content-Type', 'application/vnd.ms-excel')) headers.append(('Content-Disposition', 'attachment;filename=' + file.file_name + ' ; ')) return request.make_response(content_base64, headers) return False
def download_document(self, model, record_id, binary_field, filename_field, token=None): if not record_id: return request.not_found() else: status, headers, content = binary_content(model=model, id=record_id, field=binary_field, filename_field=filename_field, download=True) if status != 200: response = request.not_found() else: headers.append(('Content-Length', len(content))) response = request.make_response(content, headers) if token: response.set_cookie('fileToken', token) return response
def livechat_lib(self, ext, **kwargs): # _get_asset return the bundle html code (script and link list) but we want to use the attachment content xmlid = 'im_livechat.external_lib' files, remains = request.env["ir.qweb"]._get_asset_content(xmlid, options=request.context) asset = AssetsBundle(xmlid, files, remains) mock_attachment = getattr(asset, ext)() if isinstance(mock_attachment, list): # suppose that CSS asset will not required to be split in pages mock_attachment = mock_attachment[0] # can't use /web/content directly because we don't have attachment ids (attachments must be created) status, headers, content = binary_content(id=mock_attachment.id, unique=asset.checksum) content_base64 = base64.b64decode(content) if content else '' headers.append(('Content-Length', len(content_base64))) return request.make_response(content_base64, headers)
def user_avatar(self, user_id=0, **post): status, headers, content = binary_content(model='res.users', id=user_id, field='image_medium', default_mimetype='image/png', env=request.env(user=SUPERUSER_ID)) if not content: img_path = modules.get_module_resource('web', 'static/src/img', 'placeholder.png') with open(img_path, 'rb') as f: image = f.read() content = base64.b64encode(image) if status == 304: return werkzeug.wrappers.Response(status=304) image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def get_media_content(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename_field='datas_fname', unique=None, filename=None, mimetype=None, download=False, default_mimetype=None, media_type=None): status, headers, content = binary_content(xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype, default_mimetype=default_mimetype) if status == 304: response = werkzeug.wrappers.Response(status=status, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200: response = request.not_found() else: content_base64 = base64.b64decode(content) headers.append(('Content-Disposition', 'inline; filename=%s.' % str(filename) + media_type)) response = request.make_response(content_base64, headers) response.status_code = status return response
def download_document(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename=None, filename_field='datas_fname', unique=None, mimetype=None, download=None, data=None, token=None, debug=None): status, headers, content = binary_content(xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype) if status == 304: response = werkzeug.wrappers.Response(status=status, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200: response = request.not_found() else: content_base64 = base64.b64decode(content) headers.append(('Content-Length', len(content_base64))) headers.append(('Content-Type', 'application/octet-stream')) headers.append(('Content-Disposition', content_disposition(filename))) response = request.make_response(content_base64, headers) if token: response.set_cookie('fileToken', token) return response
def shop_product_picture(self, product_model=None, product_id=None, picture='default', size='default'): try: product_id = int(product_id) except: product_id = False if size == 'medium': field_name = 'image_medium' elif size == 'small': field_name = 'image_small' else: field_name = 'image' content = False request.cr.execute("SELECT id FROM s2u_baseproduct_item " "WHERE company_id = %s AND res_model = %s LIMIT 1", (request.website.company_id.id, product_model)) exists = request.cr.fetchall() if exists: status, headers, content = binary_content(model=product_model, id=product_id, field=field_name, default_mimetype='image/jpeg', env=request.env(user=SUPERUSER_ID)) if not content: img_path = modules.get_module_resource('s2uecommerce', 'static/src/img', 'product_preview.png') with open(img_path, 'rb') as f: image = f.read() content = base64.b64encode(image) if status == 304: return werkzeug.wrappers.Response(status=304) image_base64 = base64.b64decode(content) headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def core_content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename_field='datas_fname', unique=None, filename=None, mimetype=None, download=None, width=0, height=0, crop=False, access_token=None, **kwargs): contenttype = kwargs.get('wk_mime_type') or 'image/jpg' status, headers, content = binary_content( xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype, default_mimetype='image/jpg', access_token=access_token) if status == 304: return werkzeug.wrappers.Response(status=304, headers=headers) elif status == 301: return werkzeug.utils.redirect(content, code=301) elif status != 200 and download: return request.not_found() height = int(height or 0) width = int(width or 0) if crop and (width or height): content = crop_image(content, type='center', size=(width, height), ratio=(1, 1)) elif content and (width or height): # resize maximum 500*500 if width > 500: width = 500 if height > 500: height = 500 content = odoo.tools.image_resize_image(base64_source=content, size=(width or None, height or None), encoding='base64', filetype='PNG') # resize force jpg as filetype headers = Binary().force_contenttype(headers, contenttype='image/jpg') if content: image_base64 = base64.b64decode(content) else: image_base64 = Binary().placeholder(image='placeholder.jpg') # could return (contenttype, content) in master headers = Binary().force_contenttype(headers, contenttype='image/jpg') headers.append(('Content-Length', len(image_base64))) response = request.make_response(image_base64, headers) response.status_code = status return response
def user_avatar(self, user_id=0, **post): status, headers, content = binary_content( model="res.users", id=user_id, field="image_medium", default_mimetype="image/png", env=request.env(user=SUPERUSER_ID), ) if not content: img_path = modules.get_module_resource("web", "static/src/img", "placeholder.png") with open(img_path, "rb") as f: image = f.read() content = image.encode("base64") if status == 304: return werkzeug.wrappers.Response(status=304) image_base64 = base64.b64decode(content) headers.append(("Content-Length", len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def avatar(self, res_model, res_id, partner_id): headers = [("Content-Type", "image/png")] status = 200 content = "R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" # default image is one white pixel if res_model in request.env: try: # if the current user has access to the document, get the partner avatar as sudo() request.env[res_model].browse(res_id).check_access_rule("read") if (partner_id in request.env[res_model].browse(res_id).sudo().exists( ).message_ids.mapped("author_id").ids): status, headers, _content = binary_content( model="res.partner", id=partner_id, field="image_medium", default_mimetype="image/png", env=request.env(user=SUPERUSER_ID), ) # binary content return an empty string and not a placeholder if obj[field] is False if _content != "": content = _content if status == 304: return werkzeug.wrappers.Response(status=304) except AccessError: pass if status == 301 and is_url(content): r = requests.get(content, timeout=5) image_base64 = r.content else: image_base64 = base64.b64decode(content) headers.append(("Content-Length", len(image_base64))) response = request.make_response(image_base64, headers) response.status = str(status) return response
def cont_export_usfm(self, part_id=0): user_id = request.env.user if part_id.create_id == user_id: content_id = part_id.content_id status, headers, rest = binary_content( model='open.content', id=content_id.id, field='rest', env=request.env(user=SUPERUSER_ID)) rest = base64.b64decode(rest).decode('utf-8') rest = rest + '\c ' + part_id.name + '\n' + '\p' + '\n' for line_id in part_id.line_ids: rest = rest + r'\v ' + line_id.verse + ' ' + line_id.name rest = rest.encode('utf-8') while part_id.sequence < len(content_id.part_ids): content_id.update({ 'rest': base64.b64encode(rest), }) next_part_id = request.env['open.part'].search([ ('content_id', '=', content_id.id), ('sequence', '=', part_id.sequence + 1) ]) n_values = { 'part_id': next_part_id, } return request.render("website_openbiblica.export_usfm", n_values) content_id.update({ 'files': base64.b64encode(rest), 'rest': None, }) return request.redirect('/content/%s' % slug(content_id))