From 350efce581f33ba2d537ae579cf214bf99630c3b Mon Sep 17 00:00:00 2001 From: eloyoussef Date: Fri, 9 Mar 2018 17:46:00 +0100 Subject: [PATCH] =?UTF-8?q?remplacement=20de=20date=20par=20datetime=20et?= =?UTF-8?q?=20mise=20=C3=A0=20jours=20de=20la=20fonction=20de=20calcul=20d?= =?UTF-8?q?e=20date=5Fstop=20on=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/golem_resource_reservation.py | 97 +++++++++++-------- .../golem_resource_reservation_views.xml | 10 +- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/golem_resource/models/golem_resource_reservation.py b/golem_resource/models/golem_resource_reservation.py index c43d64c..a7f153e 100644 --- a/golem_resource/models/golem_resource_reservation.py +++ b/golem_resource/models/golem_resource_reservation.py @@ -19,8 +19,11 @@ """ GOLEM Resource Reservation """ from math import modf +from datetime import timedelta from odoo import models, fields, api, _ from odoo.exceptions import ValidationError +import logging +_logger = logging.getLogger(__name__) class GolemResourceReservation(models.Model): @@ -28,18 +31,24 @@ class GolemResourceReservation(models.Model): _name = 'golem.resource.reservation' _description = 'GOLEM Reservation Model' _inherit = 'mail.thread' - _order = 'date desc,hour_start asc' + _order = 'date_start desc' name = fields.Char(compute='_compute_name', store=True) # TODO: handle multiple days reservation - date = fields.Date(required=True, index=True, readonly=True, - states={'draft': [('readonly', False)]}) - hour_start = fields.Float('Start hour', required=True, readonly=True, - states={'draft': [('readonly', False)]}) - hour_stop = fields.Float('Stop hour', required=True, readonly=True, - states={'draft': [('readonly', False)]}) - date_start = fields.Datetime(compute='_compute_date_start', store=True, index=True) - date_stop = fields.Datetime(compute='_compute_date_stop', store=True, index=True) + date_start = fields.Datetime('Start date',required=True, + index=True, readonly=True, + states={'draft': [('readonly', False)]}) + date_stop = fields.Datetime('Stop date', required=True, + index=True, readonly=True, + states={'draft': [('readonly', False)]}) + #date = fields.Date(required=True, index=True, readonly=True, + # states={'draft': [('readonly', False)]}) + #hour_start = fields.Float('Start hour', required=True, readonly=True, + # states={'draft': [('readonly', False)]}) + #hour_stop = fields.Float('Stop hour', required=True, readonly=True, + # states={'draft': [('readonly', False)]}) + #date_start = fields.Datetime(compute='_compute_date_start', store=True, index=True) + #date_stop = fields.Datetime(compute='_compute_date_stop', store=True, index=True) resource_id = fields.Many2one('golem.resource', required=True, index=True, string='Resource', readonly=True, @@ -69,46 +78,48 @@ class GolemResourceReservation(models.Model): resource_reservation_count = fields.Integer(compute='_reservation_count') - @api.depends('resource_id', 'date') + @api.depends('resource_id', 'date_start') def _compute_name(self): """ Computes reservation name """ for reservation in self: reservation.name = u'{}/{}'.format(reservation.resource_id.name, - reservation.date) + reservation.date_start) - @api.depends('date', 'hour_start') - def _compute_date_start(self): - """ Computes Date start """ - for reservation in self: - minute_start, hour_start = modf(reservation.hour_start) - hour_start = int(hour_start) - minute_start = int(round(minute_start * 60)) - reservation.date_start = u'{} {}:{}:00'.format(reservation.date, - hour_start, minute_start) + #@api.depends('date', 'hour_start') + #def _compute_date_start(self): + # """ Computes Date start """ + # for reservation in self: + # minute_start, hour_start = modf(reservation.hour_start) + # hour_start = int(hour_start) + # minute_start = int(round(minute_start * 60)) + # reservation.date_start = u'{} {}:{}:00'.format(reservation.date, + # hour_start, minute_start) - @api.depends('date', 'hour_stop') - def _compute_date_stop(self): - """ Computes Date stop """ - for reservation in self: - minute_stop, hour_stop = modf(reservation.hour_stop) - hour_stop = int(hour_stop) - minute_stop = int(round(minute_stop * 60)) - reservation.date_stop = u'{} {}:{}:00'.format(reservation.date, - hour_stop, minute_stop) + #@api.depends('date', 'hour_stop') + #def _compute_date_stop(self): + # """ Computes Date stop """ + # for reservation in self: + # minute_stop, hour_stop = modf(reservation.hour_stop) + # hour_stop = int(hour_stop) + # minute_stop = int(round(minute_stop * 60)) + # reservation.date_stop = u'{} {}:{}:00'.format(reservation.date, + # hour_stop, minute_stop) - @api.onchange('hour_start') - def onchange_hour_start(self): - """ Propose automatically stop hour after start hour had been filled """ + @api.onchange('date_start') + def onchange_date_start(self): + """ Propose automatically stop date after start date had been filled """ for reservation in self: - if reservation.hour_start and not reservation.hour_stop: - reservation.hour_stop = reservation.hour_start + 1 + if reservation.date_start: + start = fields.Datetime.from_string(reservation.date_start) + duration = timedelta(hours=1) + reservation.date_stop = start + duration - @api.constrains('hour_start', 'hour_stop') - def _check_hour_consistency(self): - """ Checks hour consistency """ + @api.constrains('date_start', 'date_stop') + def _check_date_consistency(self): + """ Checks date consistency """ for reservation in self: - if reservation.hour_stop <= reservation.hour_start: - raise ValidationError(_('End time should be after than start time')) + if reservation.date_stop <= reservation.date_start: + raise ValidationError(_('Stop date should be after start date')) @api.multi def state_draft(self): @@ -157,10 +168,11 @@ class GolemResourceReservation(models.Model): verr = _('You do not have permissions to validate or reject a reservation.') raise ValidationError(verr) + """ @api.constrains('state') - def check_confirmed(self): - """ Check date coherence on reservation confirmation """ - for reservation in self: + def check_confirmed(self):""" + """ Check date coherence on reservation confirmation """ + """" for reservation in self: if reservation.state == 'confirmed': # Check is reservation is not taking place out of the resource avaibility period if reservation.date < reservation.resource_id.avaibility_start or \ @@ -203,6 +215,7 @@ class GolemResourceReservation(models.Model): 'please choose another périod before confirming.') raise ValidationError(verr.format(other_res.date_start, other_res.date_stop)) + """ @api.multi def reservation_calendar(self): """ current resource reservation calendar """ diff --git a/golem_resource/views/golem_resource_reservation_views.xml b/golem_resource/views/golem_resource_reservation_views.xml index 11fd998..70a3142 100644 --- a/golem_resource/views/golem_resource_reservation_views.xml +++ b/golem_resource/views/golem_resource_reservation_views.xml @@ -39,9 +39,8 @@ along with this program. If not, see . - - - + + @@ -91,9 +90,8 @@ along with this program. If not, see . - - - + +