Updated sequence utility interface
authorThierry Florac <thierry.florac@onf.fr>
Wed, 28 Jun 2017 10:24:23 +0200
changeset 10 aa2472483e00
parent 9 a19dd404951e
child 11 97d056a73504
Updated sequence utility interface
src/pyams_sequence/interfaces/__init__.py
src/pyams_sequence/utility.py
--- a/src/pyams_sequence/interfaces/__init__.py	Wed Jun 07 10:52:03 2017 +0200
+++ b/src/pyams_sequence/interfaces/__init__.py	Wed Jun 28 10:24:23 2017 +0200
@@ -53,6 +53,12 @@
     def get_short_oid(self, oid, obj_prefix=None):
         """Get short ID based on given numeric object ID"""
 
+    def get_internal_id(self, oid):
+        """Get internal ID matching given OID"""
+
+    def query_object_from_oid(self, oid):
+        """Query object with given OID"""
+
 
 class ISequentialIdInfo(Interface):
     """Sequential ID info interface"""
--- a/src/pyams_sequence/utility.py	Wed Jun 07 10:52:03 2017 +0200
+++ b/src/pyams_sequence/utility.py	Wed Jun 28 10:24:23 2017 +0200
@@ -69,6 +69,17 @@
                                        sequence.get_short_oid(info.oid))}
 
 
+def get_sequence_target(oid, state):
+    """Get content matching given OID"""
+    sequence = get_utility(ISequentialIntIds)
+    content = sequence.query_object_from_oid(oid)
+    if handle_workflow and (IWorkflowVersion.providedBy(content) or IWorkflowManagedContent.providedBy(content)):
+        versions = IWorkflowVersions(content).get_versions(state, sort=True)
+        if versions:
+            content = versions[0]
+            return content
+
+
 @implementer(ISequentialIntIds)
 class SequentialIntIds(IntIds):
     """Sequential IntIds utility"""
@@ -106,10 +117,10 @@
                         hex_id=oid)
 
     def get_full_oid(self, oid, obj_prefix=None):
-        if self.prefix and oid.startswith(self.prefix or ''):
-            return oid
         if oid.startswith('+'):
             oid = oid[1:]
+        elif self.prefix and oid.startswith(self.prefix):
+            return oid
         return '{prefix}{obj_prefix}{zeros}{hex_id}'.format(prefix=self.prefix or '',
                                                             obj_prefix=obj_prefix or '',
                                                             zeros='0' * (self.hex_oid_length - len(oid)),
@@ -120,6 +131,17 @@
                                                         obj_prefix=obj_prefix or '',
                                                         hex_id=oid)
 
+    def get_internal_id(self, oid):
+        if oid.startswith('+'):
+            oid = oid[1:]
+        elif self.prefix and oid.startswith(self.prefix):
+            oid = oid[len(self.prefix):]
+        return int(oid, 16)
+
+    def query_object_from_oid(self, oid):
+        internal_id = self.get_internal_id(oid)
+        return self.queryObject(internal_id)
+
 
 @subscriber(IObjectAddedEvent, context_selector=ISequentialIdTarget)
 def handle_added_intid_target(event):