## Script (Python) "hall_allocation_list"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=student=None
##title=
##
# $Id: list_students.py 5163 2010-04-21 04:38:00Z henrik $
"""
export student_list
"""
try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass
from Products.AdvancedQuery import Eq, Between, Le,In,MatchRegexp
try:
    aq_portal = context.portal_catalog.evalAdvancedQuery
except:
    aq_portal = context.portal_catalog_real.evalAdvancedQuery
aq_students = context.students_catalog.evalAdvancedQuery
request = context.REQUEST
setheader = request.RESPONSE.setHeader
pa = context.portal_accommodation
scat = context.students_catalog
st_schema = context.students_catalog.schema()
#fields = list(st_schema) + ["bed",]
fields = ("id",
          "name",
          "matric_no",
          "jamb_reg_no",
          "sex",
          "email",
          "faculty",
          "department",
          "course",
          "level",
          )
res_list = []
lines = []
lines.append(','.join(fields))
format = '"%(' + ')s","%('.join(fields) + ')s"'
#cleared = context.portal_catalog(review_state = "cleared_and_validated")
query = In('review_state',('cleared_and_validated',
                           'school_fee_paid',
                           'courses_registered',
                           'courses_validated'))
cleared = aq_portal(query)
newquery = Eq('portal_type','StudentApplication') & MatchRegexp('SearchableText',r'^6*')
#newquery = MatchRegexp('SearchableText','^5*')
#new_students = aq_portal(newquery)
#set_trace()
#new_sids = []
#for ns in new_students:
#    new_sids.append(ns.getPath().split('/')[-2])
#set_trace()
for student in cleared:
##    if student.getId not in new_sids:
##        continue
    erg = scat(id=student.getId)
    if not erg:
        continue
    if not erg[0].jamb_reg_no.startswith('6'):
        continue
    d = context.getFormattedStudentEntry(erg[0])
    lines.append(format % d)
#setheader('Content-type', 'text/x-cvs')
#setheader('Content-type','application/vnd.ms-excel')
setheader('Content-type','text/semicolon-seperated-values')
setheader('Content-Disposition:', 'attachment; filename="cleared_students.csv"')
setheader('Expires',  'Mon, 26 Jul 1997 05:00:00GMT') # Date in the past
setheader('Cache-Control', 'no-store, no-cache,must-revalidate') # HTTP/1.1
setheader('Cache-Control', 'post-check=0,pre-check=0')
setheader('Pragma', 'no-cache') # HTTP/1.0
return '\n'.join(lines)
