changeset b34e27b43aa4 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=b34e27b43aa4
description:
        Add slugify tool

        issue8776
        review288441002
diffstat:

 CHANGELOG                   |   1 +
 trytond/tests/test_tools.py |  10 +++++++++-
 trytond/tools/misc.py       |  22 ++++++++++++++++++----
 3 files changed, 28 insertions(+), 5 deletions(-)

diffs (75 lines):

diff -r c4157134135a -r b34e27b43aa4 CHANGELOG
--- a/CHANGELOG Tue Nov 26 22:16:00 2019 +0100
+++ b/CHANGELOG Tue Nov 26 22:27:36 2019 +0100
@@ -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 -r c4157134135a -r b34e27b43aa4 trytond/tests/test_tools.py
--- a/trytond/tests/test_tools.py       Tue Nov 26 22:16:00 2019 +0100
+++ b/trytond/tests/test_tools.py       Tue Nov 26 22:27:36 2019 +0100
@@ -12,7 +12,7 @@
 
 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 @@
     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 -r c4157134135a -r b34e27b43aa4 trytond/tools/misc.py
--- a/trytond/tools/misc.py     Tue Nov 26 22:16:00 2019 +0100
+++ b/trytond/tools/misc.py     Tue Nov 26 22:27:36 2019 +0100
@@ -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 @@
     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)

Reply via email to