中国本土应用
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

97 行
3.3KB

  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(
  39. placeholder(imgname + imgext)
  40. ).get_response()
  41. else:
  42. try:
  43. # create an empty registry
  44. registry = odoo.modules.registry.Registry(dbname)
  45. with registry.cursor() as cr:
  46. company = int(kw["company"]) if kw and kw.get("company") else False
  47. if company:
  48. cr.execute(
  49. """SELECT square_logo_web, write_date
  50. FROM res_company
  51. WHERE id = %s
  52. """,
  53. (company,),
  54. )
  55. else:
  56. cr.execute(
  57. """SELECT c.square_logo_web, c.write_date
  58. FROM res_users u
  59. LEFT JOIN res_company c
  60. ON c.id = u.company_id
  61. WHERE u.id = %s
  62. """,
  63. (uid,),
  64. )
  65. row = cr.fetchone()
  66. if row and row[0]:
  67. image_base64 = base64.b64decode(row[0])
  68. image_data = io.BytesIO(image_base64)
  69. mimetype = guess_mimetype(image_base64, default="image/png")
  70. imgext = "." + mimetype.split("/")[1]
  71. if imgext == ".svg+xml":
  72. imgext = ".svg"
  73. response = send_file(
  74. image_data,
  75. filename=imgname + imgext,
  76. mimetype=mimetype,
  77. mtime=row[1],
  78. )
  79. else:
  80. response = http.Stream.from_path(
  81. placeholder("nologo.png")
  82. ).get_response()
  83. except Exception:
  84. response = http.Stream.from_path(
  85. placeholder(imgname + imgext)
  86. ).get_response()
  87. return response
上海开阖软件有限公司 沪ICP备12045867号-1