中国本土应用
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.

172 lines
5.8KB

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. from email.policy import default
  4. import io
  5. import os
  6. from odoo import models, fields, api, tools, _
  7. from odoo.modules.module import get_module_resource, get_resource_path
  8. from random import randrange
  9. from PIL import Image
  10. class ResCompany(models.Model):
  11. _inherit = "res.company"
  12. def _get_theme_favicon(self, original=False):
  13. img_path = get_resource_path("oec_theme_backend", "static/img/favicon.ico")
  14. with tools.file_open(img_path, "rb") as f:
  15. if original:
  16. return base64.b64encode(f.read())
  17. # Modify the source image to add a colored bar on the bottom
  18. # This could seem overkill to modify the pixels 1 by 1, but
  19. # Pillow doesn't provide an easy way to do it, and this
  20. # is acceptable for a 16x16 image.
  21. color = (
  22. randrange(32, 224, 24),
  23. randrange(32, 224, 24),
  24. randrange(32, 224, 24),
  25. )
  26. original = Image.open(f)
  27. new_image = Image.new("RGBA", original.size)
  28. height = original.size[1]
  29. width = original.size[0]
  30. bar_size = 1
  31. for y in range(height):
  32. for x in range(width):
  33. pixel = original.getpixel((x, y))
  34. if height - bar_size <= y + 1 <= height:
  35. new_image.putpixel((x, y), (color[0], color[1], color[2], 255))
  36. else:
  37. new_image.putpixel(
  38. (x, y), (pixel[0], pixel[1], pixel[2], pixel[3])
  39. )
  40. stream = io.BytesIO()
  41. new_image.save(stream, format="ICO")
  42. return base64.b64encode(stream.getvalue())
  43. def _default_theme(self):
  44. return self.env["res.theme"].sudo()._get_or_create_theme(self.id, "company")
  45. def _get_square_logo(self):
  46. img_path = get_module_resource(
  47. "oec_theme_backend", "static", "img", "square_logo.png"
  48. )
  49. return base64.b64encode(open(img_path, "rb").read())
  50. square_logo = fields.Binary(
  51. default=_get_square_logo,
  52. # related="partner_id.image_1920",
  53. string="Company Square Logo",
  54. readonly=False,
  55. )
  56. square_logo_web = fields.Binary(
  57. compute="_compute_square_logo_web", store=True, attachment=False
  58. )
  59. @api.depends("square_logo")
  60. def _compute_square_logo_web(self):
  61. for company in self:
  62. img = company.square_logo
  63. company.square_logo_web = img and base64.b64encode(
  64. tools.image_process(base64.b64decode(img), size=(180, 0))
  65. )
  66. theme_id = fields.Many2one(
  67. "res.theme",
  68. string="Theme",
  69. store=True,
  70. domain="[('company_id', '=', id)]",
  71. )
  72. disable_theme_customizer = fields.Boolean(
  73. string="Disable theme customizer",
  74. related="theme_id.disable_theme_customizer",
  75. readonly=False,
  76. )
  77. # ------------------------------------------------------------
  78. # main
  79. # ------------------------------------------------------------
  80. main_menu_mode = fields.Selection(related="theme_id.main_menu_mode", readonly=False)
  81. main_submenu_position = fields.Selection(
  82. related="theme_id.main_submenu_position",
  83. readonly=False,
  84. )
  85. # ------------------------------------------------------------
  86. # SideNavbar
  87. # ------------------------------------------------------------
  88. sidebar_display_number_of_submenus = fields.Boolean(
  89. related="theme_id.sidebar_display_number_of_submenus", readonly=False
  90. )
  91. sidebar_fixed = fields.Boolean(related="theme_id.sidebar_fixed", readonly=False)
  92. sidebar_show_minimize_button = fields.Boolean(
  93. related="theme_id.sidebar_show_minimize_button", readonly=False
  94. )
  95. sidebar_default_minimized = fields.Boolean(
  96. related="theme_id.sidebar_default_minimized", readonly=False
  97. )
  98. sidebar_hover_maximize = fields.Boolean(
  99. related="theme_id.sidebar_hover_maximize", readonly=False
  100. )
  101. # 用户菜单
  102. menuitem_id = fields.Many2one(
  103. "res.user.menuitems",
  104. string="User menu items",
  105. store=True,
  106. domain="[('company_id', '=', id)]",
  107. )
  108. enable_odoo_account = fields.Boolean(
  109. related="menuitem_id.enable_odoo_account", readonly=False
  110. )
  111. enable_lock_screen = fields.Boolean(
  112. related="menuitem_id.enable_lock_screen", readonly=False
  113. )
  114. lock_screen_state_storage_mode = fields.Selection(
  115. string="Lock screen state storage mode",
  116. selection=[
  117. ("1", "Use browser's local storage"),
  118. ("2", "Use database"),
  119. ],
  120. default="1",
  121. )
  122. enable_developer_tool = fields.Boolean(
  123. related="menuitem_id.enable_developer_tool", readonly=False
  124. )
  125. enable_documentation = fields.Boolean(
  126. related="menuitem_id.enable_documentation", readonly=False
  127. )
  128. documentation_url = fields.Char(
  129. related="menuitem_id.documentation_url", readonly=False
  130. )
  131. enable_support = fields.Boolean(
  132. related="menuitem_id.enable_support", readonly=False
  133. )
  134. support_url = fields.Char(related="menuitem_id.support_url", readonly=False)
  135. @api.model_create_multi
  136. def create(self, vals_list):
  137. """
  138. 创建新公司时,创建主题
  139. """
  140. companies = super(ResCompany, self).create(vals_list)
  141. for new_company in companies:
  142. new_company.theme_id = (
  143. self.env["res.theme"]
  144. .sudo()
  145. ._get_or_create_theme(new_company.id, "company")
  146. )
  147. new_company.menuitem_id = (
  148. self.env["res.user.menuitems"]
  149. .sudo()
  150. ._get_or_create_menuitems(new_company.id)
  151. )
  152. return companies
上海开阖软件有限公司 沪ICP备12045867号-1