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.

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