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

Last change on this file since 921 was 917, checked in by joachim, 18 years ago

=last change

  • Property svn:keywords set to Id
File size: 2.5 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 917 2006-11-21 18:25:51Z joachim $
11
12from Products.CPSDirectory.BaseDirectory import SearchSizeLimitExceeded
13try:
14    from Products.AdvancedQuery import Eq, Between, Le,In
15    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
16except:
17    evalAdvancedQuery = None
18
19datamodel = datastructure.getDataModel()
20
21mapping = {}
22for key, value in datamodel.items():
23    if value:
24        mapping[key] = value
25
26result_fields = context.getDirectoryResultFields(dir.getId(),
27                                                 dir.title_field)
28
29return_fields = []
30sort_by = None
31sort_direction = None
32process_fields = {}
33for 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
46if not mapping:
47    return dir.search_members_results(results=[]), 'results'
48
49try:
50    results = dir.searchEntries(return_fields=return_fields, **mapping)
51except SearchSizeLimitExceeded, e:
52    rendered = dir.cpsdirectory_entry_search_errors(exception=e)
53    return rendered, 'results'
54
55for 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
63if 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
70allres = []
71for 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
89rendered = dir.search_members_results(results=results)
90
91return rendered, 'results'
92
93
Note: See TracBrowser for help on using the repository browser.