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

Last change on this file since 1522 was 1500, checked in by joachim, 18 years ago

M skins/waeup_student/search_students.py
allow search for caseindependent jamb_reg_no
M skins/waeup_student/getStudyCourseInfo.py
fix typo

File size: 8.5 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
15logger = logging.getLogger('Search.Timer')
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
[1278]63faculties =  fget('faculties')
64departments = fget('departments')
65dep_str = fac_str = ''
[1006]66if "ClearanceOfficers" in member.getGroups():
67    state = "clearance_requested"
[1065]68    only_review = False
69    co_view = True
[1278]70    if faculties or departments:
71        #from Products.zdb import set_trace;set_trace()
72        if faculties:
73            fac_str = faculties
74            faculties = faculties.split()
75        if departments:
76            dep_str = departments
77            departments = departments.split()
78    else:
79        query = In('portal_type',('Faculty',)) &\
80                 In('localUsersWithRoles', ("user:%s" % member,))
81        res = evalAdvancedQuery(query)
82        faculties = []
83        if res:
84            faculties = [f.getId for f in res]
85        user_info['faculties'] = faculties
86        fac_str = " ".join(faculties)
87        query = In('portal_type',('Department',)) &\
88                 In('localUsersWithRoles', ("user:%s" % member,))
89        res = evalAdvancedQuery(query)
90        departments = []
91        if res:
92            departments = [f.getId for f in res]
93        user_info['departments'] = departments
94        dep_str = " ".join(departments)
[1071]95default = {'search_mode': 'student_id',
[1006]96        'review_state': state,
[920]97        'search_string': ''
98        }
99rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
100                      schema_id= 'student_search',
101                      context=context,
[1192]102                      mapping=validate and REQUEST,
[920]103                      ob=default,
104                      layout_mode='edit',
[971]105                      formaction="search_students",
[1278]106                      faculties = fac_str,
107                      departments = dep_str,
[971]108                      commit = False,
[920]109                      )
110if psm == '':
111    return context.search_students_form(rendered = rend,
112                             psm = psm,
113                             #psm = "%s, %s" % (psm,ds),
[1065]114                             info = user_info,
[971]115                             students = [],
[1096]116                             allowed = allowed,
[920]117                             )
118what = ds.get('search_mode')
119state = ds.get('review_state')
120st = term = ds.get('search_string')
121err = False
122with_review = state != "all"
123only_review = with_review and not term
124bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
125if not term and not with_review:
126    psm = "You must specify a search string when searching 'all states'!"
127    err = True
128elif '*' in term:
129    psm = "Wildcards are not supported!"
130    err = True
131if err:
132    return context.search_students_form(rendered = rend,
133                             psm = psm,
134                             #psm = "%s, %s" % (psm,ds),
[1065]135                             info = user_info,
[920]136                             students = items,
[1096]137                             allowed = allowed,
[920]138                             )
[1479]139#st_queries = ('jamb_reg_no','matric_no','name')
[920]140review_res = None
141query = None
[1071]142items = []
143res = []
[1034]144review_set = []
[1043]145search_set = []
[920]146if len(term) > 0:
147    if what == "student_id":
[1065]148        students_folder = context.portal_url.getPortalObject().campus.students
[1033]149        if hasattr(students_folder,term.strip()):
150            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
151        return context.search_students_form(rendered = rend,
152                             psm = "No student found!",
153                             students = [],
[1096]154                             allowed = allowed,
[1033]155                             )
[1034]156    elif what == "department":
157        res = context.students_catalog(department=term.strip())
158        search_set = [r.id for r in res]
[1278]159    elif what == "matric_no":
160        res = context.students_catalog(matric_no=term.strip())
161        search_set = [r.id for r in res]
[1479]162    elif what == "jamb_reg_no":
[1500]163        st_l = "%s" % term.strip().lower()
164        st_u = "%s" % term.strip().upper()
165        res = aq_students(In('jamb_reg_no',(st_l,st_u)))
[1479]166        search_set = [r.id for r in res]
167    elif what == "name":
168        pt = ('StudentPersonal')
[920]169        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
[1033]170        res = evalAdvancedQuery(query)
171        if res:
172            for r in res:
173                pl = r.getPath().split('/')
174                search_set.append(pl[pl.index('students') + 1])
[1479]175
176
[1278]177if only_review or with_review or co_view:
178    if with_timer:
179        start = DateTime().timeTime()
180        logger.info('"%s","start 100"' % member)
181    query = Eq('review_state',state)
182    review_res = evalAdvancedQuery(query)
183    if with_timer:
184        end = DateTime().timeTime()
185        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
186if co_view:
[1065]187    only_review = False
[1278]188    with_review = True
189    if with_timer:
190        start = DateTime().timeTime()
191        logger.info('"%s","start 200"' % member)
[1096]192    query = In('faculty',faculties) | In('department',departments)
[1065]193    res = aq_students(query)
[1278]194    if with_timer:
195        end = DateTime().timeTime()
196        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
197    start = DateTime().timeTime()
[1065]198    search_set = [r.id for r in res]
[1278]199    if with_timer:
200        end = DateTime().timeTime()
201        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
202if with_timer:
203    logger.info('"%s","start 300"' % member)
204    start = DateTime().timeTime()
[1034]205if review_res:
[1278]206    review_set = [r.getId for r in review_res]
207if with_timer:
208    end = DateTime().timeTime()
209    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
210    logger.info('"%s","start 400"' % member)
211    start = DateTime().timeTime()
[1073]212if only_review:
213    all = review_set
214elif with_review:
215    all = []
216    for i in search_set:
217        if i in review_set:
218            all.append(i)
219else:
220    all = search_set
[1278]221if with_timer:
222    end = DateTime().timeTime()
223    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
[1065]224items = all[:500]
[920]225students = []
[1278]226if with_timer:
227    logger.info('"%s","start 500"' % member)
228    start = DateTime().timeTime()
[920]229if items:
230    for item in items:
[1006]231        stcat = context.students_catalog
[1446]232        record = stcat(id = item)
233        if record:
234            record = record[0]
235        else:
236            return item+' not found in students_catalog'
[971]237        info = {}
[1006]238        for field in stcat.schema() + stcat.indexes():
239            info[field] = getattr(record, field)
[920]240        else:
[1011]241            students.append(info)
[1278]242    if with_timer:
243        end = DateTime().timeTime()
244        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
[920]245
246    return context.search_students_form(rendered = rend,
247                             psm = "",
[1065]248                             info = user_info,
[920]249                             students = students,
[1096]250                             allowed = allowed,
[920]251                             co_view = co_view,
252                             )
253return context.search_students_form(rendered = rend,
254                             psm = "No student found!",
[1065]255                             info = user_info,
[920]256                             students = students,
[1096]257                             allowed = allowed,
[920]258                             )
259
260
Note: See TracBrowser for help on using the repository browser.