Package: collective.autogroup
OverviewThis package provide a simple API to create automatic groups in Plone. Automatic
groups are users groups that have its member set automaticaly calculated. TheAuthenticatedMembersgroup is an example automatic group, created by Plone.Creating an automatic group is simple:Create a class implementingIMembershipCriteria. This class define a
criteria to decied whether an user belongs or not to the group.Callutils.create_auto_grouppassing an instance ofIMembershipCriteriaand other info about the group.Inside theutilsmodule there are other useful functions.Example usageUse case: we want to create an automatic group containing all users whose ID
begins with an ‘r’ letter.First we create a class implementingIMembershipCriteria:>>> from collective.autogroup.interfaces import IMembershipCriteria
>>> from zope.interface import implements
>>> class MembershipCriteria(object):
...     implements(IMembershipCriteria)
...     def __call__(self, principal):
...         return principal.getId()[0] == 'r'Now we create the auto group using this criteria:>>> from Products.CMFCore.utils import getToolByName
>>> from collective.autogroup.utils import create_auto_group
>>> group_name = 'r_users'
>>> group_title = 'Members of r_users'
>>> create_auto_group(
...     acl_users=self.portal.acl_users,
...     criteria=MembershipCriteria(),
...     group_name=group_name,
...     title=group_title,
... )
>>> gtool = getToolByName(self.portal, 'portal_groups')
>>> group = gtool.getGroupById(group_name)
>>> group is not None
TrueThe group should be empty for now, since we don’t have any users matching the
criteria:>>> len(group.getAllGroupMemberIds())
0Create some users and see what happens:>>> user1 = self.add_user('ronaldo')
>>> user2 = self.add_user('romario')
>>> user3 = self.add_user('tostao')
>>> sorted(group.getGroupMemberIds())
['romario', 'ronaldo']
>>> group_name in user1.getGroups()
True
>>> group_name in user2.getGroups()
True
>>> group_name in user3.getGroups()
FalseAbout the group titleCheck this out:>>> not group.getProperty('title')
TrueIt seems the title of the group is empty, even though we set the title togroup_titlewhen we calledcreate_auto_group. It turns outprefs_groups_overviewand other Plone templates do not callgroup.getPropertyto get the group title. Even theAuthenticatedUsersgroup (created by Plone) does not have the title property set:>>> not gtool.getGroupById('AuthenticatedUsers').getProperty('title')
TrueTheprefs_groups_overviewtemplate uses the following method to get the
group title:>>> search_results = self.portal.acl_users.searchGroups(id=group_name)
>>> search_results[0]['title'] == group_title
TrueTo change the title of an automatic group we should not callgroup.setProperty, it will not work. The correct way is change thedescriptionproperty of the correspondent PAS plugin (yes, it’s thedescriptionproperty, nottitle). Fortunately this package provides
a function that makes this job easier:>>> from collective.autogroup.utils import (set_auto_group_title,
...     get_auto_group_title)
>>> new_title = group_title + '_new'
>>> set_auto_group_title(self.acl_users, group_name, new_title)
>>> search_results = self.portal.acl_users.searchGroups(id=group_name)
>>> search_results[0]['title'] == new_title
True
>>> get_auto_group_title(self.acl_users, group_name) == new_title
TrueIt can also be done through ZMI.CreditsRafael Oliveira <rafaelbco@gmail.com>: Author.Changelog0.1b1 - 15feb2010First release.