Coworking/coworking_event/wizard/coworking_event_weekly.py

64 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
""" Weekly massive event creation module """
2017-11-21 16:40:06 +01:00
import logging
from datetime import timedelta
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
_LOGGER = logging.getLogger(__name__)
class CoworkingEventWeekly(models.TransientModel):
""" Weekly massive event creation """
_name = 'coworking.event.weekly'
_description = 'Weekly massive event creation'
2017-11-21 16:40:06 +01:00
title = fields.Char(required=True)
manager_id = fields.Many2one('coworking.coworker', string='Manager',
required=True)
date_start_recurence = fields.Datetime('Date start', required=True)
date_stop_recurence = fields.Datetime('Date stop', required=True)
duration = fields.Float(required=True)
@api.constrains('date_start_recurence', 'date_stop_recurence')
def _check_dates(self):
""" Check date coherence """
for weekly in self:
if weekly.date_start_recurence > weekly.date_stop_recurence:
raise ValidationError(_('Please check you dates.'))
@api.multi
def weekly_create(self):
""" Create multiple events repeated weekly, according to dates """
self.ensure_one()
# 5 - Rediriger vers la liste des événements (en automatique)
# _LOGGER.warning()
date_start_py = fields.Datetime.from_string(self.date_start_recurence)
delta = timedelta(hours=self.duration)
date_stop_py = fields.Datetime.from_string(self.date_stop_recurence)
week_start = date_start_py.isocalendar()[1]
week_stop = date_stop_py.isocalendar()[1]
week_count = week_stop - week_start + 1
wdelta = timedelta(days=7)
for week_number in xrange(week_count):
if week_number == 0:
date_start = self.date_start_recurence
date_stop = fields.Datetime.to_string(date_start_py + delta)
else:
date_start = fields.Datetime.from_string(self.date_start_recurence) +\
wdelta * week_number
date_stop = fields.Datetime.to_string(date_start + delta)
date_start = fields.Datetime.to_string(date_start)
self.env['coworking.event'].create({
'title': self.title,
'date_start': date_start,
'date_end': date_stop,
'manager_id': self.manager_id.id
})
return {
'type': 'ir.actions.act_window',
'res_model': 'coworking.event',
'view_mode': 'tree'}