Use declarative index definition for ModelSQL

issue5757
review361251002
This commit is contained in:
Cédric Krier
2022-10-11 00:44:49 +02:00
parent 72dfa8667d
commit f3d89d0a35
2 changed files with 23 additions and 11 deletions
+14 -7
View File
@@ -10,7 +10,7 @@ from sql.conditionals import Coalesce
from trytond import backend
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, ModelSQL, ModelView, Unique, fields, tree)
DeactivableMixin, Index, ModelSQL, ModelView, Unique, fields, tree)
from trytond.modules.currency.fields import Monetary
from trytond.pool import Pool
from trytond.pyson import Eval, If, PYSONDecoder, PYSONEncoder
@@ -25,8 +25,8 @@ class Account(
ModelSQL, ModelView):
'Analytic Account'
__name__ = 'analytic_account.account'
name = fields.Char('Name', required=True, translate=True, select=True)
code = fields.Char('Code', select=True)
name = fields.Char("Name", required=True, translate=True)
code = fields.Char("Code")
company = fields.Many2One('company.company', 'Company', required=True)
currency = fields.Function(
fields.Many2One('currency.currency', 'Currency'),
@@ -37,7 +37,8 @@ class Account(
('normal', 'Normal'),
('distribution', 'Distribution'),
], 'Type', required=True)
root = fields.Many2One('analytic_account.account', 'Root', select=True,
root = fields.Many2One(
'analytic_account.account', "Root",
domain=[
('company', '=', Eval('company', -1)),
('parent', '=', None),
@@ -47,7 +48,8 @@ class Account(
'invisible': Eval('type') == 'root',
'required': Eval('type') != 'root',
})
parent = fields.Many2One('analytic_account.account', 'Parent', select=True,
parent = fields.Many2One(
'analytic_account.account', "Parent",
domain=['OR',
('root', '=', Eval('root', -1)),
('parent', '=', None),
@@ -91,7 +93,11 @@ class Account(
@classmethod
def __setup__(cls):
cls.code.search_unaccented = False
super(Account, cls).__setup__()
t = cls.__table__()
cls._sql_indexes.add(
Index(t, (t.code, Index.Similarity())))
cls._order.insert(0, ('code', 'ASC'))
cls._order.insert(1, ('name', 'ASC'))
@@ -308,7 +314,7 @@ class AccountDistribution(ModelView, ModelSQL):
"Analytic Account Distribution"
__name__ = 'analytic_account.account.distribution'
parent = fields.Many2One(
'analytic_account.account', "Parent", required=True, select=True)
'analytic_account.account', "Parent", required=True)
root = fields.Function(
fields.Many2One('analytic_account.account', "Root"),
'on_change_with_root')
@@ -337,7 +343,7 @@ class AccountDistribution(ModelView, ModelSQL):
class AnalyticAccountEntry(ModelView, ModelSQL):
'Analytic Account Entry'
__name__ = 'analytic.account.entry'
origin = fields.Reference('Origin', selection='get_origin', select=True)
origin = fields.Reference('Origin', selection='get_origin')
root = fields.Many2One(
'analytic_account.account', "Root Analytic", required=True,
domain=[
@@ -398,6 +404,7 @@ class AnalyticAccountEntry(ModelView, ModelSQL):
('root_origin_uniq', Unique(t, t.origin, t.root),
'analytic_account.msg_root_origin_unique'),
]
cls._sql_indexes.add(Index(t, (t.origin, Index.Equality())))
@classmethod
def _get_origin(cls):
+9 -4
View File
@@ -6,7 +6,7 @@ from itertools import groupby
from sql import Literal
from trytond.model import Check, ModelSQL, ModelView, fields
from trytond.model import Check, Index, ModelSQL, ModelView, fields
from trytond.modules.currency.fields import Monetary
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, PYSONEncoder
@@ -26,8 +26,9 @@ class Line(ModelSQL, ModelView):
'on_change_with_currency')
company = fields.Function(fields.Many2One('company.company', 'Company'),
'on_change_with_company', searcher='search_company')
account = fields.Many2One('analytic_account.account', 'Account',
required=True, select=True, domain=[
account = fields.Many2One(
'analytic_account.account', "Account", required=True,
domain=[
('type', 'not in', ['view', 'distribution']),
['OR',
('company', '=', None),
@@ -47,6 +48,10 @@ class Line(ModelSQL, ModelView):
Check(t, t.credit * t.debit == 0),
'account.msg_line_debit_credit'),
]
cls._sql_indexes.update({
Index(t, (t.account, Index.Equality())),
Index(t, (t.date, Index.Range())),
})
cls._order.insert(0, ('date', 'ASC'))
@classmethod
@@ -175,7 +180,7 @@ class MoveLine(ModelSQL, ModelView):
analytic_state = fields.Selection([
('draft', 'Draft'),
('valid', 'Valid'),
], "Analytic State", readonly=True, select=True, sort=False)
], "Analytic State", readonly=True, sort=False)
@classmethod
def __setup__(cls):