source: WAeUP_SRP/trunk/skins/waeup_student/search_students.py @ 1021

Last change on this file since 1021 was 1011, checked in by joachim, 18 years ago

remove len check

File size: 5.9 KB
Line 
1## Script (Python) "search_students"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=REQUEST
8##title=
9##
10# $Id: search_students.py 911 2006-11-20 15:11:29Z henrik $
11"""
12list Students for ClearanceOfficers
13"""
14request = REQUEST
15wftool = context.portal_workflow
16mtool = context.portal_membership
17member = mtool.getAuthenticatedMember()
18roles = member.getRolesInContext(context)
19try:
20    from Products.AdvancedQuery import Eq, Between, Le,In
21    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
22except:
23    evalAdvancedQuery = None
24
25def cmp_id(a,b):
26    if a.getId() > b.getId():
27        return 1
28    return -1
29
30request = context.REQUEST
31form = request.form
32fget = form.get
33student_subobjects = ("StudentApplication",
34                      "StudentPersonal",
35                      "StudentStudyCourse",
36                      "StudentAccommodation",
37                      "StudentStudyLevel",)
38
39student_objects = student_subobjects + ("Student",)
40
41info = {}
42items = []
43wf = context.portal_workflow
44#student_wf_states = wf.waeup_student_wf.states.keys()
45#info['wf_states'] = student_wf_states
46#info['wf_states'][0] = " ----- "
47lt = context.portal_layouts
48pr = context.portal_registration
49path_info = request.get('PATH_INFO').split('/')
50validate = request.has_key("cpsdocument_edit_button")
51
52state = "all"
53if "ClearanceOfficers" in member.getGroups():
54    state = "clearance_requested"
55default = {'search_mode': 'name',
56        'review_state': state,
57        'search_string': ''
58        }
59#from Products.zdb import set_trace
60#set_trace()
61rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
62                      schema_id= 'student_search',
63                      context=context,
64                      mapping=validate and request,
65                      ob=default,
66                      layout_mode='edit',
67                      formaction="search_students",
68                      commit = False,
69                      )
70if psm == '':
71    return context.search_students_form(rendered = rend,
72                             psm = psm,
73                             #psm = "%s, %s" % (psm,ds),
74                             students = [],
75                             allowed = True,
76                             )
77#set_trace()
78what = ds.get('search_mode')
79state = ds.get('review_state')
80st = term = ds.get('search_string')
81err = False
82with_review = state != "all"
83only_review = with_review and not term
84##with_level_results = state.startswith("category") or\
85##                     state in ('content_addable',) and\
86##                     evalAdvancedQuery is not None
87bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
88if not term and not with_review:
89    psm = "You must specify a search string when searching 'all states'!"
90    err = True
91elif '*' in term:
92    psm = "Wildcards are not supported!"
93    err = True
94if err:
95    return context.search_students_form(rendered = rend,
96                             psm = psm,
97                             #psm = "%s, %s" % (psm,ds),
98                             students = items,
99                             allowed = True,
100                             )
101items = []
102res = []
103portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']}
104st_queries = ('jamb_reg_no','matric_no','name')
105query_step = 0
106review_res = None
107query = None
108if len(term) > 0:
109    if what == "student_id":
110        query_step = 1
111        query = Eq('portal_type','Student') & Eq('id', term.strip())
112    elif what in st_queries:
113        if what == "jamb_reg_no":
114            query_step = 2
115            pt = ('StudentApplication',)
116            st = "%s" % term.strip().lower()
117        elif what == "matric_no":
118            query_step = 3
119            pt = ('StudentClearance',)
120            st = "%s" % term.strip().lower()
121        elif what == "name":
122            query_step = 4
123            pt = ('StudentPersonal')
124            st = "%s" % term.strip()
125        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
126    res = evalAdvancedQuery(query)
127if with_review:
128    query_step += 10
129    review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state))
130search_set = []
131if res:
132    for r in res:
133        pl = r.getPath().split('/')
134        search_set.append(pl[pl.index('students') + 1])
135review_set = []
136if review_res:
137    for r in review_res:
138        pl = r.getPath().split('/')
139        review_set.append(pl[pl.index('students') + 1])
140all = []
141if only_review:
142    all = review_set
143elif with_review:
144    for i in search_set:
145        if i in review_set:
146            all.append(i)
147else:
148    all = search_set
149for a in all[:100]:
150    if a in items:
151        continue
152    items.append(a)
153students = []
154items.sort()
155co_view = False
156if items:
157    for item in items:
158        stcat = context.students_catalog
159        record = stcat(id = item)[0]
160        info = {}
161        for field in stcat.schema() + stcat.indexes():
162            info[field] = getattr(record, field)
163        if "ClearanceOfficers" in member.getGroups():
164            co_view = True
165            res = context.portal_catalog(portal_type='Student', id = item)
166            if len(res) != 1:
167                continue
168            droles = member.getRolesInContext(res[0].getObject())
169            info['review_state'] = res[0].review_state
170            if "ClearanceOfficer" in droles:
171                students.append(info)
172        else:
173            students.append(info)
174
175    return context.search_students_form(rendered = rend,
176                             psm = "",
177                             students = students,
178                             allowed = True,
179                             co_view = co_view,
180                             )
181return context.search_students_form(rendered = rend,
182                             psm = "No student found!",
183                             students = students,
184                             allowed = True,
185                             )
186
187
Note: See TracBrowser for help on using the repository browser.