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.

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