forked from michel/Coworking
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
# -*- 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_id = fields.Many2one('coworking.coworker', 'Users', required=False, index=True)
|
|
date_start = fields.Datetime(default=fields.Date.context_today, required=True)
|
|
date_end = fields.Datetime(required=True)
|
|
|
|
duration = fields.Float(compute='_compute_duration')
|
|
description = fields.Text()
|
|
statut = fields.Selection([('draft', 'Draft'),
|
|
('confirmed', 'Confirmed'),
|
|
('canceled', 'Canceled')], default='draft')
|
|
|
|
participants_ids = fields.Many2many('coworking.coworker', string='Users')
|
|
|
|
|
|
@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'))
|
|
|
|
|
|
@api.depends('date_start', 'date_end')
|
|
def _compute_duration(self):
|
|
for event in self:
|
|
if event.date_start and event.date_end:
|
|
date_end_py = fields.Datetime.from_string(event.date_end)
|
|
date_start_py = fields.Datetime.from_string(event.date_start)
|
|
delta = date_end_py - date_start_py
|
|
event.duration = delta.days * 24.0 + round(float(delta.seconds) / 3600.0)
|
|
else:
|
|
event.duration = 0.0
|