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.

80 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. import datetime
  2. from openerp import api, exceptions, fields, models, tools
  3. from openerp.tools.translate import _
  4. class FetchMailServer(models.Model):
  5. _inherit = "fetchmail.server"
  6. _name = "fetchmail.server"
  7. _last_updated = None
  8. run_time = fields.Datetime(string="Launch time")
  9. def _run_time(self):
  10. if not self._last_updated:
  11. self._last_updated = tools.datetime.now()
  12. src_tstamp_str = self._last_updated.strftime(
  13. tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
  14. )
  15. src_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
  16. dst_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
  17. dst_tz_name = self._context.get("tz") or self.env.user.tz
  18. _now = tools.misc.server_to_local_timestamp(
  19. src_tstamp_str, src_format, dst_format, dst_tz_name
  20. )
  21. return _now
  22. @api.model
  23. def _fetch_mails(self):
  24. if self._context.get("run_fetchmail_manually"):
  25. # if interval less than 5 seconds
  26. if self._last_updated and (
  27. datetime.datetime.now() - self._last_updated
  28. ) < datetime.timedelta(0, 5):
  29. raise exceptions.Warning(
  30. _("Error"), _("Task can be started no earlier than 5 seconds.")
  31. )
  32. super(FetchMailServer, self)._fetch_mails()
  33. res = (
  34. self.env["fetchmail.server"]
  35. .sudo()
  36. .with_context(tz=self.env.user.tz)
  37. .search([("state", "=", "done")])
  38. )
  39. if res:
  40. res[0].run_time = self._run_time()
  41. class FetchMailImmediately(models.AbstractModel):
  42. _name = "fetch_mail.imm"
  43. @api.model
  44. def get_last_update_time(self):
  45. res = (
  46. self.env["fetchmail.server"]
  47. .sudo()
  48. .with_context(tz=self.env.user.tz)
  49. .search([("state", "=", "done")])
  50. )
  51. array = [r.run_time for r in res]
  52. if array:
  53. return array[0]
  54. else:
  55. return None
  56. @api.model
  57. def run_fetchmail_manually(self):
  58. fetchmail_task = self.env.ref("fetchmail.ir_cron_mail_gateway_action")
  59. fetchmail_model = self.env["fetchmail.server"].sudo()
  60. fetchmail_task._try_lock()
  61. fetchmail_model.with_context(run_fetchmail_manually=True)._fetch_mails()