[916] | 1 | ## Script (Python) "search_members" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##parameters=dir, datastructure, **kw |
---|
| 8 | ##title= |
---|
| 9 | ## |
---|
| 10 | # $Id: search_members.py 1962 2007-06-26 04:51:01Z henrik $ |
---|
| 11 | |
---|
| 12 | from Products.CPSDirectory.BaseDirectory import SearchSizeLimitExceeded |
---|
[1845] | 13 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
[916] | 14 | try: |
---|
[1899] | 15 | from Products.zdb import set_trace |
---|
| 16 | except: |
---|
| 17 | def set_trace(): |
---|
| 18 | pass |
---|
| 19 | try: |
---|
[1845] | 20 | aq_portal = context.portal_catalog.evalAdvancedQuery |
---|
[916] | 21 | except: |
---|
[1845] | 22 | aq_portal = context.portal_catalog_real.evalAdvancedQuery |
---|
[916] | 23 | |
---|
| 24 | datamodel = datastructure.getDataModel() |
---|
| 25 | |
---|
| 26 | mapping = {} |
---|
| 27 | for key, value in datamodel.items(): |
---|
| 28 | if value: |
---|
| 29 | mapping[key] = value |
---|
| 30 | |
---|
[930] | 31 | #mapping['homeless'] = True |
---|
[1958] | 32 | #result_fields = context.getDirectoryResultFields(dir.getId(), |
---|
[1962] | 33 | # |
---|
| 34 | id_field = getattr(dir, 'id_field', 'id') |
---|
| 35 | result_fields = [{'id': 'sn', 'title': 'Last Name', 'sort': 'asc'}, |
---|
| 36 | {'id': 'givenName', 'title': 'First Name'}, |
---|
| 37 | {'id': 'email', 'title': 'Email Address'}, |
---|
| 38 | {'id': id_field, 'title': 'User Name'}, |
---|
[1958] | 39 | ] |
---|
[1899] | 40 | if dir.getId() == "members": |
---|
| 41 | result_fields.append({'id': 'groups', 'title': 'Group(s)'}) |
---|
[916] | 42 | |
---|
| 43 | return_fields = [] |
---|
| 44 | sort_by = None |
---|
| 45 | sort_direction = None |
---|
| 46 | process_fields = {} |
---|
| 47 | for field in result_fields: |
---|
| 48 | return_fields.append(field['id']) |
---|
| 49 | sorted = field.get('sort') |
---|
| 50 | if sorted == 'asc': |
---|
| 51 | sort_by = field['id'] |
---|
| 52 | sort_direction = 'asc' |
---|
| 53 | elif sorted == 'desc': |
---|
| 54 | sort_by = field['id'] |
---|
| 55 | sort_direction = 'desc' |
---|
| 56 | if field.get('process'): |
---|
| 57 | process_fields[field['id']] = field['process'] |
---|
| 58 | |
---|
| 59 | # empty search will not return anything |
---|
| 60 | if not mapping: |
---|
| 61 | return dir.search_members_results(results=[]), 'results' |
---|
| 62 | |
---|
| 63 | try: |
---|
| 64 | results = dir.searchEntries(return_fields=return_fields, **mapping) |
---|
| 65 | except SearchSizeLimitExceeded, e: |
---|
[953] | 66 | rendered = dir.member_search_errors(exception=e) |
---|
[916] | 67 | return rendered, 'results' |
---|
| 68 | for field, process_meth in process_fields.items(): |
---|
| 69 | meth = getattr(context, process_meth, None) |
---|
| 70 | if not meth: |
---|
| 71 | continue |
---|
| 72 | for item in results: |
---|
| 73 | value = item[1].get(field) |
---|
| 74 | item[1][field] = meth(field, value) |
---|
| 75 | |
---|
| 76 | if sort_by: |
---|
| 77 | items = [(str(item[1].get(sort_by, 'ZZZZ')).lower(), item) for item in results] |
---|
| 78 | items.sort() |
---|
| 79 | if sort_direction == 'desc': |
---|
| 80 | items.reverse() |
---|
| 81 | results = [item[1] for item in items] |
---|
| 82 | |
---|
| 83 | allres = [] |
---|
| 84 | for item in results: |
---|
| 85 | user_id = item[0] |
---|
| 86 | data = item[1] |
---|
[1903] | 87 | query = In('portal_type',('Faculty','Department','StudyLevel')) &\ |
---|
[1955] | 88 | In('localUsersWithRoles',('user:%s' % user_id,)) &\ |
---|
| 89 | ~ Eq('Creator',user_id) |
---|
[1845] | 90 | res = aq_portal(query) |
---|
[916] | 91 | facs = [] |
---|
| 92 | roles = [] |
---|
| 93 | for r in res: |
---|
| 94 | p = r.getPath().split('/') |
---|
| 95 | fac = p[4] |
---|
| 96 | if len(p) == 5: |
---|
| 97 | facs.append(fac) |
---|
| 98 | roles.append(r) |
---|
| 99 | elif len(p) > 5: |
---|
| 100 | if fac not in facs: |
---|
| 101 | roles.append(r) |
---|
| 102 | item[1]['roles'] = roles |
---|
[1899] | 103 | if "groups" in return_fields: |
---|
| 104 | item[1]['groups'] = " ".join(item[1]['groups']) |
---|
[1958] | 105 | |
---|
[1899] | 106 | rendered = dir.search_members_results(results=results,result_fields=result_fields) |
---|
[916] | 107 | |
---|
| 108 | return rendered, 'results' |
---|
| 109 | |
---|