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.

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