[CMF-checkins] CVS: CMF/CMFUid - UniqueIdHandlerTool.py:1.14.2.1 interfaces.py:1.12.2.1

Grégoire Weber zope.org at incept.ch
Thu Nov 18 05:39:22 EST 2004


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

Modified Files:
      Tag: CMF-1_5-branch
	UniqueIdHandlerTool.py interfaces.py 
Log Message:
- added 'setUid' method allowing to set an object uid "by hand"


=== CMF/CMFUid/UniqueIdHandlerTool.py 1.14 => 1.14.2.1 ===
--- CMF/CMFUid/UniqueIdHandlerTool.py:1.14	Thu Aug 12 11:07:43 2004
+++ CMF/CMFUid/UniqueIdHandlerTool.py	Thu Nov 18 05:38:51 2004
@@ -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.12.2.1 ===
--- CMF/CMFUid/interfaces.py:1.12	Thu Aug 12 11:07:43 2004
+++ CMF/CMFUid/interfaces.py	Thu Nov 18 05:38:51 2004
@@ -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