| 1 | ## Script (Python) "getAccoHallInfo" |
|---|
| 2 | ##bind container=container |
|---|
| 3 | ##bind context=context |
|---|
| 4 | ##bind namespace= |
|---|
| 5 | ##bind script=script |
|---|
| 6 | ##bind subpath=traverse_subpath |
|---|
| 7 | ##parameters=student=None |
|---|
| 8 | ##title= |
|---|
| 9 | ## |
|---|
| 10 | # $Id: getAccoHallInfo.py 3054 2008-01-28 13:13:11Z joachim $ |
|---|
| 11 | """ |
|---|
| 12 | return Info about a Accommodation Hall |
|---|
| 13 | """ |
|---|
| 14 | try: |
|---|
| 15 | from Products.zdb import set_trace |
|---|
| 16 | except: |
|---|
| 17 | def set_trace(): |
|---|
| 18 | pass |
|---|
| 19 | |
|---|
| 20 | from Products.AdvancedQuery import Eq, Between, Le,In |
|---|
| 21 | |
|---|
| 22 | def cmp_bed(a,b): |
|---|
| 23 | if a['bed'] > b['bed']: |
|---|
| 24 | return 1 |
|---|
| 25 | return -1 |
|---|
| 26 | |
|---|
| 27 | request = context.REQUEST |
|---|
| 28 | portal_accommodation = context.portal_accommodation |
|---|
| 29 | aq_accommodation = portal_accommodation.evalAdvancedQuery |
|---|
| 30 | aq_students = context.students_catalog.evalAdvancedQuery |
|---|
| 31 | info = {} |
|---|
| 32 | info['action'] = "%s" % context.absolute_url() |
|---|
| 33 | info['choosen_ids'] = request.get('ids',[]) |
|---|
| 34 | info['doc'] = context.getContent() |
|---|
| 35 | bt_list = [] |
|---|
| 36 | bt_names = context.getBedTypeNames() |
|---|
| 37 | query = Eq('hall',context.getId()) |
|---|
| 38 | beds = aq_accommodation(query) |
|---|
| 39 | bed_types = [] |
|---|
| 40 | for bed in beds: |
|---|
| 41 | if bed.bed_type not in bed_types: |
|---|
| 42 | bed_types += bed.bed_type, |
|---|
| 43 | bed_type_info = [] |
|---|
| 44 | booked_list = [] |
|---|
| 45 | booked_mapping = {} |
|---|
| 46 | for bt in bed_types: |
|---|
| 47 | total_beds = [b for b in beds if b.bed_type == bt] |
|---|
| 48 | booked_beds = 0 |
|---|
| 49 | for b in total_beds: |
|---|
| 50 | if b.student and b.student != context.portal_accommodation.not_occupied: |
|---|
| 51 | booked_beds += 1 |
|---|
| 52 | booked_mapping[b.student] = b |
|---|
| 53 | bed_type_info.append({'name': bt_names[bt], |
|---|
| 54 | 'total': len(total_beds), |
|---|
| 55 | 'reserved': booked_beds, |
|---|
| 56 | }) |
|---|
| 57 | info['bed_types'] = bed_type_info |
|---|
| 58 | reserved_info = [] |
|---|
| 59 | students_url = "%s/campus/students" % (context.portal_url()) |
|---|
| 60 | student_records = aq_students(In('id',booked_mapping.keys())) |
|---|
| 61 | for student_record in student_records: |
|---|
| 62 | d = {} |
|---|
| 63 | student_id = student_record.id |
|---|
| 64 | d['student'] = student_id |
|---|
| 65 | d['student_url'] ='%s/%s' % (students_url,student_id) |
|---|
| 66 | bed = context.getFormattedBed(booked_mapping[student_id].bed).split('/') |
|---|
| 67 | d['bed'] = " / ".join(bed[1:]) |
|---|
| 68 | d.update(context.getFormattedStudentEntry(student_record)) |
|---|
| 69 | reserved_info.append(d) |
|---|
| 70 | reserved_info.sort(cmp_bed) |
|---|
| 71 | info['reserved'] = reserved_info |
|---|
| 72 | |
|---|
| 73 | return info |
|---|