From 3cb69cdf28f54bcbf8e92592d573ef414cf5cb7c Mon Sep 17 00:00:00 2001
From: Nicolas Dumazet <nicolas.dumazet@nexedi.com>
Date: Mon, 27 Sep 2010 12:42:28 +0000
Subject: [PATCH] getPortalTypeClass: simple API to retrieve a portal type
 class from a portal type string, from an existing object, or from an existing
 class.

This is meant to be the high-level API to manipulate classes, and noone should
be seen using something else ;)


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38664 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/Tool/TypesTool.py | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/product/ERP5Type/Tool/TypesTool.py b/product/ERP5Type/Tool/TypesTool.py
index 9f8505b93e..21d588d528 100644
--- a/product/ERP5Type/Tool/TypesTool.py
+++ b/product/ERP5Type/Tool/TypesTool.py
@@ -169,6 +169,34 @@ class TypesTool(TypeProvider):
     from Products.ERP5Type import document_class_registry
     return sorted(document_class_registry)
 
+  security.declareProtected(Permissions.AccessContentsInformation, 'getPortalTypeClass')
+  def getPortalTypeClass(self, context, temp=False):
+    """
+    Infer a portal type class from the context.
+    Context can be a portal type string, or an object, or a class.
+
+    This is the proper API to retrieve a portal type class, and no one
+    should hack anything anywhere else.
+    """
+    portal_type = None
+    if isinstance(context, type):
+      if 'erp5' in context.__module__:
+        portal_type = context.__name__
+      else:
+        portal_type = getattr(context, 'portal_type', None)
+    elif isinstance(context, str):
+      portal_type = context
+    else:
+      portal_type = getattr(context, 'portal_type', None)
+
+    if portal_type is not None:
+      import erp5
+      if temp:
+        module = erp5.temp_portal_type
+      else:
+        module = erp5.portal_type
+      return getattr(module, portal_type, None)
+
   security.declareProtected(Permissions.AccessContentsInformation, 'getMixinTypeList')
   def getMixinTypeList(self):
     """
-- 
GitLab