Support PYSON size for Char field
issue11315 review390241005
This commit is contained in:
1 parent
b6e57422ec
commit
57b1e4b69d
4 files changed
+40
-2
No files matched your search
@@ -288,6 +288,7 @@ The field is ordered using the similarity with the context value from the key
|
||||
|
||||
The maximum length (in characters) of the field. The size is enforced at
|
||||
the storage level and in the client input.
|
||||
The value can be a :class:`~trytond.pyson.PYSON` statement.
|
||||
|
||||
.. attribute:: Char.translate
|
||||
|
||||
|
||||
@@ -57,7 +57,10 @@ class Char(FieldTranslate):
|
||||
|
||||
@property
|
||||
def _sql_type(self):
|
||||
return 'VARCHAR(%s)' % self.size if self.size else 'VARCHAR'
|
||||
if isinstance(self.size, int):
|
||||
return 'VARCHAR(%s)' % self.size
|
||||
else:
|
||||
return 'VARCHAR'
|
||||
|
||||
def set_rpc(self, model):
|
||||
super(Char, self).set_rpc(model)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
from trytond.model import ModelSQL, fields
|
||||
from trytond.pool import Pool
|
||||
from trytond.pyson import Eval
|
||||
|
||||
|
||||
class Char(ModelSQL):
|
||||
@@ -33,6 +34,15 @@ class CharSize(ModelSQL):
|
||||
char = fields.Char("Char", size=5)
|
||||
|
||||
|
||||
class CharSizePYSON(ModelSQL):
|
||||
"Char PYSON Size"
|
||||
__name__ = 'test.char_size_pyson'
|
||||
char = fields.Char(
|
||||
"Char", size=Eval('size', 0),
|
||||
depends=['size'])
|
||||
size = fields.Integer("Size")
|
||||
|
||||
|
||||
class CharTranslate(ModelSQL):
|
||||
'Char Translate'
|
||||
__name__ = 'test.char_translate'
|
||||
@@ -68,6 +78,7 @@ def register(module):
|
||||
CharDefault,
|
||||
CharRequired,
|
||||
CharSize,
|
||||
CharSizePYSON,
|
||||
CharTranslate,
|
||||
CharUnaccentedOn,
|
||||
CharUnaccentedOff,
|
||||
|
||||
@@ -7,7 +7,7 @@ from sql import Literal
|
||||
|
||||
from trytond import backend
|
||||
from trytond.model.exceptions import (
|
||||
ForbiddenCharValidationError, RequiredValidationError)
|
||||
ForbiddenCharValidationError, RequiredValidationError, SizeValidationError)
|
||||
from trytond.pool import Pool
|
||||
from trytond.tests.test_tryton import (
|
||||
ExtensionTestCase, activate_module, with_transaction)
|
||||
@@ -422,6 +422,29 @@ class FieldCharTestCase(unittest.TestCase, CommonTestCaseMixin):
|
||||
'char': "foobar",
|
||||
}])
|
||||
|
||||
@with_transaction()
|
||||
def test_create_size_pyson_valid(self):
|
||||
"Test create char with PYSON size"
|
||||
Char = Pool().get('test.char_size_pyson')
|
||||
|
||||
char, = Char.create([{
|
||||
'char': "Test",
|
||||
'size': 5,
|
||||
}])
|
||||
|
||||
self.assertEqual(char.char, "Test")
|
||||
|
||||
@with_transaction()
|
||||
def test_create_size_pyson_invalid(self):
|
||||
"Test create char with invalid PYSON size"
|
||||
Char = Pool().get('test.char_size_pyson')
|
||||
|
||||
with self.assertRaises(SizeValidationError):
|
||||
Char.create([{
|
||||
'char': "foobar",
|
||||
'size': 5,
|
||||
}])
|
||||
|
||||
@with_transaction()
|
||||
def test_create_invalid_char(self):
|
||||
"Test create char with invalid char"
|
||||
|
||||
Reference in new issue
Block a user