中国本土应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 line
2.9KB

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import io
  4. import functools
  5. import logging
  6. try:
  7. from werkzeug.utils import send_file
  8. except ImportError:
  9. from odoo.tools._vendor.send_file import send_file
  10. import odoo
  11. from odoo import _
  12. from odoo.modules import get_resource_path, module
  13. import odoo.http as http
  14. from odoo.http import request, content_disposition
  15. from odoo.tools.mimetypes import guess_mimetype
  16. from odoo.addons.web.controllers.binary import Binary
  17. _logger = logging.getLogger(__name__)
  18. class WebThemeBinary(Binary):
  19. @http.route(
  20. [
  21. "/web/binary/company_square_logo",
  22. "/company_square_logo",
  23. "/company_square_logo.png",
  24. ],
  25. type="http",
  26. auth="none",
  27. cors="*",
  28. )
  29. def company_square_logo(self, dbname=None, **kw):
  30. imgname = "square_logo"
  31. imgext = ".png"
  32. placeholder = functools.partial(
  33. get_resource_path, "oec_theme_backend", "static", "img"
  34. )
  35. dbname = request.db
  36. uid = (request.session.uid if dbname else None) or odoo.SUPERUSER_ID
  37. if not dbname:
  38. response = http.Stream.from_path(placeholder(imgname + imgext)).get_response()
  39. else:
  40. try:
  41. # create an empty registry
  42. registry = odoo.modules.registry.Registry(dbname)
  43. with registry.cursor() as cr:
  44. company = int(kw['company']) if kw and kw.get('company') else False
  45. if company:
  46. cr.execute("""SELECT square_logo_web, write_date
  47. FROM res_company
  48. WHERE id = %s
  49. """, (company,))
  50. else:
  51. cr.execute("""SELECT c.square_logo_web, c.write_date
  52. FROM res_users u
  53. LEFT JOIN res_company c
  54. ON c.id = u.company_id
  55. WHERE u.id = %s
  56. """, (uid,))
  57. row = cr.fetchone()
  58. if row and row[0]:
  59. image_base64 = base64.b64decode(row[0])
  60. image_data = io.BytesIO(image_base64)
  61. mimetype = guess_mimetype(image_base64, default='image/png')
  62. imgext = '.' + mimetype.split('/')[1]
  63. if imgext == '.svg+xml':
  64. imgext = '.svg'
  65. response = send_file(image_data, filename=imgname + imgext, mimetype=mimetype, mtime=row[1])
  66. else:
  67. response = http.Stream.from_path(placeholder('nologo.png')).get_response()
  68. except Exception:
  69. response = http.Stream.from_path(placeholder(imgname + imgext)).get_response()
  70. return response
上海开阖软件有限公司 沪ICP备12045867号-1