Add ir.message and use custom exceptions
issue3672
This commit is contained in:
+14
-21
@@ -8,6 +8,7 @@ from sql.aggregate import Sum
|
||||
from sql.conditionals import Coalesce
|
||||
|
||||
from trytond import backend
|
||||
from trytond.i18n import gettext
|
||||
from trytond.model import (ModelView, ModelSQL, DeactivableMixin, fields,
|
||||
Unique, tree)
|
||||
from trytond.wizard import Wizard, StateView, StateAction, Button
|
||||
@@ -15,6 +16,8 @@ from trytond.pyson import Eval, If, PYSONEncoder, PYSONDecoder
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
|
||||
from .exceptions import AccountValidationError, RootValidationError
|
||||
|
||||
__all__ = ['Account', 'AccountDistribution',
|
||||
'OpenChartAccountStart', 'OpenChartAccount',
|
||||
'AnalyticAccountEntry', 'AnalyticMixin']
|
||||
@@ -109,11 +112,6 @@ class Account(
|
||||
super(Account, cls).__setup__()
|
||||
cls._order.insert(0, ('code', 'ASC'))
|
||||
cls._order.insert(1, ('name', 'ASC'))
|
||||
cls._error_messages.update({
|
||||
'invalid_distribution': (
|
||||
'The distribution sum of account "%(account)s" '
|
||||
'is not 100%%.'),
|
||||
})
|
||||
|
||||
@classmethod
|
||||
def __register__(cls, module_name):
|
||||
@@ -153,9 +151,9 @@ class Account(
|
||||
if self.type != 'distribution':
|
||||
return
|
||||
if sum((d.ratio for d in self.distributions)) != 1:
|
||||
self.raise_user_error('invalid_distribution', {
|
||||
'account': self.rec_name,
|
||||
})
|
||||
raise AccountValidationError(
|
||||
gettext('analytic_account.msg_invalid_distribution',
|
||||
account=self.rec_name))
|
||||
|
||||
@fields.depends('company')
|
||||
def on_change_with_currency(self, name=None):
|
||||
@@ -447,7 +445,7 @@ class AnalyticAccountEntry(ModelView, ModelSQL):
|
||||
t = cls.__table__()
|
||||
cls._sql_constraints += [
|
||||
('root_origin_uniq', Unique(t, t.origin, t.root),
|
||||
'Only one account is allowed per analytic root and origin.'),
|
||||
'analytic_account.msg_root_origin_unique'),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
@@ -502,14 +500,6 @@ class AnalyticMixin(object):
|
||||
analytic_accounts_size = fields.Function(fields.Integer(
|
||||
'Analytic Accounts Size'), 'get_analytic_accounts_size')
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(AnalyticMixin, cls).__setup__()
|
||||
cls._error_messages.update({
|
||||
'root_account': ('Some mandatory root account are missing '
|
||||
'on "%(name)s"'),
|
||||
})
|
||||
|
||||
@classmethod
|
||||
def __register__(cls, module_name):
|
||||
pool = Pool()
|
||||
@@ -591,7 +581,10 @@ class AnalyticMixin(object):
|
||||
for mandatory in all_mandatory_roots:
|
||||
if mandatory.company in companies:
|
||||
mandatory_roots.add(mandatory)
|
||||
if not mandatory_roots <= analytic_roots:
|
||||
cls.raise_user_error('root_account', {
|
||||
'name': analytic.rec_name,
|
||||
})
|
||||
mandatory_roots.difference_update(analytic_roots)
|
||||
if mandatory_roots:
|
||||
raise RootValidationError(
|
||||
gettext('analytic_account.msg_missing_root_account',
|
||||
name=analytic.rec_name,
|
||||
analytics=', '.join(
|
||||
(r.rec_name for r in mandatory_roots))))
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# 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.model.exceptions import ValidationError
|
||||
|
||||
|
||||
class AccountValidationError(ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class RootValidationError(ValidationError):
|
||||
pass
|
||||
@@ -46,15 +46,8 @@ class Line(ModelSQL, ModelView):
|
||||
('credit_debit',
|
||||
Check(t,
|
||||
(t.credit * t.debit == 0) & (t.credit + t.debit >= 0)),
|
||||
'Wrong credit/debit values.'),
|
||||
'account.msg_line_debit_credit'),
|
||||
]
|
||||
cls._error_messages.update({
|
||||
'line_on_view_account': (
|
||||
'You can not create a move line using '
|
||||
'view account "%s".'),
|
||||
'line_on_inactive_account': ('You can not create a move line '
|
||||
'using inactive account "%s".'),
|
||||
})
|
||||
cls._order.insert(0, ('date', 'ASC'))
|
||||
|
||||
@classmethod
|
||||
@@ -144,20 +137,6 @@ class Line(ModelSQL, ModelView):
|
||||
MoveLine.set_analytic_state(move_lines)
|
||||
MoveLine.save(move_lines)
|
||||
|
||||
@classmethod
|
||||
def validate(cls, lines):
|
||||
super(Line, cls).validate(lines)
|
||||
for line in lines:
|
||||
line.check_account()
|
||||
|
||||
def check_account(self):
|
||||
if self.account.type == 'view':
|
||||
self.raise_user_error('line_on_view_account',
|
||||
(self.account.rec_name,))
|
||||
if not self.account.active:
|
||||
self.raise_user_error('line_on_inactive_account',
|
||||
(self.account.rec_name,))
|
||||
|
||||
|
||||
class Move(metaclass=PoolMeta):
|
||||
__name__ = 'account.move'
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?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_invalid_distribution">
|
||||
<field name="text">To save account "%(account)s" the sum of their distributions must be 100%%.</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_missing_root_account">
|
||||
<field name="text">To save "%(name)s" you must set a value for analytics "%(analytics)s".</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_root_origin_unique">
|
||||
<field name="text">Only one account is allowed by analytic root and origin.</field>
|
||||
</record>
|
||||
</data>
|
||||
</tryton>
|
||||
@@ -12,3 +12,4 @@ xml:
|
||||
account.xml
|
||||
line.xml
|
||||
rule.xml
|
||||
message.xml
|
||||
|
||||
Reference in New Issue
Block a user