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.

105 lines
4.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK SaaS Branding
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import io
  23. import os
  24. import base64
  25. import mimetypes
  26. import functools
  27. from odoo import http, SUPERUSER_ID
  28. from odoo.modules import registry, get_resource_path
  29. from odoo.tools.mimetypes import guess_mimetype
  30. from odoo.tools import config
  31. from odoo.http import request
  32. from odoo.addons.web.controllers.main import Binary
  33. from odoo.addons.muk_saas_branding.tools.utils import safe_execute
  34. class Binary(Binary):
  35. def _get_company_image_placeholder(self):
  36. if config.get("default_company_image_folder", False):
  37. def get_path(filename):
  38. return os.path.join(config.get("default_company_image_folder"), filename)
  39. return get_path
  40. return functools.partial(get_resource_path, 'muk_saas_branding', 'static', 'src', 'img')
  41. def _get_company_image_data(self, dbname, uid, field, company=None):
  42. try:
  43. dbreg = registry.Registry(dbname)
  44. with dbreg.cursor() as cr:
  45. if company:
  46. cr.execute("""
  47. SELECT {0}, write_date
  48. FROM res_company
  49. WHERE id = %s
  50. """.format(field), (company,))
  51. else:
  52. cr.execute("""
  53. SELECT c.{0}, c.write_date
  54. FROM res_users u
  55. LEFT JOIN res_company c
  56. ON c.id = u.company_id
  57. WHERE u.id = %s
  58. """.format(field), (uid,))
  59. return cr.fetchone()
  60. except Exception:
  61. return None
  62. def _get_company_image_response(self, dbname, field, placeholders, default_mimetype, company=None):
  63. uid = request.session.uid if request.session.db else None
  64. placeholder = self._get_company_image_placeholder()
  65. if request.session.db:
  66. dbname = request.session.db
  67. elif dbname is None:
  68. dbname = http.db_monodb()
  69. if not dbname:
  70. response = http.send_file(placeholder(placeholders[0]))
  71. else:
  72. uid = uid if uid else SUPERUSER_ID
  73. company_data = self._get_company_image_data(dbname, uid, field, company)
  74. if company_data and company_data[0]:
  75. image_data = base64.b64decode(company_data[0])
  76. mimetype = guess_mimetype(image_data, default=default_mimetype)
  77. extension = mimetypes.guess_extension(mimetype)
  78. response = http.send_file(
  79. io.BytesIO(image_data), filename=('logo%s' % extension),
  80. mimetype=mimetype, mtime=company_data[1]
  81. )
  82. else:
  83. response = http.send_file(placeholder(placeholders[1]))
  84. return response
  85. @http.route(['/web/binary/company_logo', '/logo', '/logo.png'], type='http', auth="none")
  86. def company_logo(self, dbname=None, **kw):
  87. company = safe_execute(False, int, kw['company']) if kw and kw.get('company') else False
  88. return self._get_company_image_response(
  89. dbname, 'logo_web', ('logo.png', 'nologo.png'), 'image/png', company
  90. )
  91. @http.route(['/web/binary/company_favicon', '/favicon', '/favicon.ico'], type='http', auth="none")
  92. def company_favicon(self, dbname=None, **kw):
  93. company = safe_execute(False, int, kw['company']) if kw and kw.get('company') else False
  94. return self._get_company_image_response(
  95. dbname, 'favicon', ('favicon.ico', 'nofavicon.ico'), 'image/x-icon', company
  96. )