source: WAeUP_SRP/branches/joachim-azax-branch/skins/waeup_accommodation/reallocate_bed.py @ 7900

Last change on this file since 7900 was 1566, checked in by Henrik Bettermann, 18 years ago

now logging format Part 1

File size: 4.2 KB
Line 
1## Script (Python) "reallocate_bed"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=
8##title=
9##
10# $Id: reallocateBed.py 1206 2007-01-05 18:24:56Z joachim $
11"""
12reallocate beds
13"""
14import logging
15logger = logging.getLogger('Skins.reallocate_bed')
16
17request = context.REQUEST
18redirect = request.RESPONSE.redirect
19mtool = context.portal_membership
20wf = context.portal_workflow
21member = mtool.getAuthenticatedMember()
22member_id = str(member)
23path_info = request.get('PATH_INFO').split('/')
24
25if mtool.isAnonymousUser():
26    return None
27students = context.portal_url.getPortalObject().campus.students
28beds = context.portal_accommodation
29studs = context.students_catalog
30student_id = context.getStudentId()
31
32if student_id is not None:
33    logger.info('%s requests bed_change for %s' % (member_id,student_id))
34    info = context.getAccommodationInfo(student_id)
35    res = beds(student=student_id)
36    if len(res) == 0:
37        logger.info('No bed found for %s' % (student_id))
38        redirect("%s/%s" % (students.absolute_url(),student_id))
39    allocated_bed = res[0]
40    status = info['student_status']
41    student = student_id
42    bedtype = context.getBedStatusFromHall(allocated_bed.bed)
43    if not bedtype.endswith("reserved"):
44        logger.info('%s is NOT a reserved bed' % (bedtype))
45        return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))
46    logger.info('%s reallocate reserved bed %s/%s' % (student_id,bedtype,status))
47    beds.modifyRecord(bed=allocated_bed.bed,
48                      student='',
49                      bed_type=bedtype)
50    code,bed = beds.searchAndReserveBed(student_id,status)
51    if code > 0:
52        #from Products.zdb import set_trace; set_trace()
53        d = {}
54        d['bed'] = bed
55        d['student_status'] = status
56        acco_info = context.waeup_tool.getAccommodationInfo(bed)
57        d['acco_maint_code'] = acco_info.get('maintenance_code')
58        d['acco_maint_fee'] = acco_info.get('maintenance_fee')
59        acco_doc = info['acco_doc']
60        acco_doc.edit(mapping=d)
61        return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))
62    logger.info('%s new bed allocation failed, code = %s' % (student_id,code))
63    return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id']))
64
65info = {}
66#records = [r for r in beds() if  r.student]
67records = beds.uniqueValuesFor('bed')
68list = []
69to_modify = []
70for b in records:
71    r = beds(bed=b)[0]
72    bedtype = context.getBedStatusFromHall(r.bed)
73    if not bedtype.endswith("reserved"):
74        #logger.info('"%s", "allocated bed is NOT reserved","%s"' % (student_id,bedtype))
75        #list.append("Student %s bed_type %s ok" % (r.student,bedtype))
76        continue
77    if not r.student:
78        beds.modifyRecord(bed=b,
79                      bed_type=bedtype)
80        list.append("Empty bed %s set to %s" % (r.bed,bedtype))
81        continue
82    info = context.getAccommodationInfo(r.student)
83    sbt = info.get('student_status',None)
84    if sbt is None:
85        continue
86    list.append("Student %s bed_type %s != %s" % (r.student,
87                                                  r.bed_type,
88                                                  bedtype))
89    to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc'],bedtype))
90for former_bed, student, status,acco_doc,bedtype in to_modify:
91    beds.modifyRecord(bed=former_bed,
92                      student='',
93                      bed_type=bedtype)
94    code,bed = beds.searchAndReserveBed(student,status)
95    if code > 0:
96        #from Products.zdb import set_trace; set_trace()
97        d = {}
98        d['bed'] = bed
99        d['student_status'] = status
100        acco_info = context.waeup_tool.getAccommodationInfo(bed)
101        d['acco_maint_code'] = acco_info.get('maintenance_code')
102        d['acco_maint_fee'] = acco_info.get('maintenance_fee')
103        acco_doc.edit(mapping=d)
104        list.append("Student %s got bed %s, code = %s" % (student,
105                                                              bed,
106                                                              code))
107return "\r".join(list)
Note: See TracBrowser for help on using the repository browser.