## Script (Python) "search_students" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=REQUEST ##title= ## # $Id: search_students.py 911 2006-11-20 15:11:29Z henrik $ """ list Students for ClearanceOfficers """ request = REQUEST wftool = context.portal_workflow mtool = context.portal_membership member = mtool.getAuthenticatedMember() roles = member.getRolesInContext(context) try: from Products.AdvancedQuery import Eq, Between, Le,In evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery except: evalAdvancedQuery = None def cmp_id(a,b): if a.getId() > b.getId(): return 1 return -1 request = context.REQUEST form = request.form fget = form.get student_subobjects = ("StudentApplication", "StudentPersonal", "StudentStudyCourse", "StudentAccommodation", "StudentStudyLevel",) student_objects = student_subobjects + ("Student",) info = {} items = [] wf = context.portal_workflow #student_wf_states = wf.waeup_student_wf.states.keys() #info['wf_states'] = student_wf_states #info['wf_states'][0] = " ----- " lt = context.portal_layouts pr = context.portal_registration path_info = request.get('PATH_INFO').split('/') validate = request.has_key("cpsdocument_edit_button") state = "all" if "ClearanceOfficers" in member.getGroups(): state = "clearance_requested" default = {'search_mode': 'name', 'review_state': state, 'search_string': '' } #from Products.zdb import set_trace #set_trace() rend,psm,ds = lt.renderLayout(layout_id= 'student_search', schema_id= 'student_search', context=context, mapping=validate and request, ob=default, layout_mode='edit', formaction="search_students", commit = False, ) if psm == '': return context.search_students_form(rendered = rend, psm = psm, #psm = "%s, %s" % (psm,ds), students = [], allowed = True, ) #set_trace() what = ds.get('search_mode') state = ds.get('review_state') st = term = ds.get('search_string') err = False with_review = state != "all" only_review = with_review and not term ##with_level_results = state.startswith("category") or\ ## state in ('content_addable',) and\ ## evalAdvancedQuery is not None bools = "with_review = %s only_review = %s" % (with_review,only_review) if not term and not with_review: psm = "You must specify a search string when searching 'all states'!" err = True elif '*' in term: psm = "Wildcards are not supported!" err = True if err: return context.search_students_form(rendered = rend, psm = psm, #psm = "%s, %s" % (psm,ds), students = items, allowed = True, ) items = [] res = [] portal_type_query = {'query':['Student','StudentApplication','StudentPersonal']} st_queries = ('jamb_reg_no','matric_no','name') query_step = 0 review_res = None query = None if len(term) > 0: if what == "student_id": query_step = 1 query = Eq('portal_type','Student') & Eq('id', term.strip()) elif what in st_queries: if what == "jamb_reg_no": query_step = 2 pt = ('StudentApplication',) st = "%s" % term.strip().lower() elif what == "matric_no": query_step = 3 pt = ('StudentClearance',) st = "%s" % term.strip().lower() elif what == "name": query_step = 4 pt = ('StudentPersonal') st = "%s" % term.strip() query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip()) res = evalAdvancedQuery(query) if with_review: query_step += 10 review_res = evalAdvancedQuery(In('portal_type',student_objects) & Eq('review_state',state)) search_set = [] if res: for r in res: pl = r.getPath().split('/') search_set.append(pl[pl.index('students') + 1]) review_set = [] if review_res: for r in review_res: pl = r.getPath().split('/') review_set.append(pl[pl.index('students') + 1]) all = [] if only_review: all = review_set elif with_review: for i in search_set: if i in review_set: all.append(i) else: all = search_set for a in all[:100]: if a in items: continue items.append(a) students = [] items.sort() co_view = False if items: for item in items: stcat = context.students_catalog record = stcat(id = item)[0] info = {} for field in stcat.schema() + stcat.indexes(): info[field] = getattr(record, field) if "ClearanceOfficers" in member.getGroups(): co_view = True res = context.portal_catalog(portal_type='Student', id = item) if len(res) != 1: continue droles = member.getRolesInContext(res[0].getObject()) info['review_state'] = res[0].review_state if "ClearanceOfficer" in droles: students.append(info) else: students.append(info) return context.search_students_form(rendered = rend, psm = "", students = students, allowed = True, co_view = co_view, ) return context.search_students_form(rendered = rend, psm = "No student found!", students = students, allowed = True, )