source: WAeUP_SRP/trunk/skins/waeup_directory/search_members.py @ 1917

Last change on this file since 1917 was 1903, checked in by joachim, 17 years ago

show roles for courseadvisors

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
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
12from Products.CPSDirectory.BaseDirectory import SearchSizeLimitExceeded
13from Products.AdvancedQuery import Eq, Between, Le,In
14try:
15    from Products.zdb import set_trace
16except:
17    def set_trace():
18        pass
19try:
20    aq_portal = context.portal_catalog.evalAdvancedQuery
21except:
22    aq_portal = context.portal_catalog_real.evalAdvancedQuery
23
24datamodel = datastructure.getDataModel()
25
26mapping = {}
27for key, value in datamodel.items():
28    if value:
29        mapping[key] = value
30
31#mapping['homeless'] = True
32result_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##                  ]
39if dir.getId() == "members":
40    result_fields.append({'id': 'groups', 'title': 'Group(s)'})
41
42return_fields = []
43sort_by = None
44sort_direction = None
45process_fields = {}
46for 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
59if not mapping:
60    return dir.search_members_results(results=[]), 'results'
61
62try:
63    results = dir.searchEntries(return_fields=return_fields, **mapping)
64except SearchSizeLimitExceeded, e:
65    rendered = dir.member_search_errors(exception=e)
66    return rendered, 'results'
67for 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
75if 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
82allres = []
83for 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       
104rendered = dir.search_members_results(results=results,result_fields=result_fields)
105
106return rendered, 'results'
107
108
Note: See TracBrowser for help on using the repository browser.