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.

182 lines
6.4KB

  1. # Copyright (C) 2016-Today: Odoo Community Association (OCA)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. from odoo.tools import html_sanitize
  6. class OdooModule(models.Model):
  7. _inherit = "abstract.action.mixin"
  8. _name = "odoo.module"
  9. _description = "Odoo Module"
  10. _order = "technical_name, name"
  11. # Column Section
  12. name = fields.Char(
  13. string="Name", store=True, readonly=True, compute="_compute_name"
  14. )
  15. technical_name = fields.Char(
  16. string="Technical Name", index=True, required=True, readonly=True
  17. )
  18. module_version_ids = fields.One2many(
  19. comodel_name="odoo.module.version",
  20. inverse_name="module_id",
  21. string="Versions",
  22. readonly=True,
  23. )
  24. module_version_qty = fields.Integer(
  25. string="Number of Module Versions",
  26. compute="_compute_module_version_qty",
  27. store=True,
  28. )
  29. author_ids = fields.Many2many(
  30. string="Authors",
  31. comodel_name="odoo.author",
  32. compute="_compute_author",
  33. relation="github_module_author_rel",
  34. column1="module_id",
  35. column2="author_id",
  36. store=True,
  37. )
  38. author_ids_description = fields.Char(
  39. string="Authors (Text)", compute="_compute_author", store=True
  40. )
  41. organization_serie_ids = fields.Many2many(
  42. string="Series",
  43. comodel_name="github.organization.serie",
  44. compute="_compute_organization_serie",
  45. store=True,
  46. relation="github_module_organization_serie_rel",
  47. column1="module_id",
  48. column2="organization_serie_id",
  49. )
  50. organization_serie_ids_description = fields.Char(
  51. string="Series (Text)", store=True, compute="_compute_organization_serie",
  52. )
  53. description_rst = fields.Char(
  54. string="RST Description of the last Version",
  55. store=True,
  56. readonly=True,
  57. compute="_compute_description",
  58. )
  59. description_rst_html = fields.Html(
  60. string="HTML of the RST Description of the last Version",
  61. store=True,
  62. readonly=True,
  63. compute="_compute_description",
  64. )
  65. dependence_module_version_ids = fields.Many2many(
  66. comodel_name="odoo.module.version",
  67. string="Module Versions that depend on this module",
  68. relation="module_version_dependency_rel",
  69. column1="dependency_module_id",
  70. column2="module_version_id",
  71. )
  72. dependence_module_version_qty = fields.Integer(
  73. string="Number of Module Versions that depend on this module",
  74. compute="_compute_dependence_module_version_qty",
  75. store=True,
  76. )
  77. image = fields.Binary(
  78. string="Icon Image", compute="_compute_image", store=True, attachment=True
  79. )
  80. # Compute Section
  81. @api.depends("module_version_ids.image")
  82. def _compute_image(self):
  83. module_version_obj = self.env["odoo.module.version"]
  84. for module in self:
  85. version_ids = module.module_version_ids.ids
  86. last_version = module_version_obj.search(
  87. [("id", "in", version_ids)], order="organization_serie_id desc", limit=1
  88. )
  89. module.image = last_version and last_version.image
  90. @api.depends("technical_name", "module_version_ids.name")
  91. def _compute_name(self):
  92. module_version_obj = self.env["odoo.module.version"]
  93. for module in self:
  94. version_ids = module.module_version_ids.ids
  95. last_version = module_version_obj.search(
  96. [("id", "in", version_ids)], order="organization_serie_id desc", limit=1
  97. )
  98. if last_version:
  99. module.name = last_version.name
  100. else:
  101. module.name = module.technical_name
  102. @api.depends("module_version_ids", "module_version_ids.description_rst_html")
  103. def _compute_description(self):
  104. module_version_obj = self.env["odoo.module.version"]
  105. for module in self:
  106. version_ids = module.module_version_ids.ids
  107. last_version = module_version_obj.search(
  108. [("id", "in", version_ids)], order="organization_serie_id desc", limit=1
  109. )
  110. if last_version:
  111. module.description_rst = last_version.description_rst
  112. module.description_rst_html = last_version.description_rst_html
  113. else:
  114. module.description_rst = ""
  115. module.description_rst_html = html_sanitize(
  116. "<h1 style='color:gray;'>" + _("No Version Found") + "</h1>"
  117. )
  118. @api.depends("dependence_module_version_ids.dependency_module_ids")
  119. def _compute_dependence_module_version_qty(self):
  120. for module in self:
  121. module.dependence_module_version_qty = len(
  122. module.dependence_module_version_ids
  123. )
  124. @api.depends("module_version_ids")
  125. def _compute_module_version_qty(self):
  126. for module in self:
  127. module.module_version_qty = len(module.module_version_ids)
  128. @api.depends("module_version_ids.author_ids")
  129. def _compute_author(self):
  130. for module in self:
  131. authors = []
  132. for version in module.module_version_ids:
  133. authors += version.author_ids
  134. authors = set(authors)
  135. module.author_ids = [x.id for x in authors]
  136. module.author_ids_description = ", ".join(sorted([x.name for x in authors]))
  137. @api.depends("module_version_ids.organization_serie_id")
  138. def _compute_organization_serie(self):
  139. for module in self:
  140. organization_series = []
  141. for version in module.module_version_ids:
  142. organization_series += version.organization_serie_id
  143. organization_series = set(organization_series)
  144. module.organization_serie_ids = [x.id for x in organization_series]
  145. module.organization_serie_ids_description = " - ".join(
  146. [x.name for x in sorted(organization_series, key=lambda x: x.sequence)]
  147. )
  148. # Custom Section
  149. @api.model
  150. def create_if_not_exist(self, technical_name):
  151. module = self.search([("technical_name", "=", technical_name)])
  152. if not module:
  153. module = self.create({"technical_name": technical_name})
  154. return module
  155. def name_get(self):
  156. return [(module.id, module.technical_name) for module in self]
上海开阖软件有限公司 沪ICP备12045867号-1