## Script (Python) "search_members" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=dir, datastructure, **kw ##title= ## # $Id: search_members.py 1903 2007-06-15 17:33:03Z joachim $ from Products.CPSDirectory.BaseDirectory import SearchSizeLimitExceeded from Products.AdvancedQuery import Eq, Between, Le,In try: from Products.zdb import set_trace except: def set_trace(): pass try: aq_portal = context.portal_catalog.evalAdvancedQuery except: aq_portal = context.portal_catalog_real.evalAdvancedQuery datamodel = datastructure.getDataModel() mapping = {} for key, value in datamodel.items(): if value: mapping[key] = value #mapping['homeless'] = True result_fields = context.getDirectoryResultFields(dir.getId(), dir.title_field) ##result_fields = [{'id': 'sn', 'title': 'label_last_name', 'sort': 'asc'}, ## {'id': 'givenName', 'title': 'label_first_name'}, ## {'id': 'email', 'title': 'label_email'}, ## {'id': id_field, 'title': 'label_user_name'}, ## ] if dir.getId() == "members": result_fields.append({'id': 'groups', 'title': 'Group(s)'}) return_fields = [] sort_by = None sort_direction = None process_fields = {} for field in result_fields: return_fields.append(field['id']) sorted = field.get('sort') if sorted == 'asc': sort_by = field['id'] sort_direction = 'asc' elif sorted == 'desc': sort_by = field['id'] sort_direction = 'desc' if field.get('process'): process_fields[field['id']] = field['process'] # empty search will not return anything if not mapping: return dir.search_members_results(results=[]), 'results' try: results = dir.searchEntries(return_fields=return_fields, **mapping) except SearchSizeLimitExceeded, e: rendered = dir.member_search_errors(exception=e) return rendered, 'results' for field, process_meth in process_fields.items(): meth = getattr(context, process_meth, None) if not meth: continue for item in results: value = item[1].get(field) item[1][field] = meth(field, value) if sort_by: items = [(str(item[1].get(sort_by, 'ZZZZ')).lower(), item) for item in results] items.sort() if sort_direction == 'desc': items.reverse() results = [item[1] for item in items] allres = [] for item in results: user_id = item[0] data = item[1] query = In('portal_type',('Faculty','Department','StudyLevel')) &\ In('localUsersWithRoles',('user:%s' % user_id,)) res = aq_portal(query) facs = [] roles = [] for r in res: p = r.getPath().split('/') fac = p[4] if len(p) == 5: facs.append(fac) roles.append(r) elif len(p) > 5: if fac not in facs: roles.append(r) item[1]['roles'] = roles if "groups" in return_fields: item[1]['groups'] = " ".join(item[1]['groups']) rendered = dir.search_members_results(results=results,result_fields=result_fields) return rendered, 'results'