Skip to content

Instantly share code, notes, and snippets.

@MatthewWilkes
Created September 11, 2015 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewWilkes/45dbc759c426356cb6f0 to your computer and use it in GitHub Desktop.
Save MatthewWilkes/45dbc759c426356cb6f0 to your computer and use it in GitHub Desktop.
from zope import schema
from plone.directives import form
from .contenttree import SubsectionUUIDSourceBinder
class IExamples(form.Schema):
profileref = schema.Choice(title=u"Profile",
description=u"Select the profile",
source=SubsectionUUIDSourceBinder(
portal_type='UserProfile',
whitelist_types=('UserProfile', ),
)
)
org = schema.List(title=u"Organisation",
description=u"Select organisation",
value_type=schema.Choice(
source=SubsectionUUIDSourceBinder(
portal_type='Organisation',
subsection="/orgs"
)
)
)
from Products.CMFCore.utils import getToolByName
from plone.formwidget.contenttree import UUIDSourceBinder
from plone.formwidget.contenttree.widget import ContentTreeBase
from zope.schema.vocabulary import SimpleTerm
ContentTreeBase.show_all_content_types = False
class SubsectionUUIDSourceBinder(UUIDSourceBinder):
def __init__(self, *args, **kwargs):
self.subsection = kwargs.get("subsection", "")
self.portal_types =kwargs.get("whitelist_types")
super(SubsectionUUIDSourceBinder, self).__init__(*args, **kwargs)
def __call__(self, context):
UUIDSource = super(SubsectionUUIDSourceBinder, self).__call__(context)
try:
portal = getToolByName(context, "portal_url").getPortalObject()
except AttributeError:
# We aren't rooted in a Plone site, so we can't specialise our path
# This happens when instantiating a vocabulary in the form internals
# and is never used to show to users, so we can skip display-only logic
return UUIDSource
path = '/'.join(portal.getPhysicalPath()) + self.subsection
UUIDSource.navigation_tree_query = {'path': {'query':path}}
if self.portal_types is not None:
UUIDSource.navigation_tree_query['portal_type'] = self.portal_types
return UUIDSource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment