michel_coworking/models/event.py

40 lines
1.3 KiB
Python
Raw Normal View History

2017-11-10 15:03:12 +01:00
# -*- coding: utf-8 -*-
""" Event module """
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class Event(models.Model):
""" Event model """
_name = 'coworking.event'
_description = 'Event model definition'
_order = 'id desc'
title = fields.Char(required=True)
# manager = fields.Many2one('coworker.users', 'Manager', required=True, index=True)
2017-11-10 17:08:56 +01:00
date_start = fields.Datetime(default=fields.Date.context_today, required=True)
date_end = fields.Datetime(required=True)
2017-11-10 15:03:12 +01:00
2017-11-10 15:59:48 +01:00
duration = fields.Float(compute='_compute_duration')
2017-11-10 15:03:12 +01:00
@api.constrains('date_end')
def _check_date_end(self):
"""Test si la modification de la date n'est pas postérieur à la date de début"""
if self.date_start > self.date_end:
raise ValidationError(_('Date most be supperior to to start date'))
2017-11-10 15:59:48 +01:00
@api.depends('date_start', 'date_end')
def _compute_duration(self):
for event in self:
2017-11-10 17:35:10 +01:00
if event.date_start and event.date_end:
2017-11-10 17:08:56 +01:00
t1 = fields.Datetime.from_string(event.date_end)
t2 = fields.Datetime.from_string(event.date_start)
2017-11-10 17:28:46 +01:00
delta = t1 - t2
2017-11-10 17:35:10 +01:00
event.duration = delta.days * 24.0 + round(float(delta.seconds) / 3600.0)
else:
event.duration = 0.0