Hi,
When reviewing my code, @pokoli suggest me to use the register_mixin (https://codereview.tryton.org/276871002/diff/262991002/trytond/trytond/model/unit.py#newcode31) instead of hardcoding class name…
Here’s my mixin (unit.py in trytond/model):
# 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.cache import Cache
from trytond.rpc import RPC
class UnitMixin(object):
__slots__ = ()
@classmethod
def __setup__(cls):
super().__setup__()
cls.__rpc__.update({
'get_unit_symbol': RPC(cache=dict(days=1)),
})
# Do not instantiate more than one Cache
if not hasattr(cls, '_symbol_cache'):
cls._symbol_cache = Cache(
cls.__name__ + '.symbol')
@classmethod
def get_unit_symbol(cls, id, value=0):
'Return the symbol and its position'
position = 1
symbol = cls._symbol_cache.get(id)
if not symbol:
unit, = cls.search([('id', '=', id)])
symbol = unit.symbol
cls._symbol_cache.set(id, symbol)
return (symbol, position)
In module currency, i’m using this mixin but i need to extend it.
init.py
# 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 import UnitMixin
from trytond.pool import Pool
from . import currency
def register():
Pool.register_mixin(currency.UnitMixin, UnitMixin,
module='currency')
Pool.register(
currency.Currency,
currency.Rate,
module='currency', type_='model')
and the code added in currency.py
…
__all__ = ['Currency', 'Rate']
class UnitMixin:
@classmethod
def __setup__(cls):
super().__setup__()
@classmethod
def get_unit_symbol(cls, id, value=0):
symbol, position = super().get_unit_symbol(id, value)
p_cs_precedes = Transaction().context.get('p_cs_precedes', True)
n_cs_precedes = Transaction().context.get('_cs_precedes', True)
if (value < 0 and n_cs_precedes or p_cs_precedes):
position = 0
return (symbol, position)
class Currency(UnitMixin, DeactivableMixin, ModelSQL, ModelView):
'Currency'
__name__ = 'currency.currency'
name = fields.Char('Name', required=True, translate=True,
…
When testing my code, i get this error:
INFO:tryton.rpc:model.currency.currency.get_unit_symbol(1, Decimal('18.00000'), {'client': '6c74fe34-1130-4e77-8244-c50bb2865019', 'warehouse': None, 'employee': None, 'company': 1, 'company.rec_name': 'Preciball SA', 'language': 'fr', 'language_direction': 'ltr', 'groups': [6, 7, 1, 4, 2, 5, 3, 8, 12, 13, 10, 9, 11], 'p_cs_precedes': 0, 'n_cs_precedes': 1, 'locale': {'date': '%d.%m.%Y', 'grouping': [3, 0], 'decimal_point': ',', 'thousands_sep': ' '}})
ERROR:tryton.common.common:FORBIDDEN
Fault: 403
It was working before doing the register_mixin…
Thanks for help!