Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PlustronWebsite(Website):
- # ------------------------------------------------------
- # View
- # ------------------------------------------------------
- #
- @http.route([
- '/web/binary/company_logo',
- '/logo',
- '/logo.png',
- ], type='http', auth="public", cors="*")
- def company_logo(self, dbname=None, **kw):
- imgname = 'logo.png'
- placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img')
- uid = None
- wid = None
- if request.session.db:
- dbname = request.session.db
- uid = request.session.uid
- wid = request.registry['website'].get_current_website(request.cr, request.uid, context=request.context).id
- elif dbname is None:
- dbname = db_monodb()
- if not uid and not wid:
- uid = 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 wid:
- cr.execute("""SELECT c.logo_web, c.write_date
- FROM website u
- LEFT JOIN res_company c
- ON c.id = u.company_id
- WHERE u.id = %s
- """, (wid,))
- 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:
- response = http.send_file(placeholder(imgname))
- return response
- @http.route(['/robots.txt'], type='http', auth="public")
- def robots(self):
- return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
- @http.route('/sitemap.xml', type='http', auth="public", website=True)
- def sitemap_xml_index(self):
- cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
- ira = request.registry['ir.attachment']
- iuv = request.registry['ir.ui.view']
- mimetype = 'application/xml;charset=utf-8'
- content = None
- def create_sitemap(url, content):
- ira.create(cr, uid, dict(
- datas=content.encode('base64'),
- mimetype=mimetype,
- type='binary',
- name=url,
- url=url,
- ), context=context)
- sitemap = ira.search_read(cr, uid, [('url', '=', '/sitemap.xml'), ('type', '=', 'binary')],
- ('datas', 'create_date'), context=context)
- if sitemap:
- # Check if stored version is still valid
- server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
- create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
- delta = datetime.datetime.now() - create_date
- if delta < SITEMAP_CACHE_TIME:
- content = sitemap[0]['datas'].decode('base64')
- if not content:
- # Remove all sitemaps in ir.attachments as we're going to regenerated them
- sitemap_ids = ira.search(cr, uid, [('url', '=like', '/sitemap%.xml'), ('type', '=', 'binary')],
- context=context)
- if sitemap_ids:
- ira.unlink(cr, uid, sitemap_ids, context=context)
- pages = 0
- first_page = None
- locs = request.website.sudo(user=request.website.user_id.id).enumerate_pages()
- while True:
- start = pages * LOC_PER_SITEMAP
- values = {
- 'locs': islice(locs, start, start + LOC_PER_SITEMAP),
- 'url_root': request.httprequest.url_root[:-1],
- }
- urls = iuv.render(cr, uid, 'website.sitemap_locs', values, context=context)
- if urls.strip():
- page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
- if not first_page:
- first_page = page
- pages += 1
- create_sitemap('/sitemap-%d.xml' % pages, page)
- else:
- break
- if not pages:
- return request.not_found()
- elif pages == 1:
- content = first_page
- else:
- # Sitemaps must be split in several smaller files with a sitemap index
- content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
- pages=range(1, pages + 1),
- url_root=request.httprequest.url_root,
- ), context=context)
- create_sitemap('/sitemap.xml', content)
- return request.make_response(content, [('Content-Type', mimetype)])
- # ------------------------------------------------------
- # Misc. route utils
- # ------------------------------------------------------
- @http.route(['/report/barcode', '/report/barcode/<type>/<path:value>'], type='http', auth="user")
- def report_barcode(self, type, value, width=300, height=300, humanreadable=0):
- """Contoller able to render barcode images thanks to reportlab.
- Samples:
- <img t-att-src="'/report/barcode/QR/%s' % o.name"/>
- <img t-att-src="'/report/barcode/?type=%s&value=%s&width=%s&height=%s' %
- ('QR', o.name, 200, 200)"/>
- :param type: Accepted types: 'Codabar', 'Code11', 'Code128', 'EAN13', 'EAN8', 'Extended39',
- 'Extended93', 'FIM', 'I2of5', 'MSI', 'POSTNET', 'QR', 'Standard39', 'Standard93',
- 'UPCA', 'USPS_4State'
- :param humanreadable: Accepted values: 0 (default) or 1. 1 will insert the readable value
- at the bottom of the output image
- """
- try:
- width, height, humanreadable = int(width), int(height), bool(int(humanreadable))
- barcode = createBarcodeDrawing(
- type, value=value, format='png', width=width, height=height,
- humanReadable=humanreadable
- )
- barcode = barcode.asString('png')
- except (ValueError, AttributeError):
- raise exceptions.HTTPException(description='Cannot convert into barcode.')
- return request.make_response(barcode, headers=[('Content-Type', 'image/png')])
Add Comment
Please, Sign In to add comment