From 749094ea8a4c052a6332d3308a323699bf2f3519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 9 Feb 2015 23:02:56 +0100 Subject: [PATCH] Use TimeDelta field issue4026 review13821002 --- CHANGELOG | 2 + line.py | 56 ++++++++++++++++++++++------ view/hours_employee_graph.xml | 3 +- view/hours_employee_monthly_tree.xml | 2 +- view/hours_employee_tree.xml | 3 +- view/hours_employee_weekly_tree.xml | 2 +- view/line_form.xml | 4 +- view/line_tree.xml | 3 +- view/work_graph.xml | 3 +- view/work_tree2.xml | 2 +- work.py | 29 ++++++++++---- 11 files changed, 77 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ef139cd85e..0dc73cece3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Use TimeDelta field + Version 3.4.0 - 2014-10-20 * Bug fixes (see mercurial logs for details) diff --git a/line.py b/line.py index bb4cb69c17..f9a0e151c1 100644 --- a/line.py +++ b/line.py @@ -1,8 +1,10 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from __future__ import division +import datetime + from sql import Literal from sql.aggregate import Max, Sum -from sql.conditionals import Coalesce from sql.functions import Extract from trytond.model import ModelView, ModelSQL, fields @@ -28,7 +30,7 @@ class Line(ModelSQL, ModelView): ], depends=['company']) date = fields.Date('Date', required=True, select=True) - hours = fields.Float('Hours', digits=(16, 2), required=True) + duration = fields.TimeDelta('Duration', 'company_work_time', required=True) work = fields.Many2One('timesheet.work', 'Work', required=True, select=True, domain=[ ('company', '=', Eval('company', -1)), @@ -49,10 +51,10 @@ class Line(ModelSQL, ModelView): def __setup__(cls): super(Line, cls).__setup__() cls._order.insert(0, ('date', 'DESC')) - cls._sql_constraints += [ - ('check_move_hours_pos', - 'CHECK(hours >= 0.0)', 'Hours field must be positive'), - ] + cls._error_messages.update({ + 'duration_positive': ( + 'Duration of line "%(line)s" must be positive.'), + }) @classmethod def __register__(cls, module_name): @@ -68,12 +70,26 @@ class Line(ModelSQL, ModelView): super(Line, cls).__register__(module_name) + table = TableHandler(cursor, cls, module_name) + # Migration from 3.4: new company field if created_company: # Don't use FROM because SQLite nor MySQL support it. cursor.execute(*sql_table.update( [sql_table.company], [work.select(work.company, where=work.id == sql_table.work)])) + # Migration from 3.4: change hours into timedelta duration + if table.column_exist('hours'): + table.drop_constraint('check_move_hours_pos') + cursor.execute(*sql_table.select( + sql_table.id, sql_table.hours)) + for id_, hours in cursor.fetchall(): + duration = datetime.timedelta(hours=hours) + cursor.execute(*sql_table.update( + [sql_table.duration], + [duration], + where=sql_table.id == id_)) + table.drop_column('hours') @staticmethod def default_company(): @@ -105,6 +121,22 @@ class Line(ModelSQL, ModelView): employee = Employee(Transaction().context['employee']) return value + " (" + employee.rec_name + ")" + @classmethod + def validate(cls, lines): + super(Line, cls).validate(lines) + for line in lines: + line.check_duration() + + def check_duration(self): + if self.duration < datetime.timedelta(): + self.raise_user_error('duration_positive', { + 'line': self.rec_name, + }) + + @property + def hours(self): + return self.duration.total_seconds() / 60 / 60 + class EnterLinesStart(ModelView): 'Enter Lines' @@ -156,7 +188,7 @@ class HoursEmployee(ModelSQL, ModelView): 'Hours per Employee' __name__ = 'timesheet.hours_employee' employee = fields.Many2One('company.employee', 'Employee', select=True) - hours = fields.Float('Hours', digits=(16, 2)) + duration = fields.TimeDelta('Duration', 'company_work_time') @staticmethod def table_query(): @@ -175,7 +207,7 @@ class HoursEmployee(ModelSQL, ModelView): Max(line.write_uid).as_('write_uid'), Max(line.write_date).as_('write_date'), line.employee, - Sum(Coalesce(line.hours, 0)).as_('hours'), + Sum(line.duration).as_('duration'), where=where, group_by=line.employee) @@ -214,7 +246,7 @@ class HoursEmployeeWeekly(ModelSQL, ModelView): year = fields.Char('Year', select=True) week = fields.Integer('Week', select=True) employee = fields.Many2One('company.employee', 'Employee', select=True) - hours = fields.Float('Hours', digits=(16, 2), select=True) + duration = fields.TimeDelta('Duration', 'company_work_time') @classmethod def __setup__(cls): @@ -242,7 +274,7 @@ class HoursEmployeeWeekly(ModelSQL, ModelView): year_column, week_column, line.employee, - Sum(Coalesce(line.hours, 0)).as_('hours'), + Sum(line.duration).as_('duration'), group_by=(year_column, week_column, line.employee)) @@ -252,7 +284,7 @@ class HoursEmployeeMonthly(ModelSQL, ModelView): year = fields.Char('Year', select=True) month = fields.Integer('Month', select=True) employee = fields.Many2One('company.employee', 'Employee', select=True) - hours = fields.Float('Hours', digits=(16, 2), select=True) + duration = fields.TimeDelta('Duration', 'company_work_time') @classmethod def __setup__(cls): @@ -280,5 +312,5 @@ class HoursEmployeeMonthly(ModelSQL, ModelView): year_column, month_column, line.employee, - Sum(Coalesce(line.hours, 0)).as_('hours'), + Sum(line.duration).as_('duration'), group_by=(year_column, month_column, line.employee)) diff --git a/view/hours_employee_graph.xml b/view/hours_employee_graph.xml index c9ca574c79..324a228e35 100644 --- a/view/hours_employee_graph.xml +++ b/view/hours_employee_graph.xml @@ -6,7 +6,6 @@ this repository contains the full copyright notices and license terms. --> - + diff --git a/view/hours_employee_monthly_tree.xml b/view/hours_employee_monthly_tree.xml index b2350ed9db..a881e1acc3 100644 --- a/view/hours_employee_monthly_tree.xml +++ b/view/hours_employee_monthly_tree.xml @@ -5,5 +5,5 @@ this repository contains the full copyright notices and license terms. --> - + diff --git a/view/hours_employee_tree.xml b/view/hours_employee_tree.xml index 6df2f8ccdc..30701ccb26 100644 --- a/view/hours_employee_tree.xml +++ b/view/hours_employee_tree.xml @@ -3,6 +3,5 @@ this repository contains the full copyright notices and license terms. --> - + diff --git a/view/hours_employee_weekly_tree.xml b/view/hours_employee_weekly_tree.xml index d0335f2d01..c8f235669e 100644 --- a/view/hours_employee_weekly_tree.xml +++ b/view/hours_employee_weekly_tree.xml @@ -5,5 +5,5 @@ this repository contains the full copyright notices and license terms. --> - + diff --git a/view/line_form.xml b/view/line_form.xml index 36aa660d39..3e0df19bb6 100644 --- a/view/line_form.xml +++ b/view/line_form.xml @@ -8,8 +8,8 @@ this repository contains the full copyright notices and license terms. -->