changeset 70b0298d41e3 in modules/purchase:default
details: https://hg.tryton.org/modules/purchase?cmd=changeset;node=70b0298d41e3
description:
        Add ir.message and use custom exceptions

        issue3672
diffstat:

 exceptions.py |  17 ++++++++++++
 invoice.py    |  19 +++++---------
 message.xml   |  40 ++++++++++++++++++++++++++++++
 party.py      |  21 +++++----------
 product.py    |  20 ++++++--------
 purchase.py   |  78 +++++++++++++++++++++++-----------------------------------
 stock.py      |  23 ++++++-----------
 tryton.cfg    |   1 +
 8 files changed, 120 insertions(+), 99 deletions(-)

diffs (446 lines):

diff -r 70a6b2db3ef2 -r 70b0298d41e3 exceptions.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py     Sat Dec 29 14:20:29 2018 +0100
@@ -0,0 +1,17 @@
+# 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 trytond.exceptions import UserError, UserWarning
+from trytond.model.exceptions import ValidationError
+
+
+class PurchaseUOMWarning(UserWarning):
+    pass
+
+
+class PurchaseQuotationError(ValidationError):
+    pass
+
+
+class PartyLocationError(UserError):
+    pass
diff -r 70a6b2db3ef2 -r 70b0298d41e3 invoice.py
--- a/invoice.py        Sun Nov 18 12:11:19 2018 +0100
+++ b/invoice.py        Sat Dec 29 14:20:29 2018 +0100
@@ -1,9 +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 itertools import chain
 from functools import wraps
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, Workflow, fields
+from trytond.model.exceptions import AccessError
 from trytond.pool import Pool, PoolMeta
 from trytond.transaction import Transaction
 
@@ -34,14 +35,6 @@
     purchases = fields.Function(fields.One2Many('purchase.purchase', None,
             'Purchases'), 'get_purchases', searcher='search_purchases')
 
-    @classmethod
-    def __setup__(cls):
-        super(Invoice, cls).__setup__()
-        cls._error_messages.update({
-                'reset_invoice_purchase': ('You cannot reset to draft '
-                    'an invoice generated by a purchase.'),
-                })
-
     def get_purchase_exception_state(self, name):
         purchases = self.purchases
 
@@ -86,9 +79,11 @@
     @ModelView.button
     @Workflow.transition('draft')
     def draft(cls, invoices):
-        purchases = list(chain(*(i.purchases for i in invoices)))
-        if purchases and any(i.state == 'cancel' for i in invoices):
-            cls.raise_user_error('reset_invoice_purchase')
+        for invoice in invoices:
+            if invoice.purchases and invoice.state == 'cancel':
+                raise AccessError(
+                    gettext('purchase.msg_purchase_invoice_reset_draft',
+                        invoice=invoice.rec_name))
 
         return super(Invoice, cls).draft(invoices)
 
diff -r 70a6b2db3ef2 -r 70b0298d41e3 message.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml       Sat Dec 29 14:20:29 2018 +0100
@@ -0,0 +1,40 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton.  The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<tryton>
+    <data group="1">
+        <record model="ir.message" id="msg_change_purchase_uom">
+            <field name="text">You are trying to change the purchase UoM on 
which the purchase prices are based.</field>
+        </record>
+        <record model="ir.message" id="msg_erase_party_pending_purchase">
+            <field name="text">You cannot erase party "%(party)s" while they 
have pending purchases with company "%(company)s".</field>
+        </record>
+        <record model="ir.message" id="msg_warehouse_required_for_quotation">
+            <field name="text">To get a quote for the purchase "%(purchase)s" 
of goods or assets, you must enter a warehouse.</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_delete_cancel">
+            <field name="text">To delete purchase "%(purchase)s" you must 
cancel it.</field>
+        </record>
+        <record model="ir.message" 
id="msg_purchase_supplier_location_required">
+            <field name="text">To process purchase "%(purchase)s" you must set 
a supplier location on party "%(party)s".</field>
+        </record>
+        <record model="ir.message" 
id="msg_purchase_product_missing_account_expense">
+            <field name="text">To invoice purchase "%(purchase)s" you must 
define an account expense for product "%(product)s".</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_missing_account_expense">
+            <field name="text">To invoice purchase "%(purchase)s" you must 
configure a default account expense.</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_line_delete_cancel_draft">
+            <field name="text">To delete line "%(line)s" you must cancel or 
reset to draft purchase "%(purchase)s".</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_modify_header_draft">
+            <field name="text">To modify the header of purchase 
"%(purchase)s", it must be in draft state.</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_invoice_reset_draft">
+            <field name="text">You cannot reset invoice "%(invoice)s" to draft 
because it was generated by a purchase.</field>
+        </record>
+        <record model="ir.message" id="msg_purchase_move_reset_draft">
+            <field name="text">You cannot reset move "%(move)s" to draft 
because it was generated by a purchase.</field>
+        </record>
+    </data>
+</tryton>
diff -r 70a6b2db3ef2 -r 70b0298d41e3 party.py
--- a/party.py  Sun Nov 18 12:11:19 2018 +0100
+++ b/party.py  Sat Dec 29 14:20:29 2018 +0100
@@ -1,7 +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 trytond.i18n import gettext
 from trytond.pool import PoolMeta, Pool
 
+from trytond.modules.party.exceptions import EraseError
+
 __all__ = ['PartyReplace', 'PartyErase']
 
 
@@ -19,16 +22,6 @@
 class PartyErase(metaclass=PoolMeta):
     __name__ = 'party.erase'
 
-    @classmethod
-    def __setup__(cls):
-        super(PartyErase, cls).__setup__()
-        cls._error_messages.update({
-                'pending_purchase': (
-                    'The party "%(party)s" can not be erased '
-                    'because he has pending purchases '
-                    'for the company "%(company)s".'),
-                })
-
     def check_erase_company(self, party, company):
         pool = Pool()
         Purchase = pool.get('purchase.purchase')
@@ -39,7 +32,7 @@
                 ('state', 'not in', ['done', 'cancel']),
                 ])
         if purchases:
-            self.raise_user_error('pending_purchase', {
-                    'party': party.rec_name,
-                    'company': company.rec_name,
-                    })
+            raise EraseError(
+                gettext('purchase.msg_erase_party_pending_purchase',
+                    party=party.rec_name,
+                    company=company.rec_name))
diff -r 70a6b2db3ef2 -r 70b0298d41e3 product.py
--- a/product.py        Sun Nov 18 12:11:19 2018 +0100
+++ b/product.py        Sat Dec 29 14:20:29 2018 +0100
@@ -5,6 +5,7 @@
 from sql import Literal
 from sql.aggregate import Count
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, MatchMixin, fields, \
     sequence_ordered
 from trytond.pyson import Eval, If
@@ -13,6 +14,8 @@
 
 from trytond.modules.product import price_digits
 
+from .exceptions import PurchaseUOMWarning
+
 __all__ = ['Template', 'Product', 'ProductSupplier', 'ProductSupplierPrice']
 
 
@@ -35,14 +38,6 @@
         domain=[('category', '=', Eval('default_uom_category'))],
         depends=['active', 'purchasable', 'default_uom_category'])
 
-    @classmethod
-    def __setup__(cls):
-        super(Template, cls).__setup__()
-        cls._error_messages.update({
-                'change_purchase_uom': ('Purchase prices are based '
-                    'on the purchase uom.'),
-                })
-
     @fields.depends('default_uom', 'purchase_uom', 'purchasable')
     def on_change_default_uom(self):
         try:
@@ -65,6 +60,8 @@
 
     @classmethod
     def write(cls, *args):
+        pool = Pool()
+        Warning = pool.get('res.user.warning')
         actions = iter(args)
         for templates, values in zip(actions, actions):
             if not values.get("purchase_uom"):
@@ -77,9 +74,10 @@
                 for product in template.products:
                     if not product.product_suppliers:
                         continue
-                    cls.raise_user_warning(
-                            '%s@product_template' % template.id,
-                            'change_purchase_uom')
+                    name = '%s@product_template' % template.id
+                    if Warning.check(name):
+                        raise PurchaseUOMWarning(
+                            name, gettext('purchase.msg_change_purchase_uom'))
         super(Template, cls).write(*args)
 
 
diff -r 70a6b2db3ef2 -r 70b0298d41e3 purchase.py
--- a/purchase.py       Sun Nov 18 12:11:19 2018 +0100
+++ b/purchase.py       Sat Dec 29 14:20:29 2018 +0100
@@ -8,8 +8,10 @@
 from sql.aggregate import Count
 from sql.operators import Concat
 
+from trytond.i18n import gettext
 from trytond.model import Workflow, Model, ModelView, ModelSQL, fields, \
     sequence_ordered
+from trytond.model.exceptions import AccessError
 from trytond.modules.company import CompanyReport
 from trytond.wizard import Wizard, StateAction, StateView, StateTransition, \
     Button
@@ -19,8 +21,11 @@
 from trytond.pool import Pool
 
 from trytond.modules.account.tax import TaxableMixin
+from trytond.modules.account_product.exceptions import AccountError
 from trytond.modules.product import price_digits
 
+from .exceptions import PurchaseQuotationError, PartyLocationError
+
 __all__ = ['Purchase', 'PurchaseIgnoredInvoice',
     'PurchaseRecreadtedInvoice', 'PurchaseLine', 'PurchaseLineTax',
     'PurchaseLineIgnoredMove', 'PurchaseLineRecreatedMove', 'PurchaseReport',
@@ -197,12 +202,6 @@
             ('purchase_date', 'DESC'),
             ('id', 'DESC'),
             ]
-        cls._error_messages.update({
-                'warehouse_required': ('A warehouse must be defined for '
-                    'quotation of purchase "%s".'),
-                'delete_cancel': ('Purchase "%s" must be cancelled before '
-                    'deletion.'),
-                })
         cls._transitions |= set((
                 ('draft', 'quotation'),
                 ('quotation', 'confirmed'),
@@ -645,7 +644,9 @@
             if (not line.to_location
                     and line.product
                     and line.product.type in ('goods', 'assets')):
-                self.raise_user_error('warehouse_required', (self.rec_name,))
+                raise PurchaseQuotationError(
+                    gettext('purchase.msg_warehouse_required_for_quotation',
+                        purchase=self.rec_name))
 
     @classmethod
     def set_number(cls, purchases):
@@ -785,7 +786,9 @@
         cls.cancel(purchases)
         for purchase in purchases:
             if purchase.state != 'cancel':
-                cls.raise_user_error('delete_cancel', purchase.rec_name)
+                raise AccessError(
+                    gettext('purchase.msg_purchase_delete_cancel',
+                        purchase=purchase.rec_name))
         super(Purchase, cls).delete(purchases)
 
     @classmethod
@@ -1057,20 +1060,6 @@
         'on_change_with_purchase_state')
 
     @classmethod
-    def __setup__(cls):
-        super(PurchaseLine, cls).__setup__()
-        cls._error_messages.update({
-                'supplier_location_required': ('Purchase "%(purchase)s" '
-                    'misses the supplier location for line "%(line)s".'),
-                'missing_account_expense': ('Product "%(product)s" of '
-                    'purchase %(purchase)s misses an expense account.'),
-                'missing_default_account_expense': ('Purchase "%(purchase)s" '
-                    'misses a default "account expense".'),
-                'delete_cancel_draft': ('The line "%(line)s" must be on '
-                    'canceled or draft purchase to be deleted.'),
-                })
-
-    @classmethod
     def __register__(cls, module_name):
         super(PurchaseLine, cls).__register__(module_name)
         table = cls.__table_handler__(module_name)
@@ -1358,16 +1347,19 @@
         if self.product:
             invoice_line.account = self.product.account_expense_used
             if not invoice_line.account:
-                self.raise_user_error('missing_account_expense', {
-                        'product': invoice_line.product.rec_name,
-                        'purchase': self.purchase.rec_name,
-                        })
+                raise AccountError(
+                    gettext('purchase'
+                        '.msg_purchase_product_missing_account_expense',
+                        purchase=self.purchase.rec_name,
+                        product=self.product.rec_name))
         else:
             invoice_line.account = account_config.get_multivalue(
                 'default_category_account_expense')
             if not invoice_line.account:
-                self.raise_user_error('missing_default_account_expense',
-                    {'purchase': self.purchase.rec_name})
+                raise AccountError(
+                    gettext('purchase'
+                        '.msg_purchase_missing_account_expense',
+                        purchase=self.purchase.rec_name))
         invoice_line.stock_moves = self._get_invoice_line_moves()
         return [invoice_line]
 
@@ -1447,10 +1439,10 @@
             return
 
         if not self.purchase.party.supplier_location:
-            self.raise_user_error('supplier_location_required', {
-                    'purchase': self.purchase.rec_name,
-                    'line': self.rec_name,
-                    })
+            raise PartyLocationError(
+                gettext('purchase.msg_purchase_supplier_location_required',
+                    purchase=self.purchase.rec_name,
+                    party=self.purchase.party.rec_name))
         move = Move()
         move.quantity = quantity
         move.uom = self.unit
@@ -1521,9 +1513,10 @@
     def delete(cls, lines):
         for line in lines:
             if line.purchase_state not in {'cancel', 'draft'}:
-                cls.raise_user_error('delete_cancel_draft', {
-                        'line': line.rec_name,
-                        })
+                raise AccessError(
+                    gettext('purchase.msg_purchase_line_delete_cancel_draft',
+                        line=line.rec_name,
+                        purchase=line.purchase.rec_name))
         super(PurchaseLine, cls).delete(lines)
 
     @classmethod
@@ -1759,24 +1752,15 @@
             ])
     modify = StateTransition()
 
-    @classmethod
-    def __setup__(cls):
-        super(ModifyHeader, cls).__setup__()
-        cls._error_messages.update({
-                'not_in_draft': (
-                    'The purchase "%(purchase)s" must be in draft '
-                    'to modify header.'),
-                })
-
     def get_purchase(self):
         pool = Pool()
         Purchase = pool.get('purchase.purchase')
 
         purchase = Purchase(Transaction().context['active_id'])
         if purchase.state != 'draft':
-            self.raise_user_error('not_in_draft', {
-                    'purchase': purchase.rec_name,
-                    })
+            raise AccessError(
+                gettext('purchase.msg_purchase_modify_header_draft',
+                    purchase=purchase.rec_name))
         return purchase
 
     def default_start(self, fields):
diff -r 70a6b2db3ef2 -r 70b0298d41e3 stock.py
--- a/stock.py  Sun Nov 18 12:11:19 2018 +0100
+++ b/stock.py  Sat Dec 29 14:20:29 2018 +0100
@@ -6,7 +6,9 @@
 from sql.aggregate import Max
 from sql.operators import Concat
 
+from trytond.i18n import gettext
 from trytond.model import Workflow, ModelView, fields
+from trytond.model.exceptions import AccessError
 from trytond.pyson import Eval
 from trytond.pool import Pool, PoolMeta
 from trytond.transaction import Transaction
@@ -51,11 +53,6 @@
         if 'supplier' not in cls.incoming_moves.depends:
             cls.incoming_moves.depends.append('supplier')
 
-        cls._error_messages.update({
-                'reset_move': ('You cannot reset to draft move "%s" because '
-                    'it was generated by a purchase.'),
-                })
-
     @classmethod
     @ModelView.button
     @Workflow.transition('draft')
@@ -65,7 +62,9 @@
             for move in shipment.incoming_moves:
                 if (move.state == 'cancel'
                         and isinstance(move.origin, PurchaseLine)):
-                    cls.raise_user_error('reset_move', (move.rec_name,))
+                    raise AccessError(
+                        gettext('purchase.msg_purchase_move_reset_draft',
+                            move=move.rec_name))
 
         return super(ShipmentIn, cls).draft(shipments)
 
@@ -88,14 +87,6 @@
     __name__ = 'stock.shipment.in.return'
 
     @classmethod
-    def __setup__(cls):
-        super(ShipmentInReturn, cls).__setup__()
-        cls._error_messages.update({
-                'reset_move': ('You cannot reset to draft a move generated '
-                    'by a purchase.'),
-                })
-
-    @classmethod
     def __register__(cls, module_name):
         pool = Pool()
         Move = pool.get('stock.move')
@@ -133,7 +124,9 @@
             for move in shipment.moves:
                 if (move.state == 'cancel'
                         and isinstance(move.origin, PurchaseLine)):
-                    cls.raise_user_error('reset_move')
+                    raise AccessError(
+                        gettext('purchase.msg_purchase_move_reset_draft',
+                            move=move.rec_name))
 
         return super(ShipmentInReturn, cls).draft(shipments)
 
diff -r 70a6b2db3ef2 -r 70b0298d41e3 tryton.cfg
--- a/tryton.cfg        Sun Nov 18 12:11:19 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -18,3 +18,4 @@
     party.xml
     stock.xml
     product.xml
+    message.xml

Reply via email to