source: WAeUP_SRP/branches/joachim-event-branch/skins/waeup_directory/search_members.py @ 17421

Last change on this file since 17421 was 953, checked in by Henrik Bettermann, 18 years ago

member directory acl permission, schema and layouts changed
not yet optimized

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