Author: rspivak
Date: Fri Nov  4 00:42:19 2005
New Revision: 28948

Added:
   z3lab/cpsblog/trunk/utils.py   (contents, props changed)
Modified:
   z3lab/cpsblog/trunk/blog.py
   z3lab/cpsblog/trunk/configure.zcml
Log:
Added name chooser for blog entries. Id generation is based on
CPSUtil.id.generateId

Modified: z3lab/cpsblog/trunk/blog.py
==============================================================================
--- z3lab/cpsblog/trunk/blog.py (original)
+++ z3lab/cpsblog/trunk/blog.py Fri Nov  4 00:42:19 2005
@@ -1,11 +1,13 @@
 # $Id$
 
-from zope.interface import implements
-from zope.component import adapts
+from zope import interface
+from zope import component
+
 from zope.app.container.btree import BTreeContainer
 from zope.app.container.interfaces import INameChooser
 
 from cpsblog.interfaces import IBlog, IBlogContained
+from cpsblog.utils import generateId
 import re
 from datetime import datetime
 
@@ -35,7 +37,7 @@
     u'Blog description'
     """
 
-    implements(IBlog, IBlogContained)
+    interface.implements(IBlog, IBlogContained)
 
     # See cpsblog.interfaces.IBlog
     description = u''
@@ -46,8 +48,8 @@
 
 class BlogNameChooser:
     """An adapter to choose names for blog entries."""
-    implements(INameChooser)
-    adapts(IBlog)
+    interface.implements(INameChooser)
+    component.adapts(IBlog)
 
     def __init__(self, context):
         self.context = context
@@ -55,8 +57,7 @@
     def chooseName(self, name, entry):
         if name:
             return name
-        return datetime.now().strftime('%Y_%m_%d_') + \
-               re.sub('\s', '_', entry.title)
+        return generateId(entry.title)
 
     def checkName(self, name, entry):
         return True

Modified: z3lab/cpsblog/trunk/configure.zcml
==============================================================================
--- z3lab/cpsblog/trunk/configure.zcml  (original)
+++ z3lab/cpsblog/trunk/configure.zcml  Fri Nov  4 00:42:19 2005
@@ -241,6 +241,11 @@
       />
 
   <adapter
+      factory=".blog.BlogNameChooser"
+      permission="zope.View"
+      />
+
+  <adapter
       name="site"
       provides="zope.app.traversing.interfaces.ITraversable" for="*"
       factory=".adapters.Site" />

Added: z3lab/cpsblog/trunk/utils.py
==============================================================================
--- (empty file)
+++ z3lab/cpsblog/trunk/utils.py        Fri Nov  4 00:42:19 2005
@@ -0,0 +1,61 @@
+# $Id$
+
+import re
+
+# A regexp that does word splitting using alpha-numerical words
+WORD_SPLITTING_REGEXP = re.compile('[^_a-zA-Z0-9]*')
+
+def generateId(s, max_chars=24, lower=True, word_separator='-',
+               meaningless_words=[]):
+    """Generate an id from a given string.
+
+    This method avoids collisions and prevents words' cut.
+
+    The optional max_chars parameter truncates the generated ID if set to a
+    strictly positive number.
+
+    The optional lower parameter sets to lower-case the generated ID.
+
+    The optional word_separator parameter is the separator that will be 
inserted
+    between words that compose the generated ID. This separator will be present
+    in the URLs of portal objects and documents.
+
+    The optional meaningless_words parameter removes all the specified words
+    from the generated ID. The words are compared in a case sensitive manner.
+    """
+
+    gen_id = s
+    if lower:
+        gen_id = gen_id.lower()
+
+    # Word splitting the id based on the following separators: '-' '_' '.'.
+    # This is done because this method prevents words' cut.
+    words = WORD_SPLITTING_REGEXP.split(gen_id)
+    words = [w for w in words if w]
+
+    # Removing meaningless words if this has been asked
+    if meaningless_words:
+        # Removing meaningless words and obtaining a cleaned words list, but
+        # making sure though that we at least have one word left.
+        words_cleaned = [w for w in words if w not in meaningless_words]
+        if len(words_cleaned):
+            words = words_cleaned
+
+    if not words:
+        hash_object = md5.new()
+        hash_object.update(s)
+        gen_id = generateId(hash_object.digest())
+
+    # Doing the truncation if max_chars has been specified
+    elif max_chars > 0:
+        # Preventing word cuts if a max_chars truncation has been asked
+        gen_id = words[0] # The gen_id needs to contain at least one word
+        words = words[1:]
+        while words and ((len(gen_id) + len(words[0]) + 1) <= max_chars):
+            gen_id += word_separator + words[0]
+            words = words[1:]
+        gen_id = gen_id[:max_chars]
+    else:
+        gen_id = word_separator.join(words)
+
+    return gen_id
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to