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

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

fixed

  • 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 1962 2007-06-26 04:51:01Z henrik $
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
32#result_fields = context.getDirectoryResultFields(dir.getId(),
33#       
34id_field = getattr(dir, 'id_field', 'id')
35result_fields = [{'id': 'sn', 'title': 'Last Name', 'sort': 'asc'},
36                  {'id': 'givenName', 'title': 'First Name'},
37                  {'id': 'email', 'title': 'Email Address'},
38                  {'id': id_field, 'title': 'User Name'},
39                  ]
40if dir.getId() == "members":
41    result_fields.append({'id': 'groups', 'title': 'Group(s)'})
42
43return_fields = []
44sort_by = None
45sort_direction = None
46process_fields = {}
47for field in result_fields:
48    return_fields.append(field['id'])
49    sorted = field.get('sort')
50    if sorted == 'asc':
51        sort_by = field['id']
52        sort_direction = 'asc'
53    elif sorted == 'desc':
54        sort_by = field['id']
55        sort_direction = 'desc'
56    if field.get('process'):
57        process_fields[field['id']] = field['process']
58
59# empty search will not return anything
60if not mapping:
61    return dir.search_members_results(results=[]), 'results'
62
63try:
64    results = dir.searchEntries(return_fields=return_fields, **mapping)
65except SearchSizeLimitExceeded, e:
66    rendered = dir.member_search_errors(exception=e)
67    return rendered, 'results'
68for field, process_meth in process_fields.items():
69    meth = getattr(context, process_meth, None)
70    if not meth:
71        continue
72    for item in results:
73        value = item[1].get(field)
74        item[1][field] = meth(field, value)
75
76if sort_by:
77    items = [(str(item[1].get(sort_by, 'ZZZZ')).lower(), item) for item in results]
78    items.sort()
79    if sort_direction == 'desc':
80        items.reverse()
81    results = [item[1] for item in items]
82
83allres = []
84for item in results:
85    user_id = item[0]
86    data = item[1]
87    query = In('portal_type',('Faculty','Department','StudyLevel')) &\
88            In('localUsersWithRoles',('user:%s' % user_id,)) &\
89            ~ Eq('Creator',user_id)
90    res = aq_portal(query)
91    facs = []
92    roles = []
93    for r in res:
94        p = r.getPath().split('/')
95        fac = p[4]
96        if len(p) == 5:
97            facs.append(fac)
98            roles.append(r)
99        elif len(p) > 5:
100            if fac not in facs:
101                roles.append(r)
102    item[1]['roles'] = roles
103    if "groups" in return_fields:
104        item[1]['groups'] = " ".join(item[1]['groups'])
105
106rendered = dir.search_members_results(results=results,result_fields=result_fields)
107
108return rendered, 'results'
109
Note: See TracBrowser for help on using the repository browser.