diff --git a/CHANGELOG b/CHANGELOG index 032506905c..83cd1724f3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add slugify tool * Add is_secure, host and http_host to url module * Add __href__ to URLMixin * Add validate attribute to wizard's Button diff --git a/trytond/tests/test_tools.py b/trytond/tests/test_tools.py index 0e1ad91658..777367f046 100644 --- a/trytond/tests/test_tools.py +++ b/trytond/tests/test_tools.py @@ -12,7 +12,7 @@ import sql.operators from trytond.tools import ( reduce_ids, reduce_domain, decimal_, is_instance_method, file_open, - strip_wildcard, lstrip_wildcard, rstrip_wildcard) + strip_wildcard, lstrip_wildcard, rstrip_wildcard, slugify) from trytond.tools.string_ import StringPartitioned, LazyString from trytond.tools.domain_inversion import ( domain_inversion, parse, simplify, merge, concat, unique_value, @@ -194,6 +194,14 @@ class ToolsTestCase(unittest.TestCase): def test_rstrip_wildcard_diferent_wildcard(self): self.assertEqual(rstrip_wildcard('a___', '_'), 'a') + def test_slugify(self): + "Test slugify" + self.assertEqual(slugify('unicode ♥ is ☢'), 'unicode-is') + + def test_slugify_hyphenate(self): + "Test hyphenate in slugify" + self.assertEqual(slugify('foo bar', hyphenate='_'), 'foo_bar') + class StringPartitionedTestCase(unittest.TestCase): "Test StringPartitioned" diff --git a/trytond/tools/misc.py b/trytond/tools/misc.py index ee2763c9db..485d353df7 100644 --- a/trytond/tools/misc.py +++ b/trytond/tools/misc.py @@ -4,14 +4,16 @@ """ Miscelleanous tools used by tryton """ +import importlib +import io import os +import re import sys +import types +import unicodedata +import warnings from array import array from itertools import islice -import types -import io -import warnings -import importlib from sql import Literal from sql.operators import Or @@ -272,3 +274,15 @@ def rstrip_wildcard(string, wildcard='%', escape='\\'): if new_string[-1] == escape: return string return new_string + + +_slugify_strip_re = re.compile(r'[^\w\s-]') +_slugify_hyphenate_re = re.compile(r'[-\s]+') + + +def slugify(value, hyphenate='-'): + if not isinstance(value, str): + value = str(value) + value = unicodedata.normalize('NFKD', value) + value = str(_slugify_strip_re.sub('', value).strip()) + return _slugify_hyphenate_re.sub(hyphenate, value)