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.

201 lines
6.9KB

  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. class GithubTeam(models.Model):
  6. _name = "github.team"
  7. _inherit = ["abstract.github.model"]
  8. _order = "name"
  9. _description = "Github Team"
  10. _github_type = "team"
  11. _github_login_field = "slug"
  12. _PRIVACY_SELECTION = [("secret", "Secret"), ("closed", "Closed")]
  13. # Column Section
  14. organization_id = fields.Many2one(
  15. comodel_name="github.organization",
  16. string="Organization",
  17. required=True,
  18. index=True,
  19. readonly=True,
  20. ondelete="cascade",
  21. )
  22. name = fields.Char(string="Name", index=True, required=True, readonly=True)
  23. privacy = fields.Selection(
  24. string="Privacy",
  25. selection=_PRIVACY_SELECTION,
  26. readonly=True,
  27. default="secret",
  28. help="The level of privacy this team should have. Can be one of:\n"
  29. "* secret - only visible to organization owners and members of"
  30. " this team.\n"
  31. "* closed - visible to all members of this organization.",
  32. )
  33. parent_id = fields.Many2one(
  34. string="Parent Team", readonly=True, comodel_name="github.team"
  35. )
  36. partner_ids = fields.One2many(
  37. string="Members",
  38. comodel_name="github.team.partner",
  39. inverse_name="team_id",
  40. readonly=True,
  41. )
  42. partner_qty = fields.Integer(
  43. string="Number of Members", compute="_compute_partner_qty", store=True
  44. )
  45. repository_ids = fields.One2many(
  46. string="Repositories",
  47. comodel_name="github.team.repository",
  48. inverse_name="team_id",
  49. readonly=True,
  50. )
  51. repository_qty = fields.Integer(
  52. string="Number of Repositories", compute="_compute_repository_qty", store=True
  53. )
  54. description = fields.Char(string="Description", readonly=True)
  55. complete_name = fields.Char(
  56. string="Complete Name",
  57. readonly=True,
  58. compute="_compute_complete_name",
  59. store=True,
  60. )
  61. github_url = fields.Char(
  62. string="Github URL", compute="_compute_github_url", readonly=True
  63. )
  64. # Compute Section
  65. @api.depends("github_login", "organization_id.github_login")
  66. def _compute_github_url(self):
  67. for team in self:
  68. team.github_url = (
  69. "https://github.com/orgs/{organization_name}/"
  70. "teams/{team_name}".format(
  71. organization_name=team.organization_id.github_login,
  72. team_name=team.github_login,
  73. )
  74. )
  75. @api.depends("name", "organization_id.github_login")
  76. def _compute_complete_name(self):
  77. for team in self:
  78. team.complete_name = "{}/{}".format(
  79. team.organization_id.github_login, team.github_login
  80. )
  81. @api.depends("partner_ids")
  82. def _compute_partner_qty(self):
  83. for team in self:
  84. team.partner_qty = len(team.partner_ids)
  85. @api.depends("repository_ids")
  86. def _compute_repository_qty(self):
  87. for team in self:
  88. team.repository_qty = len(team.repository_ids)
  89. # Overloadable Section
  90. @api.model
  91. def get_conversion_dict(self):
  92. res = super().get_conversion_dict()
  93. res.update({"name": "name", "description": "description", "privacy": "privacy"})
  94. return res
  95. @api.model
  96. def get_odoo_data_from_github(self, data):
  97. organization_obj = self.env["github.organization"]
  98. res = super().get_odoo_data_from_github(data)
  99. if data.get("organization", False):
  100. organization_id = organization_obj.get_from_id_or_create(
  101. data["organization"]
  102. ).id
  103. else:
  104. organization_id = False
  105. res.update({"organization_id": organization_id})
  106. return res
  107. def get_github_data_from_odoo(self):
  108. self.ensure_one()
  109. return {
  110. "name": self.name,
  111. "description": self.description and self.description or "",
  112. "privacy": self.privacy,
  113. }
  114. def get_github_args_for_creation(self):
  115. self.ensure_one()
  116. return [self.organization_id.github_login]
  117. def full_update(self):
  118. self.button_sync_member()
  119. self.button_sync_repository()
  120. # Action Section
  121. def button_sync_member(self):
  122. partner_obj = self.env["res.partner"]
  123. connector_member = self.get_github_connector("team_members_member")
  124. connector_maintainer = self.get_github_connector("team_members_maintainer")
  125. for team in self:
  126. partner_data = []
  127. for data in connector_member.list([team.github_id_external]):
  128. partner = partner_obj.get_from_id_or_create(data)
  129. partner_data.append({"partner_id": partner.id, "role": "member"})
  130. for data in connector_maintainer.list([team.github_id_external]):
  131. partner = partner_obj.get_from_id_or_create(data)
  132. partner_data.append({"partner_id": partner.id, "role": "maintainer"})
  133. team.partner_ids = [(2, x.id, False) for x in team.partner_ids]
  134. team.partner_ids = [(0, False, x) for x in partner_data]
  135. def button_sync_repository(self):
  136. repository_obj = self.env["github.repository"]
  137. connector = self.get_github_connector("team_repositories")
  138. for team in self:
  139. repository_data = []
  140. for data in connector.list([team.github_id_external]):
  141. repository = repository_obj.get_from_id_or_create(data)
  142. if data["permissions"]["admin"] is True:
  143. permission = "admin"
  144. elif data["permissions"]["push"] is True:
  145. permission = "write"
  146. else:
  147. permission = "read"
  148. repository_data.append(
  149. {"repository_id": repository.id, "permission": permission}
  150. )
  151. team.repository_ids = [(2, x.id, False) for x in team.repository_ids]
  152. team.repository_ids = [(0, False, x) for x in repository_data]
  153. def action_github_team_partner_from_team(self):
  154. self.ensure_one()
  155. action = self.env.ref(
  156. "github_connector.action_github_team_partner_from_team"
  157. ).read()[0]
  158. action["context"] = dict(self.env.context)
  159. action["context"].pop("group_by", None)
  160. action["context"]["search_default_team_id"] = self.id
  161. return action
  162. def action_github_team_repository_from_team(self):
  163. self.ensure_one()
  164. action = self.env.ref(
  165. "github_connector.action_github_team_repository_from_team"
  166. ).read()[0]
  167. action["context"] = dict(self.env.context)
  168. action["context"].pop("group_by", None)
  169. action["context"]["search_default_team_id"] = self.id
  170. return action
上海开阖软件有限公司 沪ICP备12045867号-1