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.

57 lines
1.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from openerp.tools.translate import _
  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.Char(string="Launch time", compute='_run_time', store=False)
  11. @classmethod
  12. def _update_time(cls):
  13. cls._last_updated = datetime.datetime.now()
  14. @api.one
  15. def _run_time(self):
  16. if self._last_updated:
  17. self.run_time = str(int((datetime.datetime.now() - self._last_updated).total_seconds() / 60))
  18. else:
  19. self._last_updated = datetime.datetime.now()
  20. self.run_time = '0'
  21. @api.model
  22. def _fetch_mails(self):
  23. if self._context.get('run_fetchmail_manually'):
  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. self._update_time()
  28. class FetchMailImmediately(models.AbstractModel):
  29. _name = 'fetch_mail.imm'
  30. @api.model
  31. def get_last_update_time(self):
  32. return self.env['fetchmail.server'].sudo().search([]).run_time
  33. @api.model
  34. def run_fetchmail_manually(self):
  35. fetchmail_task = self.env.ref('fetchmail.ir_cron_mail_gateway_action')
  36. fetchmail_task_id = fetchmail_task.id
  37. fetchmail_model = self.env['fetchmail.server'].sudo()
  38. fetchmail_task._try_lock()
  39. fetchmail_model.with_context(run_fetchmail_manually=True)._fetch_mails()