source: WAeUP_SRP/branches/joachim-event-branch/skins/waeup_student/search_students.py @ 17949

Last change on this file since 17949 was 1596, checked in by joachim, 18 years ago

merged changes from trunk to joachim-event-branch

File size: 10.7 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"""
14import logging
15logger = logging.getLogger('Skins.search_students')
16from DateTime import DateTime
17#
18#with_timer = True
19with_timer = False
20#
21request = REQUEST
22form = request.form
23fget = form.get
24mtool = context.portal_membership
25member = mtool.getAuthenticatedMember()
26is_anon = mtool.isAnonymousUser()
27lt = context.portal_layouts
28path_info = request.get('PATH_INFO').split('/')
29
30allowed = True
31if is_anon:
32    allowed = False
33try:
34    from Products.AdvancedQuery import Eq, Between, Le,In
35    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
36    aq_students = context.students_catalog.evalAdvancedQuery
37except:
38    evalAdvancedQuery = None
39
40def cmp_id(a,b):
41    if a.getId() > b.getId():
42        return 1
43    return -1
44
45student_subobjects = ("StudentApplication",
46                      "StudentPersonal",
47                      "StudentStudyCourse",
48                      "StudentAccommodation",
49                      "StudentStudyLevel",)
50
51#student_objects = student_subobjects + ("Student",)
52student_objects = ("Student",)
53
54user_info = {}
55items = []
56validate = request.has_key("cpsdocument_edit_button")
57
58state = "all"
59user_info['member'] = str(member)
60user_info['departments'] = []
61user_info['faculties'] = []
62co_view = False
63ca_view = False
64faculties =  fget('faculties')
65departments = fget('departments')
66certificate_level = fget('certificate_level')
67dep_str = fac_str = certificate_level_str = ''
68if "ClearanceOfficers" in member.getGroups():
69    state = "clearance_requested"
70    only_review = False
71    co_view = True
72    if faculties or departments:
73        #from Products.zdb import set_trace;set_trace()
74        if faculties:
75            fac_str = faculties
76            faculties = faculties.split()
77        if departments:
78            dep_str = departments
79            departments = departments.split()
80    else:
81        query = In('portal_type',('Faculty',)) &\
82                 In('localUsersWithRoles', ("user:%s" % member,))
83        res = evalAdvancedQuery(query)
84        logger.info('ClearanceOfficer %s initiated student_search' % member)
85        faculties = []
86        if res:
87            faculties = [f.getId for f in res]
88        user_info['faculties'] = faculties
89        fac_str = " ".join(faculties)
90        query = In('portal_type',('Department',)) &\
91                 In('localUsersWithRoles', ("user:%s" % member,))
92        res = evalAdvancedQuery(query)
93        departments = []
94        if res:
95            departments = [f.getId for f in res]
96        user_info['departments'] = departments
97        dep_str = " ".join(departments)
98elif "CourseAdvisers" in member.getGroups():
99    state = "courses_registered"
100    only_review = False
101    ca_view = True
102    if certificate_level:
103        #from Products.zdb import set_trace;set_trace()
104        certificate_level_str = certificate_level
105        certificate_level = certificate_level.split()
106    else:
107        query = In('portal_type',('StudyLevel',)) &\
108                 In('localUsersWithRoles', ("user:%s" % member,))
109        res = evalAdvancedQuery(query)
110        logger.info('CourseAdviser %s initiated student_search' % member)
111        certificate_level = []
112        if res:
113            certificate_level = ['/'.join(f.getPath().split('/')[-2:]) for f in res]
114        user_info['certificate_level'] = certificate_level
115        certificate_level_str = " ".join(certificate_level)
116default = {'search_mode': 'student_id',
117        'review_state': state,
118        'search_string': ''
119        }
120rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
121                      schema_id= 'student_search',
122                      context=context,
123                      mapping=validate and REQUEST,
124                      ob=default,
125                      layout_mode='edit',
126                      formaction="search_students",
127                      faculties = fac_str,
128                      departments = dep_str,
129                      certificate_level = certificate_level_str,
130                      commit = False,
131                      )
132if psm == '':
133    return context.search_students_form(rendered = rend,
134                             psm = psm,
135                             #psm = "%s, %s" % (psm,ds),
136                             info = user_info,
137                             students = [],
138                             allowed = allowed,
139                             )
140what = ds.get('search_mode')
141state = ds.get('review_state')
142st = term = ds.get('search_string')
143err = False
144with_review = state != "all"
145only_review = with_review and not term
146bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
147if not term and not with_review:
148    psm = "You must specify a search string when searching 'all states'!"
149    err = True
150elif '*' in term:
151    psm = "Wildcards are not supported!"
152    err = True
153if err:
154    return context.search_students_form(rendered = rend,
155                             psm = psm,
156                             #psm = "%s, %s" % (psm,ds),
157                             info = user_info,
158                             students = items,
159                             allowed = allowed,
160                             )
161#st_queries = ('jamb_reg_no','matric_no','name')
162review_res = None
163query = None
164items = []
165res = []
166review_set = []
167search_set = []
168if len(term) > 0:
169    if what == "student_id":
170        students_folder = context.portal_url.getPortalObject().campus.students
171        if hasattr(students_folder,term.strip()):
172            logger.info('%s searches for student with id %s' % (member,term))
173            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
174        return context.search_students_form(rendered = rend,
175                             psm = "No student found!",
176                             students = [],
177                             allowed = allowed,
178                             )
179    elif what == "department":
180        res = context.students_catalog(department=term.strip())
181        logger.info('%s searches for student in department %s' % (member,term))
182        search_set = [r.id for r in res]
183    elif what == "matric_no":
184        res = context.students_catalog(matric_no=term.strip())
185        logger.info('%s searches for student with matric_no %s' % (member,term))
186        search_set = [r.id for r in res]
187    elif what == "jamb_reg_no":
188        st_l = "%s" % term.strip().lower()
189        st_u = "%s" % term.strip().upper()
190        res = aq_students(In('jamb_reg_no',(st_l,st_u)))
191        logger.info('%s searches for student with jamb_reg_no %s' % (member,term))
192        search_set = [r.id for r in res]
193    elif what == "name":
194        pt = ('StudentPersonal')
195        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
196        logger.info('%s searches for student with name %s' % (member,term))
197        res = evalAdvancedQuery(query)
198        if res:
199            for r in res:
200                pl = r.getPath().split('/')
201                search_set.append(pl[pl.index('students') + 1])
202
203
204if only_review or with_review or co_view:
205    if with_timer:
206        start = DateTime().timeTime()
207        logger.info('"%s","start 100"' % member)
208    query = Eq('review_state',state)
209    review_res = evalAdvancedQuery(query)
210
211    logger.info('%s searches for students in review_state %s' % (member,state))
212
213    if with_timer:
214        end = DateTime().timeTime()
215        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
216if co_view:
217    only_review = False
218    with_review = True
219    if with_timer:
220        start = DateTime().timeTime()
221        logger.info('"%s","start 200"' % member)
222    query = In('faculty',faculties) | In('department',departments)
223    res = aq_students(query)
224    if with_timer:
225        end = DateTime().timeTime()
226        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
227    start = DateTime().timeTime()
228    search_set = [r.id for r in res]
229    if with_timer:
230        end = DateTime().timeTime()
231        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
232elif ca_view:
233    only_review = False
234    with_review = True
235    if with_timer:
236        start = DateTime().timeTime()
237        logger.info('"%s","start 200"' % member)
238    courses = [cl.split('/')[0] for cl in certificate_level]
239    levels = [cl.split('/')[1] for cl in certificate_level]
240    query = In('course',courses) & In('level',levels)
241    res = aq_students(query)
242    if with_timer:
243        end = DateTime().timeTime()
244        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
245        start = DateTime().timeTime()
246    search_set = [r.id for r in res]
247    if with_timer:
248        end = DateTime().timeTime()
249        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
250if with_timer:
251    logger.info('"%s","start 300"' % member)
252    start = DateTime().timeTime()
253if review_res:
254    review_set = [r.getId for r in review_res]
255if with_timer:
256    end = DateTime().timeTime()
257    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
258    logger.info('"%s","start 400"' % member)
259    start = DateTime().timeTime()
260if only_review:
261    all = review_set
262elif with_review:
263    all = []
264    for i in search_set:
265        if i in review_set:
266            all.append(i)
267else:
268    all = search_set
269if with_timer:
270    end = DateTime().timeTime()
271    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
272items = all[:500]
273students = []
274if with_timer:
275    logger.info('"%s","start 500"' % member)
276    start = DateTime().timeTime()
277if items:
278    for item in items:
279        stcat = context.students_catalog
280        record = stcat(id = item)
281        if record:
282            record = record[0]
283        else:
284            return item+' not found in students_catalog'
285        info = {}
286        for field in stcat.schema() + stcat.indexes():
287            info[field] = getattr(record, field)
288        else:
289            students.append(info)
290    if with_timer:
291        end = DateTime().timeTime()
292        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
293
294    return context.search_students_form(rendered = rend,
295                             psm = "",
296                             info = user_info,
297                             students = students,
298                             allowed = allowed,
299                             co_view = co_view,
300                             ca_view = ca_view
301                             )
302return context.search_students_form(rendered = rend,
303                             psm = "No student found!",
304                             info = user_info,
305                             students = students,
306                             allowed = allowed,
307                             )
308
309
Note: See TracBrowser for help on using the repository browser.