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.

148 lines
5.2KB

  1. # Copyright (C) 2017-Today: Odoo Community Association (OCA)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. import os
  4. import tempfile
  5. import shutil
  6. import logging
  7. import base64
  8. import time
  9. from odoo.exceptions import ValidationError
  10. from odoo import models, fields, api, _
  11. _logger = logging.getLogger(__name__)
  12. class ProductProduct(models.Model):
  13. _inherit = "product.product"
  14. dependent_product_ids = fields.Many2many(
  15. "product.product",
  16. "product_product_dependent_rel",
  17. "src_id",
  18. "dest_id",
  19. string="Dependent Products",
  20. )
  21. module_path = fields.Char(
  22. related="odoo_module_version_id.repository_branch_id.local_path",
  23. readonly=True,
  24. )
  25. @api.constrains("dependent_product_ids")
  26. def check_dependent_recursion(self):
  27. if not self._check_m2m_recursion("dependent_product_ids"):
  28. raise ValidationError(
  29. _("Error: You cannot create recursive dependency.")
  30. )
  31. @api.model
  32. def child_dependency(self, children):
  33. res = self.env["product.product"]
  34. for child in children:
  35. if not child.dependent_product_ids:
  36. continue
  37. res |= child.dependent_product_ids
  38. res |= self.child_dependency(child.dependent_product_ids)
  39. return res
  40. def create_dependency_list(self):
  41. ret_val = {}
  42. for product in self:
  43. ret_val[product.id] = product.dependent_product_ids
  44. if product.dependent_product_ids:
  45. ret_val[product.id] |= self.child_dependency(
  46. product.dependent_product_ids
  47. )
  48. return ret_val
  49. def _get_module_path(self):
  50. return (
  51. self.module_path + "/" + self.odoo_module_version_id.technical_name
  52. )
  53. def generate_zip_file(self):
  54. for product in self.filtered("module_path"):
  55. tmp_dir = tempfile.mkdtemp()
  56. tmp_dir_2 = tempfile.mkdtemp()
  57. dependent_products = product.create_dependency_list()
  58. dependent_products = dependent_products[product.id]
  59. for dependent_pro in dependent_products.filtered("module_path"):
  60. tmp_module_path = os.path.join(
  61. tmp_dir,
  62. dependent_pro.odoo_module_version_id.technical_name,
  63. )
  64. shutil.copytree(
  65. dependent_pro.module_path
  66. + "/"
  67. + dependent_pro.odoo_module_version_id.technical_name,
  68. tmp_module_path,
  69. )
  70. tmp_module_path = os.path.join(
  71. tmp_dir, product.odoo_module_version_id.technical_name
  72. )
  73. module_path = product._get_module_path()
  74. shutil.copytree(module_path, tmp_module_path)
  75. time_version_value = time.strftime("_%y%m%d_%H%M%S")
  76. if product.attribute_value_ids:
  77. time_version_value = "_%s%s" % (
  78. "_".join(
  79. [
  80. name.replace(".", "_")
  81. for name in product.attribute_value_ids.mapped(
  82. "name"
  83. )
  84. ]
  85. ),
  86. time_version_value,
  87. )
  88. tmp_zip_file = (
  89. os.path.join(tmp_dir_2, product.name) + time_version_value
  90. )
  91. shutil.make_archive(tmp_zip_file, "zip", tmp_dir)
  92. tmp_zip_file = "%s.zip" % tmp_zip_file
  93. with open(tmp_zip_file, "rb") as file_obj:
  94. try:
  95. data_encode = base64.encodestring(file_obj.read())
  96. self.env["ir.attachment"].create(
  97. {
  98. "datas": data_encode,
  99. "datas_fname": (
  100. product.name.replace(" ", "-")
  101. + time_version_value
  102. + ".zip"
  103. ),
  104. "type": "binary",
  105. "name": product.name + time_version_value + ".zip",
  106. "res_model": product._name,
  107. "res_id": product.id,
  108. "product_downloadable": True,
  109. }
  110. )
  111. except Exception as exc:
  112. _logger.error(
  113. "Error creating attachment %s Error is: %s"
  114. % (tmp_zip_file, exc.message)
  115. )
  116. try:
  117. shutil.rmtree(tmp_dir)
  118. except OSError as exc:
  119. _logger.warning(
  120. "Could not remove Tempdir %s, Errormsg %s"
  121. % (tmp_dir, exc.message)
  122. )
  123. try:
  124. shutil.rmtree(tmp_dir_2)
  125. except OSError as exc:
  126. _logger.warning(
  127. "Could not remove Tempdir 2 %s, Errormsg %s"
  128. % (tmp_dir, exc.message)
  129. )
  130. @api.model
  131. def generate_zip_file_batch(self):
  132. self.search([]).generate_zip_file()
上海开阖软件有限公司 沪ICP备12045867号-1