yaltik_odoo_custom/note_dates/models/note_dates.py

85 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Author: Fabien Bourgeois. Copyright Yaltik
# Copyright (C)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import timedelta, datetime
from openerp import fields, models, api, _
class Note(models.Model):
_inherit = 'note.note'
is_daily = fields.Boolean('Daily', default=False)
moment = fields.Selection([('now', _('Now')), ('morning', _('Morning')),
('afternoon', _('Afternoon')),
('night', _('Night'))], string='Moment')
date_start = fields.Datetime('Start')
date_stop = fields.Datetime('End')
tag_names = fields.Char('Tag Names', compute='_compute_tag_names')
@api.one
@api.depends('tag_ids')
def _compute_tag_names(self):
self.tag_names = u']['.join([t.name for t in self.tag_ids])
@api.onchange('is_daily')
def onchange_isdaily(self):
""" If event is daily, sets infinity """
for n in self:
if n.is_daily:
today = datetime.now().replace(hour=0, minute=0, second=0)
n.date_start = fields.Datetime.to_string(today)
n.date_stop = fields.Datetime.to_string(datetime(
day=1, month=1, year=2099))
@api.onchange('moment')
def onchange_moment(self):
""" According to moment, give good old defaults (warn: UTC) """
for n in self:
if n.moment:
nowstr = fields.Datetime.now()
now = fields.Datetime.from_string(nowstr)
if n.moment == 'now':
n.date_start = nowstr
n.date_stop = fields.Datetime.to_string(now +
timedelta(hours=1))
elif n.moment == 'morning':
db = now.replace(hour=6, minute=0, second=0)
n.date_start = fields.Datetime.to_string(db)
n.date_stop = fields.Datetime.to_string(db +
timedelta(hours=4))
elif n.moment == 'afternoon':
db = now.replace(hour=12, minute=30, second=0)
n.date_start = fields.Datetime.to_string(db)
n.date_stop = fields.Datetime.to_string(db +
timedelta(hours=4))
else:
delta = timedelta(days=1)
db = now + delta
db = db.replace(hour=3, minute=0, second=0)
n.date_start = fields.Datetime.to_string(db)
n.date_stop = fields.Datetime.to_string(db +
timedelta(hours=3))
@api.onchange('date_start')
def onchange_datestart(self):
for n in self:
if n.date_start and not n.date_stop:
dt = fields.Datetime.from_string(n.date_start)
dur = timedelta(hours=1)
n.date_stop = fields.Datetime.to_string(dt + dur)