etq_schedule.py
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()