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)
|
2017-11-14 13:43:06 +01:00
|
|
|
manager_id = fields.Many2one('coworking.coworker', 'Manager', index=True,
|
|
|
|
domain="[('coworker_type', 'in', ['staffer', 'member'])]")
|
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-13 16:29:34 +01:00
|
|
|
description = fields.Text()
|
2017-11-13 16:45:45 +01:00
|
|
|
statut = fields.Selection([('draft', 'Draft'),
|
|
|
|
('confirmed', 'Confirmed'),
|
|
|
|
('canceled', 'Canceled')], default='draft')
|
2017-11-10 15:03:12 +01:00
|
|
|
|
2017-11-14 13:43:06 +01:00
|
|
|
participants_ids = fields.Many2many('coworking.coworker', string='Subscribers')
|
|
|
|
participants_count = fields.Integer('Number of participants',
|
|
|
|
compute='_compute_participants_count')
|
2017-11-13 16:58:20 +01:00
|
|
|
|
2017-11-14 13:43:06 +01:00
|
|
|
@api.depends('participants_ids')
|
|
|
|
def _compute_participants_count(self):
|
|
|
|
""" Computes number of participants """
|
|
|
|
for event in self:
|
|
|
|
event.participants_count = len(event.participants_ids)
|
|
|
|
|
|
|
|
@api.constrains('statut', 'participants_ids')
|
|
|
|
def _check_if_confirmed(self):
|
|
|
|
"""Test si participants_ids est confirmed"""
|
|
|
|
for event in self:
|
|
|
|
if event.participants_ids and event.statut == 'draft':
|
|
|
|
raise models.ValidationError(_('You can have subscribed people '
|
|
|
|
'if event is not confirmed yet'))
|
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:
|
2017-11-13 16:29:34 +01:00
|
|
|
raise ValidationError(
|
|
|
|
_('Date most be supperior to to start date'))
|
2017-11-10 15:03:12 +01:00
|
|
|
|
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-13 16:29:34 +01:00
|
|
|
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
|
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
|