2017-11-10 15:03:12 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
""" Event module """
|
|
|
|
|
2017-11-14 17:19:02 +01:00
|
|
|
from datetime import datetime
|
2017-11-10 15:03:12 +01:00
|
|
|
from odoo import models, fields, api, _
|
|
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
|
|
class Event(models.Model):
|
|
|
|
""" Event model """
|
|
|
|
_name = 'coworking.event'
|
|
|
|
_description = 'Event model definition'
|
2017-11-15 15:32:42 +01:00
|
|
|
_rec_name = 'title'
|
2017-11-15 11:59:12 +01:00
|
|
|
_order = 'id asc'
|
2017-11-10 15:03:12 +01:00
|
|
|
|
2017-11-15 15:20:49 +01:00
|
|
|
title = fields.Char(required=True)
|
|
|
|
|
2017-11-27 12:02:39 +01:00
|
|
|
manager_id = fields.Many2one('res.partner', 'Manager', index=True, required=True,
|
2017-11-14 13:43:06 +01:00
|
|
|
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)
|
2017-11-14 17:19:02 +01:00
|
|
|
starts_this_week = fields.Integer(compute='_compute_starts_this_week',
|
|
|
|
search='_search_starts_this_week')
|
2017-11-10 17:08:56 +01:00
|
|
|
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'),
|
2017-11-15 06:25:12 +01:00
|
|
|
('canceled', 'Canceled')], default='draft')
|
2017-11-10 15:03:12 +01:00
|
|
|
|
2017-11-27 12:02:39 +01:00
|
|
|
participants_ids = fields.Many2many('res.partner', string='Subscribers')
|
2017-11-14 13:43:06 +01:00
|
|
|
participants_count = fields.Integer('Number of participants',
|
|
|
|
compute='_compute_participants_count')
|
2017-11-14 17:19:02 +01:00
|
|
|
|
2017-11-15 11:53:14 +01:00
|
|
|
|
|
|
|
|
2017-11-14 17:19:02 +01:00
|
|
|
@api.depends('date_start')
|
|
|
|
def _compute_starts_this_week(self):
|
|
|
|
""" Computes is event starts this week """
|
|
|
|
for event in self:
|
|
|
|
date_start = fields.Datetime.from_string(event.date_start)
|
|
|
|
week_start = date_start.isocalendar()[1]
|
|
|
|
event.starts_this_week = (datetime.now().isocalendar()[1] == week_start)
|
|
|
|
|
|
|
|
def _search_starts_this_week(self, operator, value):
|
|
|
|
""" Searches function for starts_this_week """
|
|
|
|
res_ids = []
|
|
|
|
for event in self.search([]):
|
|
|
|
date_start = fields.Datetime.from_string(event.date_start)
|
|
|
|
week_start = date_start.isocalendar()[1]
|
|
|
|
if datetime.now().isocalendar()[1] == week_start:
|
|
|
|
res_ids.append(event.id)
|
2017-11-15 06:25:12 +01:00
|
|
|
if operator == '=':
|
|
|
|
operator = 'in'if value else 'not in'
|
|
|
|
else:
|
|
|
|
operator = 'not in'if value else 'in'
|
|
|
|
return [('id', operator, res_ids)]
|
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
|
|
|
|
2017-11-15 15:03:12 +01:00
|
|
|
@api.constrains('date_start', 'date_end')
|
|
|
|
def _check_dates(self):
|
|
|
|
"""Test si la modification de la date de début est infnérieure à la date de fin"""
|
2017-11-10 15:03:12 +01:00
|
|
|
if self.date_start > self.date_end:
|
2017-11-15 15:03:12 +01:00
|
|
|
raise ValidationError(_('End 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-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
|