<commit_before># -*- coding: utf-8 -*-
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """<NAME> <<EMAIL>>"""
__docformat__ = 'plaintext'

import time
import types

from zope.component import ComponentLookupError

from Products.CMFCore.utils import getToolByName
 
from interfaces import IGenericGroupTranslation
from interfaces import IGenericFilterTranslation

class MemberLookup(object):
    """This object contains the logic to list and search for users and groups.
    """
    
    def __init__(self, context, request, widget):
        """Construct this object and do base initialization.
        """
        self.context = context
        self.widget = widget
        self.searchabletext = request.get('searchabletext', '')
        group = request.get('selectgroup', '')
        try:
            grouptranslation = IGenericGroupTranslation(self.context)
            self.currentgroupid = grouptranslation.translateToRealGroupId(group)
        except ComponentLookupError:
            self.currentgroupid = group
        except TypeError, e:
            if e[0] == 'Could not adapt':
                self.currentgroupid = group
            else:
                raise
        return            
        
    def getGroups(self):
        """Return the groups.
        """
        start = time.time()
        
        filter = self._allocateFilter()
        aclu = getToolByName(self.context, 'acl_users')
        groups = aclu.getGroups()
        ret = []
        for group in groups:
            gid = group.getId()
            if not self._groupIdFilterMatch(gid, filter):
                continue
            ret.append((gid, group.getGroupTitleOrName()))
            
        print 'getGroups took %s' % str(time.time() - start)
        
        return ret
        
    def getMembers(self):
        """Return the Users in the following form.
        
        {
            'id': 'mmustermann',
            'fullname': '<NAME>',
        }
        """
        start = time.time()
        
        filter = self._allocateFilter()
        group = self.currentgroupid
        st = self.searchabletext
        aclu = getToolByName(self.context, 'acl_users')
        users = []
        user_ids = []
        if group != 'ignore' and group != '':
            group = aclu.getGroupById(group)
            if group:
                user_ids = group.getGroupMemberIds()
        else:
            # TODO: Search is done over all available groups, not only over groups which should be applied
            # also see getGroups
            if len(st) < 3:
                return []
            users_dict = aclu.searchUsers(name=st)
            user_ids = [user['id'] for user in users_dict]
        if user_ids:
            users = [aclu.getUserById(user_id) for user_id in user_ids]
        reduce = True
        for fil in filter:
            if fil == '*':
                reduce == False
        if reduce:
            users = self._reduceMembers(users, filter)
        ret = []
        for user in users:
            user_id = user.getId()
            for psheet in user.getOrderedPropertySheets():
                if psheet.hasProperty('fullname'):
                    user_fn = psheet.getProperty('fullname')
                else:
                    user_fn = user_id
            entry = {
                'id': user_id,
                'fullname': user_fn,<commit_msg>faster group member lookup<commit_after># -*- coding: utf-8 -*-
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """<NAME> <<EMAIL>>"""
__docformat__ = 'plaintext'

import time
import types
from zope.component import ComponentLookupError

from Products.CMFCore.utils import getToolByName
from Products.PlonePAS.interfaces.group import IGroupIntrospection
 
from interfaces import IGenericGroupTranslation
from interfaces import IGenericFilterTranslation

class MemberLookup(object):
    """This object contains the logic to list and search for users and groups.
    """
    
    def __init__(self, context, request, widget):
        """Construct this object and do base initialization.
        """
        self.context = context
        self.widget = widget
        self.searchabletext = request.get('searchabletext', '')
        group = request.get('selectgroup', '')
        try:
            grouptranslation = IGenericGroupTranslation(self.context)
            self.currentgroupid = grouptranslation.translateToRealGroupId(group)
        except ComponentLookupError:
            self.currentgroupid = group
        except TypeError, e:
            if e[0] == 'Could not adapt':
                self.currentgroupid = group
            else:
                raise
        return            
        
    def getGroups(self):
        """Return the groups.
        """
        start = time.time()
        
        filter = self._allocateFilter()
        aclu = getToolByName(self.context, 'acl_users')
        groups = aclu.getGroups()
        ret = []
        for group in groups:
            gid = group.getId()
            if not self._groupIdFilterMatch(gid, filter):
                continue
            ret.append((gid, group.getGroupTitleOrName()))
            
        print 'getGroups took %s' % str(time.time() - start)
        
        return ret
    
    def _getUserIdsOfGroup(self, groupid):
        aclu = getToolByName(self.context, 'acl_users')
        for giplugin in aclu.plugins.listPlugins(IGroupIntrospection):
            userids = giplugin.getGroupMembers(groupid)
            if userids: 
                return userids
        return []
        
    def getMembers(self):
        """Return the Users in the following form.
        
        {
            'id': 'mmustermann',
            'fullname': '<NAME>',
        }
        """
        start = time.time()
        
        filter = self._allocateFilter()
        group = self.currentgroupid
        st = self.searchabletext
        aclu = getToolByName(self.context, 'acl_users')
        users = []
        user_ids = []
        if group != 'ignore' and group != '':
            user_ids = _getUserIdsOfGroup(group)
        else:
            # TODO: Search is done over all available groups, not only over groups which should be applied
            # also see getGroups
            if len(st) < 3:
                return []
            users_dict = aclu.searchUsers(name=st)
            user_ids = [user['id'] for user in users_dict]
        if user_ids:
            users = [aclu.getUserById(user_id) for user_id in user_ids]
        reduce = True
        for fil in filter:
            if fil == '*':
                reduce == False
        if reduce:
            users = self._reduceMembers(users, filter)
        ret = []
        for user in users:
            user_id = user.getId()
            for psheet in user.getOrderedPropertySheets():
                if psheet.hasProperty('fullname'):
                    user_fn = psheet.getProperty('fullname')
                else:
                    user_fn = user_id
            entry = {
                'id': user_id,
                'fullname': user_fn,
{'fraction_non_alphanumeric': 0.08629022751385124, 'fraction_numerical': 0.003536484734174231, 'mean_word_length': 1.98207381370826, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1149492', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2184, 'n_words': 727}