90 lines
3.6 KiB
Python
90 lines
3.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
""" Event module """
|
|
|
|
from datetime import datetime
|
|
from odoo import models, fields, api, _
|
|
from odoo.exceptions import ValidationError
|
|
|
|
class Event(models.Model):
|
|
""" Event model """
|
|
_name = 'coworking.event'
|
|
_description = 'Event model definition'
|
|
_rec_name = 'title'
|
|
_order = 'id asc'
|
|
|
|
title = fields.Char(required=True)
|
|
|
|
manager_id = fields.Many2one('res.partner', 'Manager', index=True, required=True,
|
|
domain="[('coworker_type', 'in', ['staffer', 'member'])]")
|
|
date_start = fields.Datetime(default=fields.Date.context_today, required=True)
|
|
starts_this_week = fields.Integer(compute='_compute_starts_this_week',
|
|
search='_search_starts_this_week')
|
|
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('res.partner', string='Subscribers')
|
|
participants_count = fields.Integer('Number of participants',
|
|
compute='_compute_participants_count')
|
|
|
|
|
|
|
|
@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)
|
|
if operator == '=':
|
|
operator = 'in'if value else 'not in'
|
|
else:
|
|
operator = 'not in'if value else 'in'
|
|
return [('id', operator, res_ids)]
|
|
|
|
@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'))
|
|
|
|
@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"""
|
|
for event in self:
|
|
if event.date_start > event.date_end:
|
|
raise ValidationError(_('End 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
|