[ADD] currency qweb field widget, postfix currency

bzr revid: xmo@openerp.com-20131007145346-a3hlb6xjdwroyvzb
This commit is contained in:
Xavier Morel 2013-10-07 16:53:46 +02:00
parent 5ca6feb3fb
commit 727b0e27c5
4 changed files with 68 additions and 0 deletions

View File

@ -544,6 +544,20 @@ class ImageConverter(osv.AbstractModel):
return '<img src="data:%s;base64,%s">' % (Image.MIME[image.format], value)
class CurrencyConverter(osv.AbstractModel):
""" ``currency`` converter, has a mandatory option ``currency``.
The currency field is a (float) value, the linked ``currency`` is an m2o
to a ``res.currency`` indicating how to format the field.
"""
_name = 'ir.qweb.field.currency'
_inherit = 'ir.qweb.field'
def record_to_html(self, cr, uid, field_name, record, column, options):
return u'<span class="oe_currency_value">{0}</span> {1}'.format(
record[field_name],
record[options['currency']].symbol)
def get_field_type(column, options):
""" Gets a t-field's effective type from the field's column and its options
"""

View File

@ -1,3 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_converter_model,access_converter_model,model_test_converter_test_model,,1,1,1,1
access_test_converter_test_model_sub,access_test_converter_test_model_sub,model_test_converter_test_model_sub,,1,1,1,1
access_test_converter_currency,access_test_converter_currency,model_test_converter_currency,,1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_converter_model access_converter_model model_test_converter_test_model 1 1 1 1
3 access_test_converter_test_model_sub access_test_converter_test_model_sub model_test_converter_test_model_sub 1 1 1 1
4 access_test_converter_currency access_test_converter_currency model_test_converter_currency 1 1 1 1

View File

@ -37,3 +37,11 @@ class test_model_sub(orm.Model):
'name': fields.char()
}
class test_model_currency(orm.Model):
_name = 'test_converter.currency'
_columns = {
'value': fields.float(digits=(16, 55)),
'currency_id': fields.many2one('res.currency'),
}

View File

@ -1,10 +1,14 @@
# -*- encoding: utf-8 -*-
import json
import os
import xml.dom.minidom
from openerp.tests import common
directory = os.path.dirname(__file__)
impl = xml.dom.minidom.getDOMImplementation()
doc = impl.createDocument(None, None, None)
class TestExport(common.TransactionCase):
_model = None
@ -69,6 +73,46 @@ class TestFloatExport(TestBasicExport):
value = converter(42.01234)
self.assertEqual(value, '42.01')
class TestCurrencyExport(TestExport):
_model = 'test_converter.currency'
def setUp(self):
super(TestCurrencyExport, self).setUp()
self.Currency = self.registry('res.currency')
def create(self, model, context=None, **values):
return model.browse(
self.cr, self.uid,
model.create(self.cr, self.uid, values, context=context),
context=context)
def convert(self, obj):
converter = self.registry('ir.qweb.field.currency')
options = {'widget': 'currency', 'currency': 'currency_id'}
converted = converter.to_html(
self.cr, self.uid, 'value', obj, options,
doc.createElement('span'),
{'field': 'obj.value', 'field-options': json.dumps(options)},
'', {'obj': obj})
return converted
def test_currency_post(self):
currency = self.create(self.Currency, name="Test", symbol=u"test")
obj = self.create(self.Model, value=0.12, currency_id=currency.id)
converted = self.convert(obj)
self.assertEqual(
converted,
'<span data-oe-model="{obj._model._name}" data-oe-id="{obj.id}" '
'data-oe-field="value" data-oe-type="currency" '
'data-oe-translate="0" data-oe-expression="obj.value">'
'<span class="oe_currency_value">0.12</span> '
'{symbol}</span>'.format(
obj=obj,
symbol=currency.symbol.encode('utf-8')
),)
class TestTextExport(TestBasicExport):
def test_text(self):
converter = self.get_converter('text')
@ -175,5 +219,6 @@ class TestHTMLExport(TestBasicExport):
input = '<span>span</span>'
value = converter(input)
self.assertEqual(value, input)
# o2m, m2m?
# reference?