Add remove_forbidden_chars in tools
issue10684 review371581002
This commit is contained in:
1 parent
b843e4e5bf
commit
154d8e4a23
5 files changed
+32
-3
No files matched your search
@@ -1,3 +1,4 @@
|
||||
* Add remove_forbidden_chars in tools
|
||||
* Manage errors during non-interactive operations
|
||||
* Add estimation count to ModelStorage
|
||||
* Support empty digits in format_number of Report
|
||||
|
||||
@@ -26,3 +26,8 @@ Return an SQL expression that pairs SQL integers x and y.
|
||||
.. method:: firstline(text)
|
||||
|
||||
Return first non-empty line of a text field.
|
||||
|
||||
.. method:: remove_forbidden_chars(value)
|
||||
|
||||
Return a copy of the string with forbidden char from
|
||||
:class:`~trytond.models.fields.Char` replaced by space.
|
||||
@@ -13,7 +13,8 @@ import sql.operators
|
||||
from trytond.tools import (
|
||||
reduce_ids, reduce_domain, decimal_, is_instance_method, file_open,
|
||||
strip_wildcard, lstrip_wildcard, rstrip_wildcard, slugify, sortable_values,
|
||||
escape_wildcard, unescape_wildcard, is_full_text, firstline)
|
||||
escape_wildcard, unescape_wildcard, is_full_text, firstline,
|
||||
remove_forbidden_chars)
|
||||
from trytond.tools.string_ import StringPartitioned, LazyString
|
||||
from trytond.tools.domain_inversion import (
|
||||
domain_inversion, parse, simplify, merge, concat, unique_value,
|
||||
@@ -848,6 +849,17 @@ class DomainInversionTestCase(unittest.TestCase):
|
||||
with self.subTest(text=text, result=result):
|
||||
self.assertEqual(firstline(text), result)
|
||||
|
||||
def test_remove_forbidden_chars(self):
|
||||
"Test remove_forbidden_chars"
|
||||
for string, result in [
|
||||
("", ""),
|
||||
(None, None),
|
||||
("\ttest", "test"),
|
||||
(" test ", "test"),
|
||||
]:
|
||||
with self.subTest(string=string):
|
||||
self.assertEqual(remove_forbidden_chars(string), result)
|
||||
|
||||
|
||||
def suite():
|
||||
func = unittest.TestLoader().loadTestsFromTestCase
|
||||
|
||||
@@ -9,7 +9,8 @@ from .misc import (
|
||||
file_open, get_smtp_server, reduce_ids, reduce_domain,
|
||||
grouped_slice, is_instance_method, resolve, strip_wildcard,
|
||||
lstrip_wildcard, rstrip_wildcard, slugify, sortable_values,
|
||||
escape_wildcard, unescape_wildcard, is_full_text, sql_pairing, firstline)
|
||||
escape_wildcard, unescape_wildcard, is_full_text, sql_pairing, firstline,
|
||||
remove_forbidden_chars)
|
||||
from .decimal_ import decistmt
|
||||
|
||||
__all__ = ['file_open', 'get_smtp_server', 'reduce_ids',
|
||||
@@ -17,7 +18,7 @@ __all__ = ['file_open', 'get_smtp_server', 'reduce_ids',
|
||||
'strip_wildcard', 'lstrip_wildcard', 'rstrip_wildcard', 'slugify',
|
||||
'decistmt', 'ClassProperty', 'cursor_dict', 'cached_property',
|
||||
'sortable_values', 'escape_wildcard', 'unescape_wildcard', 'is_full_text',
|
||||
'sql_pairing', 'firstline']
|
||||
'sql_pairing', 'firstline', 'remove_forbidden_chars']
|
||||
|
||||
|
||||
class ClassProperty(property):
|
||||
|
||||
@@ -272,3 +272,13 @@ def firstline(text):
|
||||
return next((x for x in text.splitlines() if x.strip()))
|
||||
except StopIteration:
|
||||
return ''
|
||||
|
||||
|
||||
def remove_forbidden_chars(value):
|
||||
from trytond.model.fields import Char
|
||||
if value is None:
|
||||
return value
|
||||
for c in Char.forbidden_chars:
|
||||
if c in value:
|
||||
value = value.replace(c, ' ')
|
||||
return value.strip()
|
||||
Reference in new issue
Block a user