Add slugify tool
issue8776 review288441002
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
+18
-4
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user