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