def send_file(self, http, attachment=None, url=None,field=None,model=None,id=None): # return a image while given an attachment or an url if field: o = self.env[model].browse(int(id)) #_logger.warning('<<<<<<<<<<<<<< data >>>>>>>>>>>>>>>>: %s' % o) return http.send_file(StringIO(self.run(self.data_to_img(getattr(o, field))).make_blob(format='jpg')), filename=field, mtime=self.get_mtime(o)) if attachment: #_logger.warning('<<<<<<<<<<<<<< attachment >>>>>>>>>>>>>>>>: %s' % attachment) return http.send_file(StringIO(self.run(self.attachment_to_img(attachment)).make_blob(format='jpg')), filename=attachment.datas_fname, mtime=self.get_mtime(attachment)) return http.send_file(self.run(self.url_to_img(url)), filename=url)
def company_logo(self, dbname=None, company_id=None, **kw): imgname = 'logo.png' placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img') uid = None if request.session.db: dbname = request.session.db uid = request.session.uid elif dbname is None: dbname = db_monodb() if not uid: uid = openerp.SUPERUSER_ID if not dbname: response = http.send_file(placeholder(imgname)) else: try: # create an empty registry registry = openerp.modules.registry.Registry(dbname) with registry.cursor() as cr: if company_id: cr.execute( """SELECT c.logo_web, c.write_date FROM res_company c WHERE c.id = %s """, (company_id, )) else: cr.execute( """SELECT c.logo_web, c.write_date FROM res_users u LEFT JOIN res_company c ON c.id = u.company_id WHERE u.id = %s """, (uid, )) row = cr.fetchone() if row and row[0]: image_data = StringIO(str(row[0]).decode('base64')) response = http.send_file(image_data, filename=imgname, mtime=row[1]) else: response = http.send_file(placeholder('nologo.png')) except Exception as e: raise e response = http.send_file(placeholder(imgname)) return response
def download_attachment(self, attachment_id): # Check if this is a valid attachment id attachment = request.env['ir.attachment'].sudo().search_read([ ('id', '=', int(attachment_id)) ], [ "name", "datas", "file_type", "res_model", "res_id", "type", "url" ]) if attachment: attachment = attachment[0] else: return redirect('/shop') if attachment["type"] == "url": if attachment["url"]: return redirect(attachment["url"]) else: return request.not_found() elif attachment["datas"]: data = StringIO(base64.standard_b64decode(attachment["datas"])) return http.send_file(data, filename=attachment['name'], as_attachment=True) else: return request.not_found()
def send_file(self,attachment=None, url=None,field=None,model=None,id=None): # return a image while given an attachment or an url mimetype = 'image/%s' % (self.image_format or 'png') if field: #o = self.env[model].sudo().browse(int(id if id.isdigit() else 0)) o = self.env[model].sudo().search_read([('id','=',int(id if id.isdigit() else 0))],[field]) if not (o and o[0][field]): return http.send_file(StringIO(self.run(Image(filename=get_module_path('web') + '/static/src/img/placeholder.png')).make_blob(format=self.image_format or 'png')), mimetype=mimetype) o = o[0] #_logger.warning('<<<<<<<<<<<<<< data >>>>>>>>>>>>>>>>: %s' % o) #return http.send_file(StringIO(self.run(self.data_to_img(getattr(o, field)), record=o).make_blob(format=self.image_format if self.image_format else 'png')), mimetype=mimetype, filename=field, mtime=self.get_mtime(o)) return http.send_file(StringIO(self.run(Image(blob=o[field].decode('base64'))).make_blob(format=self.image_format or 'png')), mimetype=mimetype, filename=field) if attachment: #_logger.warning('<<<<<<<<<<<<<< attachment >>>>>>>>>>>>>>>>: %s' % attachment) return http.send_file(StringIO(self.run(self.attachment_to_img(attachment)).make_blob(format=self.image_format or 'png')), mimetype=mimetype, filename=attachment.datas_fname, mtime=self.get_mtime(attachment)) #~ return http.send_file(self.run(self.url_to_img(url)), filename=url) return http.send_file(StringIO(self.run(Image(filename=url)).make_blob(format=self.image_format or 'png')),mimetype=mimetype)
def get_attachment(self, attachment=None, file_name=None, **post): return http.send_file(StringIO(attachment.datas.decode('base64')), filename=attachment.datas_fname.replace( ' ', '_'), mimetype=attachment.mimetype, mtime=attachment.write_date, as_attachment=True)
def home_page_document(self, home_user=None, document=None,report=None, docname=None, tab='document', **post): self.validate_user(home_user) if report: #~ raise Warning(request.env[report.report_id.model].search(eval(report.domain or '[]')).mapped('id')) #~ pdf = request.env['report'].get_pdf(request.env['res.partner'].search([]),report.get_external_id()[report.id], data=None) pdf = report.sudo().report_id.render_report(request.env[report.report_id.model].search(eval(report.domain or '[]')).mapped('id'), report.report_id.report_name, data={})[0] #return http.send_file(StringIO(pdf), filename='min file.pdf', mimetype='application/pdf', as_attachment=True) return request.make_response(pdf, headers=[('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]) if document: document = request.env['ir.attachment'].search([('id', '=', document)]) if not document: _logger.warn('cannot read document') else: document = request.env['ir.attachment'].sudo().search([('id', '=', document.id)]) #TODO better security-check check:225 ir_attachment.py check:71 document.py _logger.warn('Try to send %s' % document) fname = (docname or document.datas_fname or document.name).encode('ascii', 'replace') mime = mimetype=document.mimetype or '' write_date=document.write_date data = document.datas.decode('base64') # ~ _logger.warn('%s %s %s %s'%(fname, mime, write_date, len(data))) return http.send_file(StringIO(data), filename=fname, mimetype=mime, mtime=write_date, as_attachment=True) # ~ return http.send_file(StringIO(data), filename=fname, mimetype=mime, mtime=write_date) return request.website.render('website_sale_home_document.page_documents', { 'home_user': home_user, 'documents': request.env['ir.attachment'].search([('partner_id', '=', home_user.partner_id.id)]), 'tab': tab, })
def company_logo(self, dbname=None, **kw): imgname = 'logo' imgext = '.png' company_logo = request.env['website'].sudo().search([])[0].company_logo custom_logo = tools.image_resize_image(company_logo, (150, None)) placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img') uid = None if request.session.db: dbname = request.session.db uid = request.session.uid elif dbname is None: dbname = db_monodb() if not uid: uid = openerp.SUPERUSER_ID if not dbname: response = http.send_file(placeholder(imgname + imgext)) else: try: # create an empty registry registry = openerp.modules.registry.Registry(dbname) if custom_logo: image_base64 = custom_logo.decode('base64') image_data = StringIO(image_base64) imgext = '.' + (imghdr.what(None, h=image_base64) or 'png') response = http.send_file(image_data, filename=imgname + imgext, mtime=None) else: with registry.cursor() as cr: cr.execute("""SELECT c.logo_web, c.write_date FROM res_users u LEFT JOIN res_company c ON c.id = u.company_id WHERE u.id = %s """, (uid,)) row = cr.fetchone() if row and row[0]: image_base64 = str(row[0]).decode('base64') image_data = StringIO(image_base64) imgext = '.' + (imghdr.what(None, h=image_base64) or 'png') response = http.send_file(image_data, filename=imgname + imgext, mtime=row[1]) else: response = http.send_file(placeholder('nologo.png')) except Exception: response = http.send_file(placeholder(imgname + imgext)) return response
def view_url(self, recipe=None, recipe_ref=None, **post): url = post.get('url','') if len(url)>0 and url[0] == '/': url=url[1:] if recipe_ref: recipe = request.env.ref(recipe_ref) # 'imagemagick.my_recipe' if url: return recipe.send_file(url='/'.join(get_module_path(url.split('/')[0]).split('/')[0:-1]) + '/' + url) return http.send_file(StringIO(recipe.run(Image(filename=get_module_path('web') + '/static/src/img/placeholder.png')).make_blob(format=recipe.image_format if recipe.image_format else 'png')))
def company_logo(self, dbname=None, company_id=None, **kw): imgname = 'logo.png' placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img') uid = None if request.session.db: dbname = request.session.db uid = request.session.uid elif dbname is None: dbname = db_monodb() if not uid: uid = openerp.SUPERUSER_ID if not dbname: response = http.send_file(placeholder(imgname)) else: try: # create an empty registry registry = openerp.modules.registry.Registry(dbname) with registry.cursor() as cr: if company_id: cr.execute("""SELECT c.logo_web, c.write_date FROM res_company c WHERE c.id = %s """, (company_id,)) else: cr.execute("""SELECT c.logo_web, c.write_date FROM res_users u LEFT JOIN res_company c ON c.id = u.company_id WHERE u.id = %s """, (uid,)) row = cr.fetchone() if row and row[0]: image_data = StringIO(str(row[0]).decode('base64')) response = http.send_file(image_data, filename=imgname, mtime=row[1]) else: response = http.send_file(placeholder('nologo.png')) except Exception as e: raise e response = http.send_file(placeholder(imgname)) return response
def user_image(self, user_id, dbname=None, **kw): imgname = 'user_image.png' placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img') if request.session.db: dbname = request.session.db elif dbname is None: dbname = db_monodb() if not dbname: response = http.send_file(placeholder(imgname)) else: try: # create an empty registry registry = openerp.modules.registry.Registry(dbname) with registry.cursor() as cr: cr.execute( """SELECT p.image_small, p.write_date FROM res_partner p LEFT JOIN res_users u ON u.partner_id = p.id WHERE u.id = %s """, (user_id, )) row = cr.fetchone() if row and row[0]: image_data = StringIO(str(row[0]).decode('base64')) response = http.send_file(image_data, filename=imgname, mtime=row[1]) else: response = http.send_file( placeholder('placeholder.png')) except Exception: response = http.send_file(placeholder(imgname)) return response
def send_file( self, http, attachment=None, url=None, field=None, model=None, id=None): # return a image while given an attachment or an url if field: o = self.env[model].sudo().browse(int(id)) #_logger.warning('<<<<<<<<<<<<<< data >>>>>>>>>>>>>>>>: %s' % o) return http.send_file(StringIO( self.run(self.data_to_img(getattr(o, field)), record=o).make_blob(format='png')), filename=field, mtime=self.get_mtime(o)) if attachment: #_logger.warning('<<<<<<<<<<<<<< attachment >>>>>>>>>>>>>>>>: %s' % attachment) return http.send_file(StringIO( self.run(self.attachment_to_img(attachment)).make_blob( format='png')), filename=attachment.datas_fname, mtime=self.get_mtime(attachment)) return http.send_file(self.run(self.url_to_img(url)), filename=url)
def send_file(path): image_base64 = base64.b64encode(open(path, 'rb').read()) image_data = StringIO(str(image_base64).decode('base64')) return http.send_file(image_data, mimetype='image/jpeg')
def get_attachment(self, attachment=None, file_name=None, **post): return http.send_file(StringIO(attachment.datas.decode('base64')), filename=attachment.datas_fname.replace(' ', '_'), mimetype=attachment.mimetype, mtime=attachment.write_date, as_attachment=True)
def w_product_import_csv(self, **kwargs): return http.send_file(os.path.join( os.path.dirname(os.path.abspath(__file__)), 'import.xlsx'), filename='import.xlsx', as_attachment=True)
def sendfile(filepath_or_fp, filename="110.NC"): print("*"*100) return http.send_file(filepath_or_fp, mimetype=None, as_attachment=True, filename=filename)
def company_logo(self, dbname=None, **kw): imgname = "logo.png" placeholder = functools.partial(get_module_resource, "web", "static", "src", "img") uid = None if request.session.db: dbname = request.session.db uid = request.session.uid elif dbname is None: dbname = db_monodb() if not uid: uid = openerp.SUPERUSER_ID groups_obj = request.registry.get("res.groups") groups_id = groups_obj.search(request.cr, openerp.SUPERUSER_ID, [("name", "=", "Portal")]) pid = None if groups_id: group_browse = groups_obj.browse(request.cr, openerp.SUPERUSER_ID, groups_id, context=request.context) if group_browse.users: for user in group_browse.users: if uid == user.id: pid = uid if not dbname: response = http.send_file(placeholder(imgname)) else: try: if pid: # create an empty registry registry = openerp.modules.registry.Registry(dbname) with registry.cursor() as cr: cr.execute( """SELECT c.portal_image, c.write_date FROM res_users u LEFT JOIN res_company c ON c.id = u.company_id WHERE u.id = %s """, (uid,), ) row = cr.fetchone() if row and row[0]: image_data = StringIO(str(row[0]).decode("base64")) response = http.send_file(image_data, filename=imgname, mtime=row[1]) else: response = http.send_file(placeholder("nologo.png")) else: registry = openerp.modules.registry.Registry(dbname) with registry.cursor() as cr: cr.execute( """SELECT c.logo_web, c.write_date FROM res_users u LEFT JOIN res_company c ON c.id = u.company_id WHERE u.id = %s """, (uid,), ) row = cr.fetchone() if row and row[0]: image_data = StringIO(str(row[0]).decode("base64")) response = http.send_file(image_data, filename=imgname, mtime=row[1]) else: response = http.send_file(placeholder("nologo.png")) except Exception: response = http.send_file(placeholder(imgname)) return response