## 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
form = request.form
fget = form.get
#wftool = context.portal_workflow
mtool = context.portal_membership
member = mtool.getAuthenticatedMember()
is_anon = mtool.isAnonymousUser()
#roles = member.getRolesInContext(context)
lt = context.portal_layouts
#pr = context.portal_registration
path_info = request.get('PATH_INFO').split('/')
allowed = True
if is_anon:
allowed = False
try:
from Products.AdvancedQuery import Eq, Between, Le,In
evalAdvancedQuery = context.portal_catalog.evalAdvancedQuery
aq_students = context.students_catalog.evalAdvancedQuery
except:
evalAdvancedQuery = None
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'] = []
#from Products.zdb import set_trace
#set_trace()
co_view = False
if "ClearanceOfficers" in member.getGroups():
state = "clearance_requested"
only_review = False
co_view = True
query = In('portal_type',('Faculty',)) &\
In('localUsersWithRoles', ("user:%s" % member,))
res = evalAdvancedQuery(query)
faculties = []
if res:
faculties = [f.getId for f in res]
user_info['faculties'] = faculties
query = In('portal_type',('Department',)) &\
In('localUsersWithRoles', ("user:%s" % member,))
res = evalAdvancedQuery(query)
departments = []
if res:
departments = [f.getId for f in res]
user_info['departments'] = departments
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",
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()):
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())
search_set = [r.id for r in res]
elif what in st_queries:
if what == "jamb_reg_no":
pt = ('StudentApplication',)
st = "%s" % term.strip().lower()
elif what == "matric_no":
pt = ('StudentClearance',)
st = "%s" % term.strip().lower()
elif what == "name":
pt = ('StudentPersonal')
st = "%s" % term.strip()
query = In('portal_type',pt) & Eq('SearchableText',"%s*" % term.strip())
res = evalAdvancedQuery(query)
if res:
for r in res:
pl = r.getPath().split('/')
search_set.append(pl[pl.index('students') + 1])
elif co_view:
#from Products.zdb import set_trace
#set_trace()
only_review = False
query = In('faculty',faculties) | In('department',departments)
res = aq_students(query)
search_set = [r.id for r in res]
if only_review or with_review:
query = Eq('review_state',state)
review_res = evalAdvancedQuery(query)
review_set = []
if review_res:
for r in review_res:
if r.portal_type == "StudentAccommodation":
start = r.getPath().find('/students/') + 10
review_set.append(r.getPath()[start:start+7])
else:
review_set.append(r.getId)
#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)[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 = "",
info = user_info,
students = students,
allowed = allowed,
co_view = co_view,
)
return context.search_students_form(rendered = rend,
psm = "No student found!",
info = user_info,
students = students,
allowed = allowed,
)