source: WAeUP_SRP/trunk/skins/waeup_accommodation/getAccoHallInfo.py @ 7713

Last change on this file since 7713 was 3054, checked in by joachim, 17 years ago

improve and sanitize code.

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
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"""
12return Info about a Accommodation Hall
13"""
14try:
15    from Products.zdb import set_trace
16except:
17    def set_trace():
18        pass
19   
20from Products.AdvancedQuery import Eq, Between, Le,In
21
22def cmp_bed(a,b):
23    if a['bed'] > b['bed']:
24        return 1
25    return -1
26
27request = context.REQUEST
28portal_accommodation = context.portal_accommodation
29aq_accommodation = portal_accommodation.evalAdvancedQuery
30aq_students = context.students_catalog.evalAdvancedQuery
31info = {}
32info['action'] = "%s" % context.absolute_url()
33info['choosen_ids'] = request.get('ids',[])
34info['doc'] = context.getContent()
35bt_list = []
36bt_names =  context.getBedTypeNames()
37query = Eq('hall',context.getId())
38beds = aq_accommodation(query)
39bed_types = []
40for bed in beds:
41    if bed.bed_type not in bed_types:
42        bed_types += bed.bed_type,
43bed_type_info = []
44booked_list = []
45booked_mapping = {}
46for 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                   })
57info['bed_types'] = bed_type_info
58reserved_info = []
59students_url = "%s/campus/students" % (context.portal_url())
60student_records = aq_students(In('id',booked_mapping.keys()))
61for 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)
70reserved_info.sort(cmp_bed)
71info['reserved'] = reserved_info
72
73return info
Note: See TracBrowser for help on using the repository browser.