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

        issue3672
diffstat:

 exceptions.py   |  13 +++++++++++++
 message.xml     |  22 ++++++++++++++++++++++
 party.py        |  22 ++++++++--------------
 recurrence.py   |  32 ++++++++++----------------------
 subscription.py |  37 ++++++++++++++++++++-----------------
 tryton.cfg      |   1 +
 6 files changed, 74 insertions(+), 53 deletions(-)

diffs (232 lines):

diff -r 6b145a7d1e8e -r 993986c4a6df 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,13 @@
+# 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
+from trytond.model.exceptions import ValidationError
+
+
+class InvoiceError(UserError):
+    pass
+
+
+class RecurrenceRuleValidationError(ValidationError):
+    pass
diff -r 6b145a7d1e8e -r 993986c4a6df 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,22 @@
+<?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_consumption_modify_invoiced">
+            <field name="text">You cannot modify invoiced consumption 
"%(consumption)s".</field>
+        </record>
+        <record model="ir.message" id="msg_consumption_delete_invoiced">
+            <field name="text">You cannot delete invoiced consumption 
"%(consumption)s".</field>
+        </record>
+        <record model="ir.message" 
id="msg_consumption_invoice_missing_account_revenue">
+            <field name="text">To invoice consumptions you must define an 
account revenue for product "%(product)s".</field>
+        </record>
+        <record model="ir.message" id="msg_erase_party_pending_subscription">
+            <field name="text">You cannot erase party "%(party)s" while they 
have pending subscriptions with company "%(company)s".</field>
+        </record>
+        <record model="ir.message" id="msg_recurrence_rule_invalid_by">
+            <field name="text">Invalid "%(field)s" "%(value)s" in recurence 
rule "%(recurrence_rule)s" with exception "%(exception)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r 6b145a7d1e8e -r 993986c4a6df party.py
--- a/party.py  Tue Oct 30 18:22:39 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']
 
 
@@ -18,16 +21,6 @@
 class PartyErase(metaclass=PoolMeta):
     __name__ = 'party.erase'
 
-    @classmethod
-    def __setup__(cls):
-        super(PartyErase, cls).__setup__()
-        cls._error_messages.update({
-                'pending_subscription': (
-                    'The party "%(party)s" can not be erased '
-                    'because he has pending subscriptions '
-                    'for the company "%(company)s".'),
-                })
-
     def check_erase_company(self, party, company):
         pool = Pool()
         Subscription = pool.get('sale.subscription')
@@ -38,7 +31,8 @@
                 ('state', 'not in', ['closed', 'canceled']),
                 ])
         if subscriptions:
-            self.raise_user_error('pending_subscription', {
-                    'party': party.rec_name,
-                    'company': company.rec_name,
-                    })
+            raise EraseError(
+                gettext('sale_subscription'
+                    '.msg_erase_party_pending_subscription',
+                    party=party.rec_name,
+                    company=company.rec_name))
diff -r 6b145a7d1e8e -r 993986c4a6df recurrence.py
--- a/recurrence.py     Tue Oct 30 18:22:39 2018 +0100
+++ b/recurrence.py     Sat Dec 29 14:20:29 2018 +0100
@@ -4,9 +4,12 @@
 from dateutil.rrule import YEARLY, MONTHLY, WEEKLY, DAILY
 from dateutil.rrule import rrule, rruleset
 
+from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, fields
 from trytond.transaction import Transaction
 
+from .exceptions import RecurrenceRuleValidationError
+
 __all__ = ['RecurrenceRuleSet', 'RecurrenceRule']
 WEEKDAYS = {
     'MO': MO,
@@ -101,24 +104,6 @@
     exclusive = fields.Boolean("Exclusive")
 
     @classmethod
-    def __setup__(cls):
-        super(RecurrenceRule, cls).__setup__()
-        cls._error_messages.update({
-                'invalid_byweekday': (
-                    '"By Week Day" (%(byweekday)s) is not valid.'),
-                'invalid_bymonthday': (
-                    '"By Month Day" (%(bymonthday)s) is not valid.'),
-                'invalid_byyearday': (
-                    '"By Year Day" (%(byyearday)s) is not valid.'),
-                'invalid_byweekno': (
-                    '"By Week Number" (%(byweekno)s) is not valid.'),
-                'invalid_bymonth': (
-                    '"By Month" (%(bymonth)s) is not valid.'),
-                'invalid_bysetpos': (
-                    '"By Position" (%(bysetpos)s) is not valid.'),
-                })
-
-    @classmethod
     def default_interval(cls):
         return 1
 
@@ -214,7 +199,10 @@
     def check_by(self, name):
         try:
             getattr(self, '_%s' % name)
-        except ValueError:
-            self.raise_user_error('invalid_%s' % name, {
-                    name: getattr(self, name),
-                    })
+        except ValueError as exception:
+            raise RecurrenceRuleValidationError(
+                gettext('sale_subscription.msg_recurrence_rule_invalid_by',
+                    value=getattr(self, name),
+                    recurrence_rule=self.rec_name,
+                    exception=exception,
+                    **self._get_error_args(name))) from exception
diff -r 6b145a7d1e8e -r 993986c4a6df subscription.py
--- a/subscription.py   Tue Oct 30 18:22:39 2018 +0100
+++ b/subscription.py   Sat Dec 29 14:20:29 2018 +0100
@@ -8,8 +8,10 @@
 from sql.conditionals import Coalesce
 
 from trytond import backend
+from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, Workflow, fields, \
         sequence_ordered
+from trytond.model.exceptions import AccessError
 from trytond.pool import Pool
 from trytond.pyson import Eval, If, Bool
 from trytond.transaction import Transaction
@@ -17,6 +19,7 @@
         Button
 
 from trytond.modules.product import price_digits
+from .exceptions import InvoiceError
 
 __all__ = ['Subscription', 'Line', 'LineConsumption',
     'CreateLineConsumption', 'CreateLineConsumptionStart',
@@ -726,14 +729,6 @@
     def __setup__(cls):
         super(LineConsumption, cls).__setup__()
         cls._order.insert(0, ('date', 'DESC'))
-        cls._error_messages.update({
-                'modify_invoiced_consumption': (
-                    "You can not modify invoiced consumption."),
-                'delete_invoiced_consumption': (
-                    "You can not delete invoiced consumption."),
-                'missing_account_revenue': ('Product "%(product)s" '
-                    'misses a revenue account.'),
-                })
 
     @fields.depends('line')
     def on_change_with_unit_digits(self, name=None):
@@ -752,16 +747,23 @@
 
     @classmethod
     def write(cls, *args):
-        if any(c.invoice_line
-                for consumptions in args[::2]
-                for c in consumptions):
-            cls.raise_user_error('modify_invoiced_consumption')
+        for consumptions in args[::2]:
+            for consumption in consumptions:
+                if consumption.invoice_line:
+                    raise AccessError(
+                        gettext('sale_subscription'
+                            '.msg_consumption_modify_invoiced',
+                            consumption=consumption.rec_name))
         super(LineConsumption, cls).write(*args)
 
     @classmethod
     def delete(cls, consumptions):
-        if any(c.invoice_line for c in consumptions):
-            cls.raise_user_error('delete_invoiced_consumption')
+        for consumption in consumptions:
+            if consumption.invoice_line:
+                raise AccessError(
+                    gettext('sale_subscription'
+                        '.msg_consumption_modify_invoiced',
+                        consumption=consumption.rec_name))
         super(LineConsumption, cls).delete(consumptions)
 
     @classmethod
@@ -782,9 +784,10 @@
 
             line.account = line.product.account_revenue_used
             if not line.account:
-                cls.raise_user_error('missing_account_revenue', {
-                        'product': line.product.rec_name,
-                        })
+                raise InvoiceError(
+                    gettext('sale_subscription'
+                        '.msg_consumption_invoice_missing_account_revenue',
+                        product=line.product.rec_name))
 
             taxes = []
             pattern = line._get_tax_rule_pattern()
diff -r 6b145a7d1e8e -r 993986c4a6df tryton.cfg
--- a/tryton.cfg        Tue Oct 30 18:22:39 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -14,3 +14,4 @@
     recurrence.xml
     service.xml
     subscription.xml
+    message.xml

Reply via email to