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.

63 lines
2.1 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from datetime import datetime
  6. from openerp import _, api, fields, models
  7. from openerp.exceptions import Warning as UserError
  8. class IrCronInactivityPeriod(models.Model):
  9. _name = 'ir.cron.inactivity.period'
  10. _SELECTION_TYPE = [
  11. ('hour', 'Hour'),
  12. ]
  13. cron_id = fields.Many2one(
  14. comodel_name='ir.cron', ondelete='cascade', required=True)
  15. type = fields.Selection(
  16. string='Type', selection=_SELECTION_TYPE,
  17. required=True, default='hour')
  18. inactivity_hour_begin = fields.Float(
  19. string='Begin Hour', default=0)
  20. inactivity_hour_end = fields.Float(
  21. string='End Hour', default=1)
  22. @api.constrains('inactivity_hour_begin', 'inactivity_hour_end')
  23. def _check_activity_hour(self):
  24. for period in self:
  25. if period.inactivity_hour_begin >= period.inactivity_hour_end:
  26. raise UserError(_(
  27. "The End Hour should be greater than the Begin Hour"))
  28. @api.multi
  29. def _check_inactivity_period(self):
  30. res = []
  31. for period in self:
  32. res.append(period._check_inactivity_period_one())
  33. return res
  34. @api.multi
  35. def _check_inactivity_period_one(self):
  36. self.ensure_one()
  37. now = fields.Datetime.context_timestamp(self, datetime.now())
  38. if self.type == 'hour':
  39. begin_inactivity = now.replace(
  40. hour=int(self.inactivity_hour_begin),
  41. minute=int((self.inactivity_hour_begin % 1) * 60),
  42. second=0)
  43. end_inactivity = now.replace(
  44. hour=int(self.inactivity_hour_end),
  45. minute=int((self.inactivity_hour_end % 1) * 60),
  46. second=0)
  47. return now >= begin_inactivity and now < end_inactivity
  48. else:
  49. raise UserError(
  50. _("Unimplemented Feature: Inactivity Period type '%s'") % (
  51. self.type))