## 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
"""
import logging
logger = logging.getLogger('Skins.search_students')
#from DateTime import DateTime
#
#with_timer = True
#with_timer = False
#
request = REQUEST
form = request.form
fget = form.get
mtool = context.portal_membership
member = mtool.getAuthenticatedMember()
is_anon = mtool.isAnonymousUser()
lt = context.portal_layouts
path_info = request.get('PATH_INFO').split('/')
allowed = True
if is_anon:
allowed = False
from Products.AdvancedQuery import Eq, Between, Le,In
try:
aq_portal = context.portal_catalog.evalAdvancedQuery
except:
aq_portal = context.portal_catalog_real.evalAdvancedQuery
aq_students = context.students_catalog.evalAdvancedQuery
def cmp_id(a,b):
if a.getId() > b.getId():
return 1
return -1
student_subobjects = ("StudentApplication",
"StudentPersonal",
"StudentStudyCourse",
"StudentAccommodation",
"StudentStudyLevel",)
#student_objects = student_subobjects + ("Student",)
student_objects = ("Student",)
user_info = {}
items = []
validate = request.has_key("cpsdocument_edit_button")
state = "all"
user_info['member'] = str(member)
user_info['departments'] = []
user_info['faculties'] = []
co_view = False
ca_view = False
faculties = fget('faculties')
departments = fget('departments')
certificate_levels = fget('certificate_levels')
dep_str = fac_str = certificate_levels_str = ''
if "ClearanceOfficers" in member.getGroups():
state = "clearance_requested"
only_review = False
co_view = True
if faculties or departments:
#from Products.zdb import set_trace;set_trace()
if faculties:
fac_str = faculties
faculties = faculties.split()
if departments:
dep_str = departments
departments = departments.split()
else:
query = In('portal_type',('Faculty',)) &\
In('localUsersWithRoles', ("user:%s" % member,))
res = aq_portal(query)
logger.info('ClearanceOfficer %s initiated student_search' % member)
faculties = []
if res:
faculties = [f.getId for f in res]
user_info['faculties'] = faculties
fac_str = " ".join(faculties)
query = In('portal_type',('Department',)) &\
In('localUsersWithRoles', ("user:%s" % member,))
res = aq_portal(query)
departments = []
if res:
departments = [f.getId for f in res]
user_info['departments'] = departments
dep_str = " ".join(departments)
elif "CourseAdvisers" in member.getGroups():
state = "courses_registered"
only_review = False
ca_view = True
if certificate_levels:
#from Products.zdb import set_trace;set_trace()
certificate_levels_str = certificate_levels
certificate_levels = certificate_levels.split()
else:
query = In('portal_type',('StudyLevel',)) &\
In('localUsersWithRoles', ("user:%s" % member,))
res = aq_portal(query)
logger.info('CourseAdviser %s initiated student_search' % member)
certificate_levels = []
if res:
certificate_levels = ['/'.join(f.getPath().split('/')[-2:]) for f in res]
user_info['certificate_levels'] = certificate_levels
certificate_levels_str = " ".join(certificate_levels)
default = {'search_mode': 'student_id',
'review_state': state,
'search_string': ''
}
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",
faculties = fac_str,
departments = dep_str,
certificate_levels = certificate_levels_str,
commit = False,
)
if psm == '':
return context.search_students_form(rendered = rend,
psm = psm,
#psm = "%s, %s" % (psm,ds),
info = user_info,
students = [],
allowed = allowed,
)
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
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),
info = user_info,
students = items,
allowed = allowed,
)
#st_queries = ('jamb_reg_no','matric_no','name')
review_res = None
query = None
items = []
res = []
review_set = []
search_set = []
if len(term) > 0:
if what == "student_id":
students_folder = context.portal_url.getPortalObject().campus.students
if hasattr(students_folder,term.strip()):
logger.info('%s searches for student with id %s' % (member,term))
request.RESPONSE.redirect("%s/%s" % (students_folder.absolute_url(),term))
return context.search_students_form(rendered = rend,
psm = "No student found!",
students = [],
allowed = allowed,
)
elif what == "department":
res = context.students_catalog(department=term.strip())
logger.info('%s searches for student in department %s' % (member,term))
search_set = [r.id for r in res]
elif what == "matric_no":
res = context.students_catalog(matric_no=term.strip())
logger.info('%s searches for student with matric_no %s' % (member,term))
search_set = [r.id for r in res]
elif what == "jamb_reg_no":
st_l = "%s" % term.strip().lower()
st_u = "%s" % term.strip().upper()
res = aq_students(In('jamb_reg_no',(st_l,st_u)))
logger.info('%s searches for student with jamb_reg_no %s' % (member,term))
search_set = [r.id for r in res]
elif what == "name":
pt = ('StudentPersonal')
query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
logger.info('%s searches for student with name %s' % (member,term))
res = aq_portal(query)
if res:
for r in res:
pl = r.getPath().split('/')
search_set.append(pl[pl.index('students') + 1])
if only_review or with_review or co_view:
#query = Eq('review_state',state)
#review_res = aq_portal(query)
#review_res = context.portal_catalog(review_state = state)
query = Eq('review_state',state)
review_res = aq_students(query)
logger.info('%s searches for students in review_state %s' % (member,state))
if co_view:
only_review = False
with_review = True
query = In('faculty',faculties) | In('department',departments)
res = aq_students(query)
search_set = [r.id for r in res]
elif ca_view:
only_review = False
with_review = True
courses = [cl.split('/')[0] for cl in certificate_levels]
levels = [cl.split('/')[1] for cl in certificate_levels]
query = In('course',courses) & In('level',levels)
res = aq_students(query)
search_set = [r.id for r in res]
if review_res:
review_set = [r.id for r in review_res]
#from Products.zdb import set_trace;set_trace()
if only_review:
all = review_set
elif with_review:
all = []
for i in search_set:
if i in review_set:
all.append(i)
else:
all = search_set
items = all[:500]
students = []
if items:
for item in items:
stcat = context.students_catalog
record = stcat(id = item)
if record:
record = record[0]
else:
return item+' not found in students_catalog'
info = {}
for field in stcat.schema() + stcat.indexes():
info[field] = getattr(record, field)
else:
students.append(info)
return context.search_students_form(rendered = rend,
psm = "",
info = user_info,
students = students,
allowed = allowed,
co_view = co_view,
ca_view = ca_view
)
return context.search_students_form(rendered = rend,
psm = "No student found!",
info = user_info,
students = students,
allowed = allowed,
)