changeset a246d4950c8f in modules/purchase:default
details: https://hg.tryton.org/modules/purchase?cmd=changeset&node=a246d4950c8f
description:
        Use declarative index definition for ModelSQL

        issue5757
        review361251002
diffstat:

 party.py    |   6 ++--
 product.py  |  22 ++++++++++-----
 purchase.py |  84 ++++++++++++++++++++++++++++++++++++++----------------------
 3 files changed, 71 insertions(+), 41 deletions(-)

diffs (273 lines):

diff -r b39d7b2b20d1 -r a246d4950c8f party.py
--- a/party.py  Mon Oct 10 18:27:32 2022 +0200
+++ b/party.py  Tue Oct 11 00:44:50 2022 +0200
@@ -35,7 +35,7 @@
     "Party Customer Code"
     __name__ = 'party.party.customer_code'
     party = fields.Many2One(
-        'party.party', "Party", ondelete='CASCADE', select=True,
+        'party.party', "Party", ondelete='CASCADE',
         context={
             'company': Eval('company', -1),
             },
@@ -47,7 +47,7 @@
     "Supplier Lead Time"
     __name__ = 'party.party.supplier_lead_time'
     party = fields.Many2One(
-        'party.party', "Party", ondelete='CASCADE', select=True,
+        'party.party', "Party", ondelete='CASCADE',
         context={
             'company': Eval('company', -1),
             },
@@ -59,7 +59,7 @@
     "Party Supplier Currency"
     __name__ = 'party.party.supplier_currency'
     party = fields.Many2One(
-        'party.party', "Party", ondelete='CASCADE', select=True)
+        'party.party', "Party", ondelete='CASCADE')
     supplier_currency = supplier_currency
 
 
diff -r b39d7b2b20d1 -r a246d4950c8f product.py
--- a/product.py        Mon Oct 10 18:27:32 2022 +0200
+++ b/product.py        Tue Oct 11 00:44:50 2022 +0200
@@ -7,7 +7,7 @@
 
 from trytond.i18n import gettext
 from trytond.model import (
-    DeactivableMixin, MatchMixin, ModelSQL, ModelView, fields,
+    DeactivableMixin, Index, MatchMixin, ModelSQL, ModelView, fields,
     sequence_ordered)
 from trytond.modules.currency.fields import Monetary
 from trytond.modules.product import price_digits
@@ -249,7 +249,7 @@
     __name__ = 'purchase.product_supplier'
     template = fields.Many2One(
         'product.template', "Product",
-        required=True, ondelete='CASCADE', select=True,
+        required=True, ondelete='CASCADE',
         domain=[
             If(Bool(Eval('product')),
                 ('products', '=', Eval('product')),
@@ -261,7 +261,7 @@
             },
         depends={'company'})
     product = fields.Many2One(
-        'product.product', "Variant", select=True,
+        'product.product', "Variant",
         domain=[
             If(Bool(Eval('template')),
                 ('template', '=', Eval('template')),
@@ -274,18 +274,17 @@
         depends={'company'})
     party = fields.Many2One(
         'party.party', 'Supplier', required=True, ondelete='CASCADE',
-        select=True,
         context={
             'company': Eval('company', -1),
             },
         depends={'company'})
-    name = fields.Char('Name', size=None, translate=True, select=True)
-    code = fields.Char('Code', size=None, select=True)
+    name = fields.Char("Name", translate=True)
+    code = fields.Char("Code")
     prices = fields.One2Many('purchase.product_supplier.price',
             'product_supplier', 'Prices')
     company = fields.Many2One(
         'company.company', "Company",
-        required=True, ondelete='CASCADE', select=True)
+        required=True, ondelete='CASCADE')
     lead_time = fields.TimeDelta('Lead Time',
         help="The time from confirming the purchase order to receiving the "
         "products.\n"
@@ -296,6 +295,15 @@
         fields.Many2One('product.uom', "UOM"), 'on_change_with_uom')
 
     @classmethod
+    def __setup__(cls):
+        cls.code.search_unaccented = False
+        super().__setup__()
+        t = cls.__table__()
+        cls._sql_indexes.update({
+                Index(t, (t.code, Index.Similarity())),
+                })
+
+    @classmethod
     def __register__(cls, module_name):
         transaction = Transaction()
         cursor = transaction.connection.cursor()
diff -r b39d7b2b20d1 -r a246d4950c8f purchase.py
--- a/purchase.py       Mon Oct 10 18:27:32 2022 +0200
+++ b/purchase.py       Tue Oct 11 00:44:50 2022 +0200
@@ -15,7 +15,8 @@
 from trytond.ir.attachment import AttachmentCopyMixin
 from trytond.ir.note import NoteCopyMixin
 from trytond.model import (
-    Model, ModelSQL, ModelView, Unique, Workflow, fields, sequence_ordered)
+    Index, Model, ModelSQL, ModelView, Unique, Workflow, fields,
+    sequence_ordered)
 from trytond.model.exceptions import AccessError
 from trytond.modules.account.tax import TaxableMixin
 from trytond.modules.account_product.exceptions import AccountError
@@ -74,7 +75,7 @@
         }
 
     company = fields.Many2One(
-        'company.company', "Company", required=True, select=True,
+        'company.company', "Company", required=True,
         states={
             'readonly': (
                 (Eval('state') != 'draft')
@@ -82,8 +83,8 @@
                 | Eval('party', True)
                 | Eval('invoice_party', True)),
             })
-    number = fields.Char('Number', size=None, readonly=True, select=True)
-    reference = fields.Char('Reference', select=True)
+    number = fields.Char("Number", readonly=True)
+    reference = fields.Char("Reference")
     description = fields.Char('Description', size=None, states=_states)
     purchase_date = fields.Date('Purchase Date',
         states={
@@ -103,7 +104,7 @@
         context={
             'company': Eval('company', -1),
             },
-        select=True, depends={'company'})
+        depends={'company'})
     party_lang = fields.Function(fields.Char('Party Language'),
         'on_change_with_party_lang')
     contact = fields.Many2One(
@@ -182,7 +183,8 @@
     invoices_recreated = fields.Many2Many(
             'purchase.purchase-recreated-account.invoice',
             'purchase', 'invoice', 'Recreated Invoices', readonly=True)
-    origin = fields.Reference('Origin', selection='get_origin', select=True,
+    origin = fields.Reference(
+        "Origin", selection='get_origin',
         states={
             'readonly': Eval('state') != 'draft',
             })
@@ -228,8 +230,27 @@
 
     @classmethod
     def __setup__(cls):
+        cls.number.search_unaccented = False
+        cls.reference.search_unaccented = False
         super(Purchase, cls).__setup__()
-        cls.create_date.select = True
+        t = cls.__table__()
+        cls._sql_indexes.update({
+                Index(t, (t.reference, Index.Similarity())),
+                Index(t, (t.party, Index.Equality())),
+                Index(
+                    t,
+                    (t.state, Index.Equality()),
+                    where=t.state.in_([
+                            'draft', 'quotation', 'confirmed', 'processing'])),
+                Index(
+                    t,
+                    (t.invoice_state, Index.Equality()),
+                    where=t.state.in_(['none', 'waiting', 'exception'])),
+                Index(
+                    t,
+                    (t.shipment_state, Index.Equality()),
+                    where=t.state.in_(['none', 'waiting', 'exception'])),
+                })
         cls._order = [
             ('purchase_date', 'DESC NULLS FIRST'),
             ('id', 'DESC'),
@@ -1010,27 +1031,27 @@
     'Purchase - Ignored Invoice'
     __name__ = 'purchase.purchase-ignored-account.invoice'
     _table = 'purchase_invoice_ignored_rel'
-    purchase = fields.Many2One('purchase.purchase', 'Purchase',
-            ondelete='CASCADE', select=True, required=True)
-    invoice = fields.Many2One('account.invoice', 'Invoice',
-            ondelete='RESTRICT', select=True, required=True)
+    purchase = fields.Many2One(
+        'purchase.purchase', "Purchase", ondelete='CASCADE', required=True)
+    invoice = fields.Many2One(
+        'account.invoice', "Invoice", ondelete='RESTRICT', required=True)
 
 
 class PurchaseRecreatedInvoice(ModelSQL):
     'Purchase - Recreated Invoice'
     __name__ = 'purchase.purchase-recreated-account.invoice'
     _table = 'purchase_invoice_recreated_rel'
-    purchase = fields.Many2One('purchase.purchase', 'Purchase',
-            ondelete='CASCADE', select=True, required=True)
-    invoice = fields.Many2One('account.invoice', 'Invoice',
-            ondelete='RESTRICT', select=True, required=True)
+    purchase = fields.Many2One(
+        'purchase.purchase', "Purchase", ondelete='CASCADE', required=True)
+    invoice = fields.Many2One(
+        'account.invoice', "Invoice", ondelete='RESTRICT', required=True)
 
 
 class Line(sequence_ordered(), ModelSQL, ModelView):
     'Purchase Line'
     __name__ = 'purchase.line'
-    purchase = fields.Many2One('purchase.purchase', 'Purchase',
-        ondelete='CASCADE', select=True, required=True,
+    purchase = fields.Many2One(
+        'purchase.purchase', "Purchase", ondelete='CASCADE', required=True,
         states={
             'readonly': ((Eval('purchase_state') != 'draft')
                 & Bool(Eval('purchase'))),
@@ -1040,7 +1061,7 @@
         ('subtotal', 'Subtotal'),
         ('title', 'Title'),
         ('comment', 'Comment'),
-        ], 'Type', select=True, required=True,
+        ], "Type", required=True,
         states={
             'readonly': Eval('purchase_state') != 'draft',
             })
@@ -1786,11 +1807,12 @@
     'Purchase Line - Tax'
     __name__ = 'purchase.line-account.tax'
     _table = 'purchase_line_account_tax'
-    line = fields.Many2One('purchase.line', 'Purchase Line',
-            ondelete='CASCADE', select=True, required=True,
-            domain=[('type', '=', 'line')])
-    tax = fields.Many2One('account.tax', 'Tax', ondelete='RESTRICT',
-            select=True, required=True, domain=[('parent', '=', None)])
+    line = fields.Many2One(
+        'purchase.line', "Purchase Line", ondelete='CASCADE', required=True,
+        domain=[('type', '=', 'line')])
+    tax = fields.Many2One(
+        'account.tax', "Tax", ondelete='RESTRICT', required=True,
+        domain=[('parent', '=', None)])
 
     @classmethod
     def __setup__(cls):
@@ -1806,20 +1828,20 @@
     'Purchase Line - Ignored Move'
     __name__ = 'purchase.line-ignored-stock.move'
     _table = 'purchase_line_moves_ignored_rel'
-    purchase_line = fields.Many2One('purchase.line', 'Purchase Line',
-            ondelete='CASCADE', select=True, required=True)
-    move = fields.Many2One('stock.move', 'Move', ondelete='RESTRICT',
-            select=True, required=True)
+    purchase_line = fields.Many2One(
+        'purchase.line', "Purchase Line", ondelete='CASCADE', required=True)
+    move = fields.Many2One(
+        'stock.move', "Move", ondelete='RESTRICT', required=True)
 
 
 class LineRecreatedMove(ModelSQL):
     'Purchase Line - Ignored Move'
     __name__ = 'purchase.line-recreated-stock.move'
     _table = 'purchase_line_moves_recreated_rel'
-    purchase_line = fields.Many2One('purchase.line', 'Purchase Line',
-            ondelete='CASCADE', select=True, required=True)
-    move = fields.Many2One('stock.move', 'Move', ondelete='RESTRICT',
-            select=True, required=True)
+    purchase_line = fields.Many2One(
+        'purchase.line', "Purchase Line", ondelete='CASCADE', required=True)
+    move = fields.Many2One(
+        'stock.move', "Move", ondelete='RESTRICT', required=True)
 
 
 class PurchaseReport(CompanyReport):

Reply via email to