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

Last change on this file since 1857 was 1845, checked in by joachim, 18 years ago

modifications to use QueueCatalog?

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