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

158 lines
5.0KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import fields, models, api, _
  3. from odoo.tools import config, ormcache, mute_logger
  4. FIELD_TYPES = [(key, key) for key in sorted(fields.Field.by_type)]
  5. class WeComAppConfig(models.Model):
  6. _name = "wecom.app_config"
  7. _description = "Wecom Application Configuration"
  8. _table = "wecom_app_config"
  9. # _rec_name = "key"
  10. _order = "id"
  11. app_id = fields.Many2one(
  12. "wecom.apps",
  13. string="Application",
  14. copy=False,
  15. ondelete="cascade",
  16. default=lambda self: self.env["wecom.apps"].id,
  17. # domain="[('company_id', '=', company_id)]",
  18. required=True,
  19. )
  20. company_id = fields.Many2one(related="app_id.company_id", store=True)
  21. name = fields.Char(string="Name", translate=True, required=True, copy=True)
  22. key = fields.Char(
  23. required=True,
  24. )
  25. ttype = fields.Selection(
  26. selection=FIELD_TYPES, string="Field Type", required=True, copy=True
  27. )
  28. value = fields.Text(required=True)
  29. description = fields.Html(string="Description", translate=True, copy=True)
  30. _sql_constraints = [
  31. (
  32. "app_id_key_uniq",
  33. "unique (app_id,key)",
  34. _("The key of each application must be unique !"),
  35. )
  36. ]
  37. def get_format_field_value_and_type(self, res_id=False, field_name=""):
  38. """
  39. JS 获取需要格式化的字段的类型和值
  40. :return:
  41. """
  42. res = self.browse(res_id)
  43. return res.ttype
  44. # return {
  45. # "id": res_id,
  46. # "value_type": res.ttype,
  47. # "value": res.value,
  48. # }
  49. def update_config(self, res_id=False, value=""):
  50. """
  51. 更新参数
  52. """
  53. # res = self.browse(res_id)
  54. app_config = (
  55. self.env["wecom.app_config"]
  56. .sudo()
  57. .search([("id", "=", res_id),])
  58. )
  59. app_config.sudo().write({"value": value})
  60. print(app_config.value)
  61. return app_config.value
  62. @api.model
  63. def get_param(self, app_id, key, default=False):
  64. """检索给定key的value
  65. :param string key: 要检索的参数值的键。
  66. :param string default: 如果缺少参数,则为默认值。
  67. :return: 参数的值, 如果不存在,则为 ``default``.
  68. :rtype: string
  69. """
  70. return self._get_param(app_id, key) or default
  71. def _get_param(self, app_id, key):
  72. params = self.search_read(
  73. [("app_id", "=", app_id), ("key", "=", key)],
  74. fields=["ttype", "value"],
  75. limit=1,
  76. )
  77. value = None
  78. ttype = None
  79. if not params:
  80. # 如果没有找到,搜索其他公司相同应用配置参数 进行复制
  81. copy_app_config = self.search([("key", "=", key)], limit=1)
  82. if copy_app_config:
  83. new_app_config = self.create(
  84. {
  85. "app_id": app_id,
  86. "name": copy_app_config.name,
  87. "key": copy_app_config.key,
  88. "ttype": copy_app_config.ttype,
  89. "value": copy_app_config.value,
  90. "description": copy_app_config.description,
  91. }
  92. )
  93. value = new_app_config.value
  94. ttype = new_app_config.ttype
  95. else:
  96. pass
  97. else:
  98. value = params[0]["value"]
  99. ttype = params[0]["ttype"]
  100. if ttype == "boolean":
  101. boolean_value = str(value).lower()
  102. if boolean_value in ["true", "yes", "t", "1"]:
  103. return True
  104. elif boolean_value in ["false", "no", "f", "0"]:
  105. return False
  106. else:
  107. return False
  108. return value if params else None
  109. @api.model
  110. def set_param(self, app_id, key, value):
  111. """设置参数的值。
  112. :param string key: 要设置的参数值的键。
  113. :param string value: 要设置的值。
  114. :return: 参数的上一个值,如果不存在,则为False。
  115. :rtype: string
  116. """
  117. param = self.search([("app_id", "=", app_id), ("key", "=", key)])
  118. if param:
  119. old = param.value
  120. if value is not False and value is not None:
  121. if str(value) != old:
  122. param.write({"value": value})
  123. else:
  124. param.unlink()
  125. return old
  126. else:
  127. if value is not False and value is not None:
  128. self.create({"app_id": app_id, "key": key, "value": value})
  129. return False
  130. @api.model_create_multi
  131. def create(self, vals_list):
  132. self.clear_caches()
  133. return super(WeComAppConfig, self).create(vals_list)
  134. def write(self, vals):
  135. self.clear_caches()
  136. return super(WeComAppConfig, self).write(vals)
  137. def unlink(self):
  138. self.clear_caches()
  139. return super(WeComAppConfig, self).unlink()
上海开阖软件有限公司 沪ICP备12045867号-1