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

Last change on this file since 1589 was 1558, checked in by joachim, 18 years ago

merged trunk up to 1557

File size: 10.0 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('Search.Timer')
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        faculties = []
85        if res:
86            faculties = [f.getId for f in res]
87        user_info['faculties'] = faculties
88        fac_str = " ".join(faculties)
89        query = In('portal_type',('Department',)) &\
90                 In('localUsersWithRoles', ("user:%s" % member,))
91        res = evalAdvancedQuery(query)
92        departments = []
93        if res:
94            departments = [f.getId for f in res]
95        user_info['departments'] = departments
96        dep_str = " ".join(departments)
97elif "CourseAdvisers" in member.getGroups():
98    state = "courses_registered"
99    only_review = False
100    ca_view = True
101    if certificate_level:
102        #from Products.zdb import set_trace;set_trace()
103        certificate_level_str = certificate_level
104        certificate_level = certificate_level.split()
105    else:
106        query = In('portal_type',('StudyLevel',)) &\
107                 In('localUsersWithRoles', ("user:%s" % member,))
108        res = evalAdvancedQuery(query)
109        certificate_level = []
110        if res:
111            certificate_level = ['/'.join(f.getPath().split('/')[-2:]) for f in res]
112        user_info['certificate_level'] = certificate_level
113        certificate_level_str = " ".join(certificate_level)
114default = {'search_mode': 'student_id',
115        'review_state': state,
116        'search_string': ''
117        }
118rend,psm,ds = lt.renderLayout(layout_id= 'student_search',
119                      schema_id= 'student_search',
120                      context=context,
121                      mapping=validate and REQUEST,
122                      ob=default,
123                      layout_mode='edit',
124                      formaction="search_students",
125                      faculties = fac_str,
126                      departments = dep_str,
127                      certificate_level = certificate_level_str,
128                      commit = False,
129                      )
130if psm == '':
131    return context.search_students_form(rendered = rend,
132                             psm = psm,
133                             #psm = "%s, %s" % (psm,ds),
134                             info = user_info,
135                             students = [],
136                             allowed = allowed,
137                             )
138what = ds.get('search_mode')
139state = ds.get('review_state')
140st = term = ds.get('search_string')
141err = False
142with_review = state != "all"
143only_review = with_review and not term
144bools = "with_review = %s<br\> only_review = %s<br\>" % (with_review,only_review)
145if not term and not with_review:
146    psm = "You must specify a search string when searching 'all states'!"
147    err = True
148elif '*' in term:
149    psm = "Wildcards are not supported!"
150    err = True
151if err:
152    return context.search_students_form(rendered = rend,
153                             psm = psm,
154                             #psm = "%s, %s" % (psm,ds),
155                             info = user_info,
156                             students = items,
157                             allowed = allowed,
158                             )
159#st_queries = ('jamb_reg_no','matric_no','name')
160review_res = None
161query = None
162items = []
163res = []
164review_set = []
165search_set = []
166if len(term) > 0:
167    if what == "student_id":
168        students_folder = context.portal_url.getPortalObject().campus.students
169        if hasattr(students_folder,term.strip()):
170            request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
171        return context.search_students_form(rendered = rend,
172                             psm = "No student found!",
173                             students = [],
174                             allowed = allowed,
175                             )
176    elif what == "department":
177        res = context.students_catalog(department=term.strip())
178        search_set = [r.id for r in res]
179    elif what == "matric_no":
180        res = context.students_catalog(matric_no=term.strip())
181        search_set = [r.id for r in res]
182    elif what == "jamb_reg_no":
183        st_l = "%s" % term.strip().lower()
184        st_u = "%s" % term.strip().upper()
185        res = aq_students(In('jamb_reg_no',(st_l,st_u)))
186        search_set = [r.id for r in res]
187    elif what == "name":
188        pt = ('StudentPersonal')
189        query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
190        res = evalAdvancedQuery(query)
191        if res:
192            for r in res:
193                pl = r.getPath().split('/')
194                search_set.append(pl[pl.index('students') + 1])
195
196
197if only_review or with_review or co_view:
198    if with_timer:
199        start = DateTime().timeTime()
200        logger.info('"%s","start 100"' % member)
201    query = Eq('review_state',state)
202    review_res = evalAdvancedQuery(query)
203    if with_timer:
204        end = DateTime().timeTime()
205        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
206if co_view:
207    only_review = False
208    with_review = True
209    if with_timer:
210        start = DateTime().timeTime()
211        logger.info('"%s","start 200"' % member)
212    query = In('faculty',faculties) | In('department',departments)
213    res = aq_students(query)
214    if with_timer:
215        end = DateTime().timeTime()
216        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
217    start = DateTime().timeTime()
218    search_set = [r.id for r in res]
219    if with_timer:
220        end = DateTime().timeTime()
221        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
222elif ca_view:
223    only_review = False
224    with_review = True
225    if with_timer:
226        start = DateTime().timeTime()
227        logger.info('"%s","start 200"' % member)
228    courses = [cl.split('/')[0] for cl in certificate_level]
229    levels = [cl.split('/')[1] for cl in certificate_level]
230    query = In('course',courses) & In('level',levels)
231    res = aq_students(query)
232    if with_timer:
233        end = DateTime().timeTime()
234        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
235        start = DateTime().timeTime()
236    search_set = [r.id for r in res]
237    if with_timer:
238        end = DateTime().timeTime()
239        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
240if with_timer:
241    logger.info('"%s","start 300"' % member)
242    start = DateTime().timeTime()
243if review_res:
244    review_set = [r.getId for r in review_res]
245if with_timer:
246    end = DateTime().timeTime()
247    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
248    logger.info('"%s","start 400"' % member)
249    start = DateTime().timeTime()
250if only_review:
251    all = review_set
252elif with_review:
253    all = []
254    for i in search_set:
255        if i in review_set:
256            all.append(i)
257else:
258    all = search_set
259if with_timer:
260    end = DateTime().timeTime()
261    logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
262items = all[:500]
263students = []
264if with_timer:
265    logger.info('"%s","start 500"' % member)
266    start = DateTime().timeTime()
267if items:
268    for item in items:
269        stcat = context.students_catalog
270        record = stcat(id = item)
271        if record:
272            record = record[0]
273        else:
274            return item+' not found in students_catalog'
275        info = {}
276        for field in stcat.schema() + stcat.indexes():
277            info[field] = getattr(record, field)
278        else:
279            students.append(info)
280    if with_timer:
281        end = DateTime().timeTime()
282        logger.info('"%s","searchtime","%6.2f"' % (member,end-start))
283
284    return context.search_students_form(rendered = rend,
285                             psm = "",
286                             info = user_info,
287                             students = students,
288                             allowed = allowed,
289                             co_view = co_view,
290                             )
291return context.search_students_form(rendered = rend,
292                             psm = "No student found!",
293                             info = user_info,
294                             students = students,
295                             allowed = allowed,
296                             )
297
298
Note: See TracBrowser for help on using the repository browser.