Use declarative index definition for ModelSQL

issue5757
review361251002
This commit is contained in:
Cédric Krier
2022-10-11 00:44:51 +02:00
parent b481201640
commit 1730195c8f
2 changed files with 32 additions and 8 deletions
+29 -6
View File
@@ -8,7 +8,7 @@ from sql.aggregate import Max, Sum
from sql.functions import Extract
from trytond.i18n import gettext
from trytond.model import ModelSQL, ModelView, Unique, fields
from trytond.model import Index, ModelSQL, ModelView, Unique, fields
from trytond.pool import Pool
from trytond.pyson import Date, Eval, PYSONEncoder
from trytond.transaction import Transaction
@@ -22,8 +22,8 @@ class Line(ModelSQL, ModelView):
__name__ = 'timesheet.line'
company = fields.Many2One('company.company', 'Company', required=True,
help="The company on which the time is spent.")
employee = fields.Many2One('company.employee', 'Employee', required=True,
select=True, domain=[
employee = fields.Many2One(
'company.employee', "Employee", required=True, domain=[
('company', '=', Eval('company', -1)),
['OR',
('start_date', '=', None),
@@ -35,11 +35,13 @@ class Line(ModelSQL, ModelView):
],
],
help="The employee who spends the time.")
date = fields.Date('Date', required=True, select=True,
date = fields.Date(
"Date", required=True,
help="When the time is spent.")
duration = fields.TimeDelta('Duration', 'company_work_time', required=True)
work = fields.Many2One('timesheet.work', 'Work',
required=True, select=True, domain=[
work = fields.Many2One(
'timesheet.work', "Work", required=True,
domain=[
('company', '=', Eval('company', -1)),
['OR',
('timesheet_start_date', '=', None),
@@ -67,6 +69,27 @@ class Line(ModelSQL, ModelView):
('uuid_unique', Unique(t, t.uuid),
'timesheet.msg_line_uuid_unique'),
]
cls._sql_indexes.update({
Index(
t,
(t.date, Index.Equality()),
(t.company, Index.Equality()),
(t.employee, Index.Equality())),
Index(
t,
(t.date, Index.Range()),
(t.employee, Index.Equality())),
Index(
t,
(Extract('YEAR', t.date), Index.Equality()),
(Extract('WEEK', t.date), Index.Equality()),
(t.employee, Index.Equality())),
Index(
t,
(Extract('YEAR', t.date), Index.Equality()),
(Extract('MONTH', t.date), Index.Equality()),
(t.employee, Index.Equality())),
})
@classmethod
def __register__(cls, module_name):
+3 -2
View File
@@ -48,8 +48,9 @@ class Work(ModelSQL, ModelView):
()),
],
help="Restrict adding lines after the date.")
company = fields.Many2One('company.company', 'Company', required=True,
select=True, help="Make the work belong to the company.")
company = fields.Many2One(
'company.company', "Company", required=True,
help="Make the work belong to the company.")
timesheet_lines = fields.One2Many('timesheet.line', 'work',
'Timesheet Lines',
help="Spend time on this work.")