source: WAeUP_SRP/branches/uli/skins/waeup_student/search_students.py @ 2932

Last change on this file since 2932 was 1593, checked in by uli, 18 years ago

Merged current trunk into uli-branch.

File size: 10.7 KB
RevLine 
[920]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"""
[1278]14import logging
[1593]15logger = logging.getLogger('Skins.search_students')
[1278]16from DateTime import DateTime
17#
18#with_timer = True
19with_timer = False
20#
[920]21request = REQUEST
[1033]22form = request.form
23fget = form.get
[920]24mtool = context.portal_membership
25member = mtool.getAuthenticatedMember()
[1096]26is_anon = mtool.isAnonymousUser()
[1065]27lt = context.portal_layouts
28path_info = request.get('PATH_INFO').split('/')
[1096]29
30allowed = True
31if is_anon:
32    allowed = False
[920]33try:
34    from Products.AdvancedQuery import Eq, Between, Le,In
35    evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
[1065]36    aq_students = context.students_catalog.evalAdvancedQuery
[920]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
[1065]51#student_objects = student_subobjects + ("Student",)
52student_objects = ("Student",)
[920]53
[1065]54user_info = {}
[920]55items = []
56validate = request.has_key("cpsdocument_edit_button")
[1006]57
58state = "all"
[1065]59user_info['member'] = str(member)
60user_info['departments'] = []
61user_info['faculties'] = []
62co_view = False
[1593]63ca_view = False
[1278]64faculties =  fget('faculties')
65departments = fget('departments')
[1593]66certificate_level = fget('certificate_level')
67dep_str = fac_str = certificate_level_str = ''
[1006]68if "ClearanceOfficers" in member.getGroups():
69    state = "clearance_requested"
[1065]70    only_review = False
71    co_view = True
[1278]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)
[1593]84        logger.info('ClearanceOfficer %s initiated student_search' % member)
[1278]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)
[1593]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)
[1071]116default = {'search_mode': 'student_id',
[1006]117        'review_state': state,
[920]118        'search_string': ''
119        }
120rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
121                      schema_id= 'student_search',
122                      context=context,
[1192]123                      mapping=validate and REQUEST,
[920]124                      ob=default,
125                      layout_mode='edit',
[971]126                      formaction="search_students",
[1278]127                      faculties = fac_str,
128                      departments = dep_str,
[1593]129                      certificate_level = certificate_level_str,
[971]130                      commit = False,
[920]131                      )
132if psm == '':
133    return context.search_students_form(rendered = rend,
134                             psm = psm,
135                             #psm = "%s, %s" % (psm,ds),
[1065]136                             info = user_info,
[971]137                             students = [],
[1096]138                             allowed = allowed,
[920]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),
[1065]157                             info = user_info,
[920]158                             students = items,
[1096]159                             allowed = allowed,
[920]160                             )
[1479]161#st_queries = ('jamb_reg_no','matric_no','name')
[920]162review_res = None
163query = None
[1071]164items = []
165res = []
[1034]166review_set = []
[1043]167search_set = []
[920]168if len(term) > 0:
169    if what == "student_id":
[1065]170        students_folder = context.portal_url.getPortalObject().campus.students
[1033]171        if hasattr(students_folder,term.strip()):
[1593]172            logger.info('%s searches for student with id %s' % (member,term))
[1033]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 = [],
[1096]177                             allowed = allowed,
[1033]178                             )
[1034]179    elif what == "department":
180        res = context.students_catalog(department=term.strip())
[1593]181        logger.info('%s searches for student in department %s' % (member,term))
[1034]182        search_set = [r.id for r in res]
[1278]183    elif what == "matric_no":
184        res = context.students_catalog(matric_no=term.strip())
[1593]185        logger.info('%s searches for student with matric_no %s' % (member,term))
[1278]186        search_set = [r.id for r in res]
[1479]187    elif what == "jamb_reg_no":
[1500]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)))
[1593]191        logger.info('%s searches for student with jamb_reg_no %s' % (member,term))
[1479]192        search_set = [r.id for r in res]
193    elif what == "name":
194        pt = ('StudentPersonal')
[920]195        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
[1593]196        logger.info('%s searches for student with name %s' % (member,term))
[1033]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])
[1479]202
203
[1278]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)
[1593]210
211    logger.info('%s searches for students in review_state %s' % (member,state))
212
[1278]213    if with_timer:
214        end = DateTime().timeTime()
215        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
216if co_view:
[1065]217    only_review = False
[1278]218    with_review = True
219    if with_timer:
220        start = DateTime().timeTime()
221        logger.info('"%s","start 200"' % member)
[1096]222    query = In('faculty',faculties) | In('department',departments)
[1065]223    res = aq_students(query)
[1278]224    if with_timer:
225        end = DateTime().timeTime()
226        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
227    start = DateTime().timeTime()
[1065]228    search_set = [r.id for r in res]
[1278]229    if with_timer:
230        end = DateTime().timeTime()
231        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
[1593]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))
[1278]250if with_timer:
251    logger.info('"%s","start 300"' % member)
252    start = DateTime().timeTime()
[1034]253if review_res:
[1278]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()
[1073]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
[1278]269if with_timer:
270    end = DateTime().timeTime()
271    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
[1065]272items = all[:500]
[920]273students = []
[1278]274if with_timer:
275    logger.info('"%s","start 500"' % member)
276    start = DateTime().timeTime()
[920]277if items:
278    for item in items:
[1006]279        stcat = context.students_catalog
[1446]280        record = stcat(id = item)
281        if record:
282            record = record[0]
283        else:
284            return item+' not found in students_catalog'
[971]285        info = {}
[1006]286        for field in stcat.schema() + stcat.indexes():
287            info[field] = getattr(record, field)
[920]288        else:
[1011]289            students.append(info)
[1278]290    if with_timer:
291        end = DateTime().timeTime()
292        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
[920]293
294    return context.search_students_form(rendered = rend,
295                             psm = "",
[1065]296                             info = user_info,
[920]297                             students = students,
[1096]298                             allowed = allowed,
[920]299                             co_view = co_view,
[1593]300                             ca_view = ca_view
[920]301                             )
302return context.search_students_form(rendered = rend,
303                             psm = "No student found!",
[1065]304                             info = user_info,
[920]305                             students = students,
[1096]306                             allowed = allowed,
[920]307                             )
308
309
Note: See TracBrowser for help on using the repository browser.