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