## 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: hall_allocation_list.py 3136 2008-02-09 18:11:00Z henrik $
"""
export hall_allocation_list
"""
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",
          "hall",
          "block",
          "room",
          "bed",
          "matric_no",
          "jamb_reg_no",
          "sex",
          "email",
          "phone",
          "faculty",
          "department",
          "course",
          "level",
          )
res_list = []
lines = []
lines.append(','.join(fields))
format = '"%(' + ')s","%('.join(fields) + ')s"'
hallid=context.getId()
booked = pa(hall=hallid)
if not booked:
    hallid = 'all_halls'
    booked = pa()
for bed in booked:
    if bed.student == '':
        continue
    erg = scat(id=bed.student)
    if not erg:
        continue
    d = context.getFormattedStudentEntry(erg[0])
    hall,block,room,bed = bed.bed.split('_')
    d['hall'] = hall
    d['block'] = block
    d['room'] = room
    d['bed'] = bed
    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="%s.csv"' % hallid)
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)
