Show groups first. By default show groups and search for users or groups with ajax.

This commit is contained in:
Vincent Fretin 2013-07-27 14:05:59 +02:00
parent 9b7293acd8
commit 2a9e614f03
3 changed files with 78 additions and 29 deletions

View File

@ -1,8 +1,5 @@
from five import grok
from zope import schema
from zope.interface import implements
from zope.component import queryUtility
from plone.autoform import directives as form
from plone.dexterity.content import Container
@ -10,13 +7,10 @@ from plone.dexterity.schema import DexteritySchemaPolicy
from plone.supermodel import model
from collective.dms.basecontent.relateddocs import RelatedDocs
from collective.z3cform.chosen.widget import ChosenMultiFieldWidget
from collective.z3cform.rolefield.field import LocalRolesToPrincipals
from . import _
from zope.schema.interfaces import IVocabularyFactory
from .widget import AjaxChosenMultiFieldWidget
class IDmsDocument(model.Schema):
"""Schema for DmsDocument"""
@ -32,7 +26,7 @@ class IDmsDocument(model.Schema):
roles_to_assign=('Editor',),
value_type=schema.Choice(vocabulary=u'collective.dms.basecontent.treating_groups',)
)
form.widget(treating_groups=ChosenMultiFieldWidget)
form.widget(treating_groups=AjaxChosenMultiFieldWidget)
recipient_groups = LocalRolesToPrincipals(
title=_(u"Recipient groups"),
@ -40,7 +34,7 @@ class IDmsDocument(model.Schema):
roles_to_assign=('Reader',),
value_type=schema.Choice(vocabulary=u'collective.dms.basecontent.recipient_groups')
)
form.widget(recipient_groups=ChosenMultiFieldWidget)
form.widget(recipient_groups=AjaxChosenMultiFieldWidget)
related_docs = RelatedDocs(
title=_(u"Related documents"),
@ -61,23 +55,3 @@ class DmsDocumentSchemaPolicy(DexteritySchemaPolicy):
def bases(self, schemaName, tree):
return (IDmsDocument, )
class TreatingGroupsVocabulary(grok.GlobalUtility):
"""Vocabulary for treating groups"""
grok.name('collective.dms.basecontent.treating_groups')
grok.implements(IVocabularyFactory)
def __call__(self, context):
principals = queryUtility(IVocabularyFactory, name=u'plone.principalsource.Principals')
return principals(context)
class RecipientGroupsVocabulary(grok.GlobalUtility):
"""Vocabulary for recipient groups"""
grok.name('collective.dms.basecontent.recipient_groups')
grok.implements(IVocabularyFactory)
def __call__(self, context):
principals = queryUtility(IVocabularyFactory, name=u'plone.principalsource.Principals')
return principals(context)

View File

@ -0,0 +1,63 @@
from five import grok
from zope.schema.interfaces import IVocabularyFactory
from plone.principalsource.source import PrincipalSourceBinder, PrincipalSource
# By default, we list groups and we can search for users in ajax
class PrincipalSource(PrincipalSource):
def search_principals(self, groups_first=False, **kw):
if kw:
results = self.acl_users.searchPrincipals(groups_first=True, **kw)
else:
# if no kw, we have been called from source __iter__ because
# of Chosen widget populate_select attribute is set to True
results = self.acl_users.searchGroups()
return [r for r in results if r.get('groupid', None) != 'AuthenticatedUsers']
@property
def _search(self):
if self.users and self.groups:
# return self.acl_users.searchPrincipals
return self.search_principals
elif self.users:
return self.acl_users.searchUsers
elif self.groups:
return self.acl_users.searchGroups
class PrincipalSourceBinder(PrincipalSourceBinder):
def __call__(self, context):
return PrincipalSource(context, self.users, self.groups)
class PrincipalsVocabularyFactory(grok.GlobalUtility):
"""Vocabulary for principals"""
grok.name('dms.principals')
grok.implements(IVocabularyFactory)
def __call__(self, context):
principals = PrincipalSourceBinder(users=True, groups=True)
return principals(context)
class TreatingGroupsVocabulary(grok.GlobalUtility):
"""Vocabulary for treating groups"""
grok.name('collective.dms.basecontent.treating_groups')
grok.implements(IVocabularyFactory)
def __call__(self, context):
principals = PrincipalSourceBinder(users=True, groups=True)
# principals = queryUtility(IVocabularyFactory, name=u'plone.principalsource.Principals')
return principals(context)
class RecipientGroupsVocabulary(grok.GlobalUtility):
"""Vocabulary for recipient groups"""
grok.name('collective.dms.basecontent.recipient_groups')
grok.implements(IVocabularyFactory)
def __call__(self, context):
# principals = queryUtility(IVocabularyFactory, name=u'plone.principalsource.Principals')
principals = PrincipalSourceBinder(users=True, groups=True)
return principals(context)

View File

@ -0,0 +1,12 @@
from zope.interface import implementer
import z3c.form.interfaces
from collective.z3cform.chosen.widget import AjaxChosenMultiSelectionWidget
@implementer(z3c.form.interfaces.IFieldWidget)
def AjaxChosenMultiFieldWidget(field, request):
widget = z3c.form.widget.FieldWidget(field,
AjaxChosenMultiSelectionWidget(request))
widget.populate_select = True
return widget