source: WAeUP_SRP/branches/regebro-noskins/skins/waeup_accommodation/allocate_reserved_bed.py @ 17727

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

fixed and renamed

File size: 5.3 KB
Line 
1##parameters=REQUEST
2# $Id: allocate_reserved_bed.py 1451 2007-02-20 22:17:41Z joachim $
3"""
4process the the accommodation reservation
5"""
6import DateTime
7current = DateTime.DateTime()
8pr = context.portal_registration
9wftool = context.portal_workflow
10lt = context.portal_layouts
11mtool = context.portal_membership
12member = mtool.getAuthenticatedMember()
13acco_cat = context.portal_accommodation
14import logging
15logger = logging.getLogger('Accommodation.AllocateReservedBed')
16
17#change = REQUEST.has_key("allocate_and_change")
18change = False
19release = REQUEST.has_key("release")
20allocate = REQUEST.has_key("allocate")
21validate = release or change or (allocate and
22           REQUEST.get("allocate").startswith('Allocate'))
23mode = 'create'
24d = {}
25rendered,psm,ds = lt.renderLayout(layout_id= 'acco_bed_booking',
26                      schema_id= 'acco_bed_booking',
27                      context=context,
28                      mapping=validate and REQUEST,
29                      ob=d,
30                      layout_mode='edit',
31                      commit = False
32                      )
33if psm == 'invalid':
34    psm = "Please correct your input."
35    return context.allocate_reserved_bed_form(rendered = rendered,
36                                 psm = psm,
37                                 #psm = "%s, %s" % (psm,ds),
38                                 mode = mode,
39                                 formaction = "allocate_reserved_bed",
40                                 button = "Allocate Bed",
41                                 ds = ds,
42                                 )
43elif psm == '':
44    return context.allocate_reserved_bed_form(rendered = rendered,
45                                 psm = psm,
46                                 mode = mode,
47                                 formaction = "allocate_reserved_bed",
48                                 button = "Allocate Bed",
49                                 ds = ds,
50                                 )
51elif psm == 'valid':
52    pass
53#from Products.zdb import set_trace;set_trace()
54hall_id = context.getId()
55doc = context.getContent()
56student = ds.get('student')
57sid = ds.get('student_id')
58br = ds.get('block_room')
59block = br[0]
60bl = ds.get('bed_letter')
61bid = "%s_%s_%s" % (hall_id,br,bl)
62res = acco_cat(bed = bid)
63psm = ''
64students = context.portal_url.getPortalObject().campus.students
65student_obj = getattr(students,sid)
66acco_id = "accommodation_%s" % context.getSessionId()
67while True:
68    if not res:
69        psm = "No bed with id %s" % bid
70        break
71    bed_brain = res[0]
72    already = acco_cat(student=sid)
73    if allocate or change:
74        if already and not change:
75            psm = "Student %s already reserved bed %s" % (sid, already[0].bed)
76            break
77        if not bed_brain.bed_type.endswith("reserved"):
78            psm = "Not a reserved bed: %s" % bid
79            break
80        if bed_brain.student and not change:
81            psm = "Bed %s already reserved for %s" % (bid,bed_brain.student)
82            break
83        if student.sex and not block in doc.blocks_for_female:
84            psm = "Sex does not match %s" % bid
85            break
86    if release:
87        if acco_id in student_obj.objectIds():
88            acco_doc = getattr(student_obj, acco_id).getContent()
89            if acco_doc.bed == bid:
90                psm = "Student %s has already booked (paid) bed %s" % (sid, bid)
91                break
92        if not already:
93            psm = "Student %s has no reserved bed" % (sid)
94            break
95        if not bed_brain.bed_type.endswith("reserved"):
96            psm = "Not a reserved bed: %s" % bid
97            break
98        if not bed_brain.student:
99            psm = "Bed %s not allocated" % (bid)
100            break
101    break
102
103if psm != '':
104    return context.allocate_reserved_bed_form(rendered = rendered,
105                                 psm = psm,
106                                 mode = mode,
107                                 ds = ds,
108                                 formaction = "allocate_reserved_bed",
109                                 button = "Allocate Bed",
110                                 )
111if change:
112    for bed in already:
113        acco_cat.modifyRecord(bed=bed.bed,student='')
114
115if allocate or change:
116    acco_cat.modifyRecord(bed = bid, student = sid)
117    if acco_id in student_obj.objectIds():
118        acco_doc = getattr(student_obj, acco_id).getContent()
119        acco_doc.edit(mapping={'bed': bid})
120    logger.info('"%s","booked reserved bed %s for","%s"' % (member,bid,sid))
121    psm = "Bed %s reserved for %s %s" % (bid,sid,student.name)
122elif release:
123    modify = False
124    if acco_id in student_obj.objectIds():
125        #acco_doc = getattr(student_obj, acco_id).getContent()
126        if acco_doc.bed != bid:
127            modify = True
128    else:
129        modify = True
130    if modify:
131        acco_cat.modifyRecord(bed=bid,student='')
132        logger.info('"%s","cancelled reservation %s by","%s"' % (member,bid,sid))
133        psm = "%s removed from %s" % (student.name,bid)
134    else:
135        psm = "%s removing failed from %s" % (student.name,bid)
136
137mode = 'view'
138return context.allocate_reserved_bed_form(rendered = "",
139                                 psm = psm,
140                                 mode = mode,
141                                 formaction = "allocate_reserved_bed",
142                                 button = "Next Reservation",
143                                 ds = ds,
144                                 )
145
Note: See TracBrowser for help on using the repository browser.