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 953 2006-11-27 22:03:04Z henrik $ |
---|
11 | |
---|
12 | from Products.CPSDirectory.BaseDirectory import SearchSizeLimitExceeded |
---|
13 | try: |
---|
14 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
15 | evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery |
---|
16 | except: |
---|
17 | evalAdvancedQuery = None |
---|
18 | |
---|
19 | datamodel = datastructure.getDataModel() |
---|
20 | |
---|
21 | mapping = {} |
---|
22 | for key, value in datamodel.items(): |
---|
23 | if value: |
---|
24 | mapping[key] = value |
---|
25 | |
---|
26 | #mapping['homeless'] = True |
---|
27 | result_fields = context.getDirectoryResultFields(dir.getId(), |
---|
28 | dir.title_field) |
---|
29 | ##result_fields = [{'id': 'sn', 'title': 'label_last_name', 'sort': 'asc'}, |
---|
30 | ## {'id': 'givenName', 'title': 'label_first_name'}, |
---|
31 | ## {'id': 'email', 'title': 'label_email'}, |
---|
32 | ## {'id': id_field, 'title': 'label_user_name'}, |
---|
33 | ## ] |
---|
34 | |
---|
35 | return_fields = [] |
---|
36 | sort_by = None |
---|
37 | sort_direction = None |
---|
38 | process_fields = {} |
---|
39 | for field in result_fields: |
---|
40 | return_fields.append(field['id']) |
---|
41 | sorted = field.get('sort') |
---|
42 | if sorted == 'asc': |
---|
43 | sort_by = field['id'] |
---|
44 | sort_direction = 'asc' |
---|
45 | elif sorted == 'desc': |
---|
46 | sort_by = field['id'] |
---|
47 | sort_direction = 'desc' |
---|
48 | if field.get('process'): |
---|
49 | process_fields[field['id']] = field['process'] |
---|
50 | |
---|
51 | # empty search will not return anything |
---|
52 | if not mapping: |
---|
53 | return dir.search_members_results(results=[]), 'results' |
---|
54 | |
---|
55 | try: |
---|
56 | results = dir.searchEntries(return_fields=return_fields, **mapping) |
---|
57 | except SearchSizeLimitExceeded, e: |
---|
58 | rendered = dir.member_search_errors(exception=e) |
---|
59 | return rendered, 'results' |
---|
60 | |
---|
61 | for field, process_meth in process_fields.items(): |
---|
62 | meth = getattr(context, process_meth, None) |
---|
63 | if not meth: |
---|
64 | continue |
---|
65 | for item in results: |
---|
66 | value = item[1].get(field) |
---|
67 | item[1][field] = meth(field, value) |
---|
68 | |
---|
69 | if sort_by: |
---|
70 | items = [(str(item[1].get(sort_by, 'ZZZZ')).lower(), item) for item in results] |
---|
71 | items.sort() |
---|
72 | if sort_direction == 'desc': |
---|
73 | items.reverse() |
---|
74 | results = [item[1] for item in items] |
---|
75 | |
---|
76 | allres = [] |
---|
77 | for item in results: |
---|
78 | user_id = item[0] |
---|
79 | data = item[1] |
---|
80 | query = In('portal_type',('Faculty','Department')) &\ |
---|
81 | In('localUsersWithRoles',('user:%s' % user_id,)) |
---|
82 | res = evalAdvancedQuery(query) |
---|
83 | facs = [] |
---|
84 | roles = [] |
---|
85 | for r in res: |
---|
86 | p = r.getPath().split('/') |
---|
87 | fac = p[4] |
---|
88 | if len(p) == 5: |
---|
89 | facs.append(fac) |
---|
90 | roles.append(r) |
---|
91 | elif len(p) > 5: |
---|
92 | if fac not in facs: |
---|
93 | roles.append(r) |
---|
94 | item[1]['roles'] = roles |
---|
95 | rendered = dir.search_members_results(results=results) |
---|
96 | |
---|
97 | return rendered, 'results' |
---|
98 | |
---|
99 | |
---|