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.

111 lines
3.7 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  2. import logging
  3. from odoo import api, fields, models, registry
  4. _logger = logging.getLogger(__name__)
  5. class AttachmentQueue(models.Model):
  6. _name = "attachment.queue"
  7. _inherits = {"ir.attachment": "attachment_id"}
  8. _inherit = ["mail.thread"]
  9. attachment_id = fields.Many2one(
  10. "ir.attachment",
  11. required=True,
  12. ondelete="cascade",
  13. help="Link to ir.attachment model ",
  14. )
  15. file_type = fields.Selection(
  16. selection=[],
  17. help="The file type determines an import method to be used "
  18. "to parse and transform data before their import in ERP or an export",
  19. )
  20. date_done = fields.Datetime()
  21. state = fields.Selection(
  22. [("pending", "Pending"), ("failed", "Failed"), ("done", "Done")],
  23. readonly=False,
  24. required=True,
  25. default="pending",
  26. )
  27. state_message = fields.Text()
  28. failure_emails = fields.Char(
  29. compute="_compute_failure_emails",
  30. string="Failure Emails",
  31. help="Comma-separated list of email addresses to be notified in case of"
  32. "failure",
  33. )
  34. def _compute_failure_emails(self):
  35. for attach in self:
  36. attach.failure_emails = attach._get_failure_emails()
  37. def _get_failure_emails(self):
  38. # to be overriden in submodules implementing the file_type
  39. self.ensure_one()
  40. return ""
  41. @api.model
  42. def run_attachment_queue_scheduler(self, domain=None):
  43. if domain is None:
  44. domain = [("state", "=", "pending")]
  45. batch_limit = self.env.ref(
  46. "attachment_queue.attachment_queue_cron_batch_limit"
  47. ).value
  48. if batch_limit and batch_limit.isdigit():
  49. limit = int(batch_limit)
  50. else:
  51. limit = 200
  52. attachments = self.search(domain, limit=limit)
  53. if attachments:
  54. return attachments.run()
  55. return True
  56. def run(self):
  57. """
  58. Run the process for each attachment queue
  59. """
  60. failure_tmpl = self.env.ref(
  61. "attachment_queue.attachment_failure_notification"
  62. )
  63. for attachment in self:
  64. with api.Environment.manage():
  65. with registry(self.env.cr.dbname).cursor() as new_cr:
  66. new_env = api.Environment(
  67. new_cr, self.env.uid, self.env.context
  68. )
  69. attach = attachment.with_env(new_env)
  70. try:
  71. attach._run()
  72. # pylint: disable=broad-except
  73. except Exception as e:
  74. attach.env.cr.rollback()
  75. _logger.exception(str(e))
  76. attach.write(
  77. {"state": "failed", "state_message": str(e)}
  78. )
  79. emails = attach.failure_emails
  80. if emails:
  81. failure_tmpl.send_mail(attach.id)
  82. attach.env.cr.commit()
  83. else:
  84. vals = {
  85. "state": "done",
  86. "date_done": fields.Datetime.now(),
  87. }
  88. attach.write(vals)
  89. attach.env.cr.commit()
  90. return True
  91. def _run(self):
  92. self.ensure_one()
  93. _logger.info("Starting processing of attachment queue id %d", self.id)
  94. def set_done(self):
  95. """
  96. Manually set to done
  97. """
  98. message = "Manually set to done by %s" % self.env.user.name
  99. self.write({"state_message": message, "state": "done"})