## Script (Python) "getAccoHallInfo" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=student=None ##title= ## # $Id: getAccoHallInfo.py 2845 2007-12-03 21:14:20Z joachim $ """ return Info about a Accommodation Hall """ try: from Products.zdb import set_trace except: def set_trace(): pass from Products.AdvancedQuery import Eq, Between, Le,In def cmp_bed(a,b): if a['bed'] > b['bed']: return 1 return -1 request = context.REQUEST wf = context.portal_workflow path_info = request.get('PATH_INFO').split('/') mtool = context.portal_membership portal_accommodation = context.portal_accommodation aq_accommodation = portal_accommodation.evalAdvancedQuery info = {} #dep_id = request.get('PATH_TRANSLATED').split('/')[-2] dep_id = context.aq_parent.getId() info['action'] = "%s" % context.absolute_url() info['choosen_ids'] = request.get('ids',[]) info['doc'] = context.getContent() bed_types = portal_accommodation.uniqueValuesFor('bed_type') bt_list = [] bt_names = context.getBedTypeNames() res = portal_accommodation.uniqueValuesFor('student') all_booked = [st for st in res if st and st != 'not_occupied'] booked_list = [] for bt in bed_types: total = len(portal_accommodation(bed_type=bt,hall=context.getId())) if total <= 1: continue booked = portal_accommodation.searchResults({'bed_type': bt, 'student':all_booked, 'hall': context.getId()}) bt_list.append({'name': bt_names[bt], 'total': total, 'reserved': len(booked), }) booked_list.extend(booked) # query = ~Eq('student','not_occupied') # beds = aq_accommodation(query,sortSpecs=('bed_type',)) # bt_list = [] # bt_names = context.getBedTypeNames() # bt = 'xxx' # for bed in beds: # if bt != bed.bed_type: # bt = bed.bed_type # total = len(aq_accommodation(Eq('bed_type',bt))) # bt_list.append({'name': bt_names[bt], # 'total': total, # 'reserved': len(booked), # }) info['bed_types'] = bt_list res_list = [] students_url = "%s/campus/students" % (context.portal_url()) for sbrain in booked_list: d = {} st = sbrain.student if not st: continue d['student'] = st d['student_url'] ='%s/%s' % (students_url,st) bed = context.getFormattedBed(sbrain.bed).split('/') d['bed'] = " / ".join(bed[1:]) erg = context.students_catalog(id = st) if erg: sd = context.getFormattedStudentEntry(erg[0]) d.update(sd) else: #d['student'] = None d['student_url'] = None #d['bed'] = None d['name'] = 'Student record removed' res_list.append(d) res_list.sort(cmp_bed) info['reserved'] = res_list return info