Added "generate_url" function
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Apr 2018 12:23:18 +0200
changeset 171 d6c77b2b5c4b
parent 170 0013ddcfdc37
child 172 8476b679fcab
Added "generate_url" function
src/pyams_utils/url.py
--- a/src/pyams_utils/url.py	Thu Apr 12 11:21:59 2018 +0200
+++ b/src/pyams_utils/url.py	Fri Apr 13 12:23:18 2018 +0200
@@ -16,16 +16,27 @@
 # import standard library
 
 # import interfaces
-from persistent.interfaces import IPersistent
 from pyams_utils.interfaces.tales import ITALESExtension
 
 # import packages
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.unicode import translate_string
 from pyramid.encode import url_quote, urlencode
 from pyramid.url import resource_url, QUERY_SAFE
 from zope.interface import Interface
 
 
+def generate_url(title):
+    """Generate an SEO-friendly content URL from it's title
+
+    The original title is translated to remove accents, converted to lowercase, and words
+    shorter than three characters are removed; terms are joined by hyphens.
+    """
+    return '-'.join(filter(lambda x: len(x) > 2,
+                           translate_string(title, escape_slashes=True, force_lower=True, spaces='-',
+                                            remove_punctuation=True, keep_chars='-').split('-')))
+
+
 def absolute_url(context, request, view_name=None, query=None):
     """Get resource absolute_url