class IListUsersForm(Interface): """Field definition for List Users form.""" groups = schema.FrozenSet( title=_(u'Groups'), description=_(u'Select groups from which you want to display users ' \ 'from.'), constraint=must_select_one_constraint, value_type=schema.Choice( vocabulary='collective.listusers.vocabularies.FilteredGroups' ) ) user_attributes = schema.List( title=_(u'User attributes'), description=_(u'Select which user attributes you want displayed in ' \ 'the results table.'), #constraint=must_select_one_constraint, value_type=schema.Choice( vocabulary='collective.listusers.vocabularies.UserAttributes', ), defaultFactory=default_user_attributes, required=True, ) search_fullname = schema.TextLine( title=_(u'Search'), required=False, )
def get_users(self): """Compile a list of users to display with selected user attributes + user group membership and username. :returns: Selected (+ additional) attributes for listed users :rtype: Dictionary of selected users' attributes """ gtool = getToolByName(self.context, 'portal_groups') attrs = self.user_attributes groups = self.request.get('form.widgets.groups') or [] search_fullname = self.request.get('form.widgets.search_fullname', '') if not (attrs or groups): return if not attrs: IStatusMessage(self.request).addStatusMessage( _('No user attributes predefined.'), type="error") return results = {} for user in self.get_groups_members(groups): result = dict() # do fullname search if search_fullname and search_fullname not in user.getProperty( 'fullname', ''): continue for attr in attrs: if attr == 'username': result[attr] = user.getId() elif attr == 'groups': result[attr] = ", ".join( sorted( filter( lambda g: g not in self.settings. exclude_groups, gtool.getGroupsForPrincipal(user)))) elif attr == 'vcard': # Only save the user_id, we will render the vcard link # manually in the template. result[attr] = user.getId() else: result[attr] = user.getProperty(attr, '') results[user.getId()] = result if not results: IStatusMessage(self.request).addStatusMessage( _('Search returned no results.'), type="info") return results
def get_users(self): """Compile a list of users to display with selected user attributes + user group membership and username. :returns: Selected (+ additional) attributes for listed users :rtype: Dictionary of selected users' attributes """ gtool = getToolByName(self.context, 'portal_groups') attrs = self.request.get('form.widgets.user_attributes') or [] groups = self.request.get('form.widgets.groups') or [] if not (attrs or groups): return results = {} for user in self.get_groups_members(groups): result = [] for attr in attrs: if attr == 'username': result.append(user.getId()) elif attr == 'groups': result.append(", ".join(gtool.getGroupsForPrincipal(user))) elif attr == 'vcard': # Do nothing, we will render the vcard link manually in the # template. pass else: result.append(user.getProperty(attr, '')) results[user.getId()] = result if not results: IStatusMessage(self.request).addStatusMessage(_('Search returned no results.'), type="info") return results
def get_users(self): """Compile a list of users to display with selected user attributes + user group membership and username. :returns: Selected (+ additional) attributes for listed users :rtype: Dictionary of selected users' attributes """ gtool = getToolByName(self.context, 'portal_groups') attrs = self.user_attributes groups = self.request.get('form.widgets.groups') or [] search_fullname = self.request.get('form.widgets.search_fullname', '') if not (attrs or groups): return if not attrs: IStatusMessage(self.request).addStatusMessage(_('No user attributes predefined.'), type="error") return results = {} for user in self.get_groups_members(groups): result = dict() # do fullname search if search_fullname and search_fullname not in user.getProperty('fullname', ''): continue for attr in attrs: if attr == 'username': result[attr] = user.getId() elif attr == 'groups': result[attr] = ", ".join(sorted(filter(lambda g: g not in self.settings.exclude_groups, gtool.getGroupsForPrincipal(user)))) elif attr == 'vcard': # Only save the user_id, we will render the vcard link # manually in the template. result[attr] = user.getId() else: result[attr] = user.getProperty(attr, '') results[user.getId()] = result if not results: IStatusMessage(self.request).addStatusMessage(_('Search returned no results.'), type="info") return results
class IListUsersSettings(Interface): """Global settings for the package""" # TODO: use FrozenSet once we fix # https://github.com/collective/collective.elephantvocabulary/issues/1 exclude_groups = schema.List( title=_(u'What groups to exclude from the product'), description= _(u'Select groups that should not show up in widgets or user list table' ), value_type=schema.Choice(vocabulary='plone.app.vocabularies.Groups', ), defaultFactory=default_settings_exclude_groups, ) filter_by_member_properties_attribute = schema.Choice( title=_(u'User property'), description=_( u'What member property to filter on based on defined vocabulary'), vocabulary='collective.listusers.vocabularies.UserAttributes', ) filter_by_member_properties_vocabulary = schema.DottedName( title=_(u'Dotted name to Vocabulary'), description=_( u'Select vocabulary used for filtering by member attribute'), constraint=validate_vocabulary, required=False, ) enable_user_attributes_widget = schema.Bool( title=_(u"label_enable_user_attributes_widget", default=u"Enable user attributes widget"), description=_( u"help_enable_user_attributes_widget", default= u"If checked, it will display widget to select user attributes to show in table" ), required=False, default=True, ) default_user_attributes = schema.List( title=_(u'Default user attributes'), description=_(u'Select which user attributes you want displayed in ' \ 'the results table.'), value_type=schema.Choice( vocabulary='collective.listusers.vocabularies.UserAttributes', ), constraint=must_select_one_constraint, defaultFactory=default_settings_user_attributes, )
class ListUsersForm(form.Form): """The List Users search form based on z3c.form.""" fields = field.Fields(IListUsersForm) label = _(u"List users") # don't try to read Plone root for form fields data, this is only mostly # usable for edit forms, where you have an actual context ignoreContext = True @button.buttonAndHandler(_(u"List users!")) def list_users(self, action): """Submit button handler.""" data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return @button.buttonAndHandler(_(u"Reset")) def reset_form(self, action): """Cancel button handler.""" url = self.context.portal_url() + "/@@listusers" self.request.response.redirect(url) def updateWidgets(self): """Hook before rendering the form""" settings = queryUtility(IRegistry).forInterface(IListUsersSettings) # filter by user attribute if settings.filter_by_member_properties_vocabulary and settings.filter_by_member_properties_attribute: field = schema.FrozenSet( __name__="filter_by_member_properties", title=u"%s" % settings.filter_by_member_properties_attribute.capitalize(), description=_(u'Filter members by attribute "%s"') % settings.filter_by_member_properties_attribute, value_type=schema.Choice( vocabulary=settings.filter_by_member_properties_vocabulary, ), ) self.fields += Fields(field) super(ListUsersForm, self).updateWidgets() if not settings.enable_user_attributes_widget: del self.widgets['user_attributes']
class IListUsersForm(Interface): """Field definition for List Users form.""" groups = FrozenSet( title=_(u'Groups'), description=_(u'Select groups from which you want to display users ' \ 'from.'), constraint=must_select_one_constraint, value_type=Choice( vocabulary='plone.app.vocabularies.Groups', ) ) user_attributes = List( title=_(u'User attributes'), description=_(u'Select which user attributes you want displayed in ' \ 'the results table.'), constraint=must_select_one_constraint, value_type=Choice( vocabulary='collective.listusers.vocabularies.UserAttributes', ) )
class ListUsersForm(form.Form): """The List Users search form based on z3c.form.""" fields = field.Fields(IListUsersForm) label = _(u"List users") # don't try to read Plone root for form fields data, this is only mostly # usable for edit forms, where you have an actual context ignoreContext = True @button.buttonAndHandler(_(u"List users!")) def list_users(self, action): """Submit button handler.""" data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return @button.buttonAndHandler(_(u"Reset")) def reset_form(self, action): """Cancel button handler.""" url = self.context.portal_url() + "/@@listusers" self.request.response.redirect(url)
def updateWidgets(self): """Hook before rendering the form""" settings = queryUtility(IRegistry).forInterface(IListUsersSettings) # filter by user attribute if settings.filter_by_member_properties_vocabulary and settings.filter_by_member_properties_attribute: field = schema.FrozenSet( __name__="filter_by_member_properties", title=u"%s" % settings.filter_by_member_properties_attribute.capitalize(), description=_(u'Filter members by attribute "%s"') % settings.filter_by_member_properties_attribute, value_type=schema.Choice( vocabulary=settings.filter_by_member_properties_vocabulary, ), ) self.fields += Fields(field) super(ListUsersForm, self).updateWidgets() if not settings.enable_user_attributes_widget: del self.widgets['user_attributes']
def must_select_one_constraint(value): """Check that at least item was selected.""" if len(value) == 0: raise Invalid(_(u"You need to select at least one value.")) return True
def validate_vocabulary(value): """check if dotted name really is vocabulary""" if queryUtility(IVocabularyFactory, value) is None: raise Invalid(_(u"Not a vocabulary: %s") % value) return True