[CMF-checkins] CVS: CMF/CMFUid - UniqueIdHandlerTool.py:1.15 interfaces.py:1.13

Jens Vagelpohl jens at dataflake.org
Sun Mar 13 09:14:39 EST 2005


Update of /cvs-repository/CMF/CMFUid
In directory cvs.zope.org:/tmp/cvs-serv20800

Modified Files:
	UniqueIdHandlerTool.py interfaces.py 
Log Message:
- Forward-porting CMFUid changes from CMF-1_5-branch (CMF Collector
  issue 307)


=== CMF/CMFUid/UniqueIdHandlerTool.py 1.14 => 1.15 ===
--- CMF/CMFUid/UniqueIdHandlerTool.py:1.14	Thu Aug 12 11:07:43 2004
+++ CMF/CMFUid/UniqueIdHandlerTool.py	Sun Mar 13 09:14:08 2005
@@ -72,23 +72,27 @@
         # reindex
         catalog.reindexObject(obj)
 
+    def _setUid(self, obj, uid):
+        """Attaches a unique id to the object and does reindexing.
+        """
+        # attach a unique id annotation to the object
+        anno_tool = getToolByName(self, 'portal_uidannotation')
+        annotation = anno_tool(obj, self.UID_ATTRIBUTE_NAME)
+        annotation.setUid(uid)
+        
+        # reindex the object
+        self._reindexObject(obj)
+
     security.declarePublic('register')
     def register(self, obj):
         """See IUniqueIdSet.
         """
         uid = self.queryUid(obj, default=None)
         if uid is None:
-            # attach a unique id annotation to the object
-            anno_tool = getToolByName(self, 'portal_uidannotation')
-            annotation = anno_tool(obj, self.UID_ATTRIBUTE_NAME)
-            
-            # initialize the annotation with a (new) unique id
+            # generate a new unique id and set it
             generator = getToolByName(self, 'portal_uidgenerator')
             uid = generator()
-            annotation.setUid(uid)
-            
-            # reindex the object
-            self._reindexObject(obj)
+            self._setUid(obj, uid)
             
         return uid
     
@@ -128,6 +132,24 @@
         if uid is None:
             raise UniqueIdError, "No unique id available on '%s'" % obj
         return uid
+    
+    security.declarePrivate('setUid')
+    def setUid(self, obj, uid, check_uniqueness=True):
+        """See IUniqueIdSet.
+        """
+        # None is the only value a unique id shall never have!
+        if uid is None:
+            raise UniqueIdError, "It's forbidden to set a unique id to 'None'."
+        
+        # check for uniqueness if enabled
+        result = self.queryObject(uid)
+        if check_uniqueness and result is not None and result != obj:
+            if callable(uid):
+                uid = uid()
+            raise UniqueIdError, "The unique id '%s' is already in use" % uid
+        
+        # everything is ok: set it!
+        self._setUid(obj, uid)
     
     def _queryBrain(self, uid, searchMethodName, default=None):
         """This helper method does the "hard work" of querying the catalog


=== CMF/CMFUid/interfaces.py 1.12 => 1.13 ===
--- CMF/CMFUid/interfaces.py:1.12	Thu Aug 12 11:07:43 2004
+++ CMF/CMFUid/interfaces.py	Sun Mar 13 09:14:08 2005
@@ -81,6 +81,14 @@
         UniqueIdError is raised if object was not registered previously.
         """
 
+    def setUid(obj, uid, check_uniqueness=True):
+        """Set the unique id of an object.
+        
+        By default a check ensuring uniqueness is enabled. Be aware when
+        disabling this check: You really need to know what you do !!!
+        """
+
+
 class IUniqueIdQuery(Interface):
     """Querying unique ids.
     """



More information about the CMF-checkins mailing list