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.

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