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