Repository: Products.Archetypes
Branch: refs/heads/master
Date: 2014-11-14T12:43:50+01:00
Author: Johannes Raggam (thet) <raggam-nl@adm.at>
Commit: https://github.com/plone/Products.Archetypes/commit/bf2fcfdd55447aa1b420323b9302ea42aab9895e

For Plone 5, support getting markup control panel settings from the registry, while still supporting normal portal_properties access for Plone &lt; 5.

Files changed:
M CHANGES.txt
M Products/Archetypes/mimetype_utils.py

diff --git a/CHANGES.txt b/CHANGES.txt
index 7a28d03..48f0226 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,7 +4,9 @@ Changelog
 1.10.3 (unreleased)
 -------------------
 
-- Nothing changed yet.
+- For Plone 5, support getting markup control panel settings from the registry,
+  while still supporting normal portal_properties access for Plone < 5.
+  [thet]
 
 
 1.10.2 (2014-10-23)
diff --git a/Products/Archetypes/mimetype_utils.py b/Products/Archetypes/mimetype_utils.py
index a1696f6..24c9945 100644
--- a/Products/Archetypes/mimetype_utils.py
+++ b/Products/Archetypes/mimetype_utils.py
@@ -1,16 +1,44 @@
+"""Default- and allowable content type handling.
+"""
 from Products.CMFCore.utils import getToolByName
 
-#
-# default- and allowable content type handling
-#
+try:
+    from Products.CMFPlone.interfaces import IMarkupSchema
+    from plone.registry.interfaces import IRegistry
+    from zope.component import getUtility
+    from zope.component.interfaces import ComponentLookupError
+except ImportError:
+    IMarkupSchema = None
+
+
+def markupRegistrySettings(context):
+    if not IMarkupSchema:
+        return None
+    try:
+        # get the new registry
+        registry = getUtility(IRegistry, context=context)
+        settings = registry.forInterface(
+            IMarkupSchema,
+            prefix='plone',
+        )
+    except (KeyError, ComponentLookupError):
+        settings = None
+    return settings
+
 
 def getDefaultContentType(context):
-    portal_properties = getToolByName(context, 'portal_properties', None)
-    if portal_properties is not None:
-        site_properties = getattr(portal_properties, 'site_properties', None)
-        if site_properties is not None:
-            return site_properties.getProperty('default_contenttype')
-    return 'text/plain'
+    default_type = 'text/plain'
+    reg = markupRegistrySettings(context)
+    if reg:
+        default_type = reg.default_type
+    else:
+        portal_props = getToolByName(context, 'portal_properties', None)
+        if portal_props is not None:
+            site_props = getattr(portal_props, 'site_properties', None)
+            if site_props is not None:
+                default_type = site_props.getProperty('default_contenttype')
+    return default_type
+
 
 def setDefaultContentType(context, value):
     portal_properties = getToolByName(context, 'portal_properties', None)
@@ -19,36 +47,54 @@ def setDefaultContentType(context, value):
         if site_properties is not None:
             site_properties.manage_changeProperties(default_contenttype=value)
 
+
 def getAllowedContentTypes(context):
-    """ computes the list of allowed content types by subtracting the site property blacklist
-        from the list of installed types.
+    """Computes the list of allowed content types by subtracting the site
+    property blacklist from the list of installed types.
     """
-    allowable_types = getAllowableContentTypes(context)
-    forbidden_types = getForbiddenContentTypes(context)
-    allowed_types = [type for type in allowable_types if type not in forbidden_types]
+    allowed_types = []
+    reg = markupRegistrySettings(context)
+    if reg:
+        allowed_types = reg.allowed_types
+    else:
+        allowable_types = getAllowableContentTypes(context)
+        forbidden_types = getForbiddenContentTypes(context)
+        allowed_types = [
+            _type for _type in allowable_types if _type not in forbidden_types]
     return allowed_types
 
+
 def getAllowableContentTypes(context):
-    """ retrieves the list of installed content types by querying portal transforms. """
+    """Retrieves the list of installed content types by querying portal
+    transforms.
+    """
     portal_transforms = getToolByName(context, 'portal_transforms')
     return portal_transforms.listAvailableTextInputs()
 
+
 def setForbiddenContentTypes(context, forbidden_contenttypes=None):
-    """ Convenience method for settng the site property 'forbidden_contenttypes'."""
+    """Convenience method for settng the site property
+    ``forbidden_contenttypes``.
+    """
     if forbidden_contenttypes is None:
         forbidden_contenttypes = []
     portal_properties = getToolByName(context, 'portal_properties', None)
     if portal_properties is not None:
         site_properties = getattr(portal_properties, 'site_properties', None)
         if site_properties is not None:
-            site_properties.manage_changeProperties(forbidden_contenttypes=tuple(forbidden_contenttypes))
+            site_properties.manage_changeProperties(
+                forbidden_contenttypes=tuple(forbidden_contenttypes))
+
 
 def getForbiddenContentTypes(context):
-    """ Convenence method for retrevng the site property 'forbidden_contenttypes'."""
+    """Convenence method for retrevng the site property
+    ``forbidden_contenttypes``.
+    """
     portal_properties = getToolByName(context, 'portal_properties', None)
     if portal_properties is not None:
         site_properties = getattr(portal_properties, 'site_properties', None)
         if site_properties is not None:
             if site_properties.hasProperty('forbidden_contenttypes'):
-                return list(site_properties.getProperty('forbidden_contenttypes'))
+                return list(
+                    site_properties.getProperty('forbidden_contenttypes'))
     return []