etq_schedule.py 2.13 KB
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
import logging

# import requests
# from odoo.http import request
#
# # from datetime import datetime
# # from openerp.tools import html_escape as escape, ustr, image_resize_and_sharpen, image_save_for_web
# # import unicodedata
# # import re
_logger = logging.getLogger(__name__)


#
#
class EtqSchedule(models.Model):
    _name = "etq.schedule"
    _description = "Exam Schedule"
    exam_id = fields.Many2one('etq.exam', string="Exam", required=True)
    name = fields.Char(related='exam_id.name', store=True)
    start_time = fields.Datetime()
    end_time = fields.Datetime()
    members = fields.Many2many('res.partner', 'etq_schedule_partner_rel',
                               'schedule_id', 'partner_id', string="Members")
    active = fields.Boolean(default=True)
    duration = fields.Char(compute="_compute_duration", string="Duration")

    @api.depends('start_time', 'end_time')
    def _compute_duration(self):
        for row in self:
            if not row.end_time or not row.start_time:
                row.duration = ""
                continue
            duration = row.end_time - row.start_time
            res = duration.days and f'{duration.days} day(s)' or ""
            seconds = duration.seconds
            _logger.debug(f"Second: {seconds}")
            hour_flag = 60 * 60
            hours = int(seconds / hour_flag)
            seconds = seconds - hours * hour_flag
            minutes = int(seconds / 60)
            seconds = seconds - minutes * 60
            _logger.debug(f"Second: {seconds}")
            res += hours and f'{hours} hour(s) ' or ""
            res += minutes and f'{minutes} minute(s) ' or ""
            res += seconds and f'{seconds} second(s)' or ""
            row.duration = res

    @api.onchange('start_time', 'end_time')
    def time_change(self):
        if not self.end_time or not self.start_time:
            return
        if self.end_time < self.start_time:
            raise ValidationError(_("End time must be greater then start time"))
        self._compute_duration()