1 | ##parameters=REQUEST |
---|
2 | # $Id: allocate_reserved_bed.py 5653 2011-01-21 12:16:53Z henrik $ |
---|
3 | """ |
---|
4 | process the the accommodation reservation |
---|
5 | """ |
---|
6 | import DateTime |
---|
7 | current = DateTime.DateTime() |
---|
8 | pr = context.portal_registration |
---|
9 | wftool = context.portal_workflow |
---|
10 | lt = context.portal_layouts |
---|
11 | mtool = context.portal_membership |
---|
12 | member = mtool.getAuthenticatedMember() |
---|
13 | portal_acco_cat = context.portal_accommodation |
---|
14 | import logging |
---|
15 | logger = logging.getLogger('Skins.allocate_reserved_bed') |
---|
16 | |
---|
17 | release = REQUEST.has_key("release") |
---|
18 | allocate = REQUEST.has_key("allocate") |
---|
19 | validate = release or (allocate and REQUEST.get("allocate").startswith('Allocate')) |
---|
20 | mode = 'create' |
---|
21 | d = {} |
---|
22 | rendered,psm,ds = lt.renderLayout(layout_id= 'acco_bed_booking', |
---|
23 | schema_id= 'acco_bed_booking', |
---|
24 | context=context, |
---|
25 | mapping=validate and REQUEST, |
---|
26 | ob=d, |
---|
27 | layout_mode='edit', |
---|
28 | commit = False |
---|
29 | ) |
---|
30 | if psm == 'invalid': |
---|
31 | psm = "Please correct your input." |
---|
32 | return context.allocate_reserved_bed_form(rendered = rendered, |
---|
33 | psm = psm, |
---|
34 | #psm = "%s, %s" % (psm,ds), |
---|
35 | mode = mode, |
---|
36 | formaction = "allocate_reserved_bed", |
---|
37 | button = "Allocate Bed", |
---|
38 | ds = ds, |
---|
39 | ) |
---|
40 | elif psm == '': |
---|
41 | return context.allocate_reserved_bed_form(rendered = rendered, |
---|
42 | psm = psm, |
---|
43 | mode = mode, |
---|
44 | formaction = "allocate_reserved_bed", |
---|
45 | button = "Allocate Bed", |
---|
46 | ds = ds, |
---|
47 | ) |
---|
48 | elif psm == 'valid': |
---|
49 | pass |
---|
50 | #from Products.zdb import set_trace;set_trace() |
---|
51 | hall_id = context.getId() |
---|
52 | doc = context.getContent() |
---|
53 | student = ds.get('student') |
---|
54 | sid = ds.get('student_id') |
---|
55 | br = ds.get('block_room') |
---|
56 | block = br[0] |
---|
57 | bl = ds.get('bed_letter') |
---|
58 | bid = "%s_%s_%s" % (hall_id,br,bl) |
---|
59 | res = portal_acco_cat(bed = bid) |
---|
60 | already = portal_acco_cat(student=sid) |
---|
61 | psm = '' |
---|
62 | |
---|
63 | while True: |
---|
64 | if not res: |
---|
65 | psm = "No bed with id %s" % bid |
---|
66 | break |
---|
67 | bed_brain = res[0] |
---|
68 | if allocate: |
---|
69 | if already: |
---|
70 | psm = "Student %s has already booked bed %s. This booking must be cancelled first." % (sid, already[0].bed) |
---|
71 | break |
---|
72 | if not "reserved" in bed_brain.bed_type: |
---|
73 | psm = "%s is not a reserved bed" % bid |
---|
74 | break |
---|
75 | if bed_brain.student and bed_brain.student != portal_acco_cat.not_occupied: |
---|
76 | psm = "Bed %s already reserved for %s" % (bid,bed_brain.student) |
---|
77 | break |
---|
78 | if student.sex and not block in doc.blocks_for_female: |
---|
79 | psm = "Sex does not match %s" % bid |
---|
80 | break |
---|
81 | if release: |
---|
82 | if not already: |
---|
83 | psm = "Student %s has no bed" % (sid) |
---|
84 | break |
---|
85 | already_brain = already[0] |
---|
86 | if already_brain.bed != bid: |
---|
87 | psm = "Student %s has booked bed %s. Bed can only be released by cancelling the booking record." % (sid, already_brain.bed) |
---|
88 | break |
---|
89 | if not bed_brain.student: |
---|
90 | psm = "Bed %s not allocated" % (bid) |
---|
91 | break |
---|
92 | accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0],reservation_status = 'maintenance_fee_paid') |
---|
93 | if accommodation_records: |
---|
94 | psm = "Student %s has already paid for bed %s" % (sid, bid) |
---|
95 | break |
---|
96 | if not "reserved" in bed_brain.bed_type: |
---|
97 | psm = "%s is not a reserved bed" % bid |
---|
98 | break |
---|
99 | break |
---|
100 | |
---|
101 | if psm != '': |
---|
102 | return context.allocate_reserved_bed_form(rendered = rendered, |
---|
103 | psm = psm, |
---|
104 | mode = mode, |
---|
105 | ds = ds, |
---|
106 | formaction = "allocate_reserved_bed", |
---|
107 | button = "Allocate Bed", |
---|
108 | ) |
---|
109 | if allocate: |
---|
110 | portal_acco_cat.modifyRecord(bed = bid, student = sid) |
---|
111 | accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0]) |
---|
112 | if accommodation_records: |
---|
113 | d = {} |
---|
114 | d['bed'] = bid |
---|
115 | d['catkey'] = sid + '|' + context.getSessionId()[0] |
---|
116 | context.accommodation_catalog.modifyRecord(**d) |
---|
117 | logger.info('%s booked reserved bed %s for %s' % (member,bid,sid)) |
---|
118 | psm = "Bed %s reserved for %s %s" % (bid,sid,student.name) |
---|
119 | elif release: |
---|
120 | accommodation_records = context.accommodation_catalog(student_id = sid, session = context.getSessionId()[0]) |
---|
121 | if accommodation_records and accommodation_records[0].bed == bid: |
---|
122 | psm = "Removing of %s from bed %s failed. The booking record must be cancelled first." % (student.name,bid) |
---|
123 | else: |
---|
124 | portal_acco_cat.modifyRecord(bed=bid,student='') |
---|
125 | logger.info('%s released bed %s booked by %s' % (member,bid,sid)) |
---|
126 | psm = "%s removed from %s" % (student.name,bid) |
---|
127 | |
---|
128 | mode = 'view' |
---|
129 | return context.allocate_reserved_bed_form(rendered = "", |
---|
130 | psm = psm, |
---|
131 | mode = mode, |
---|
132 | formaction = "allocate_reserved_bed", |
---|
133 | button = "Next Reservation", |
---|
134 | ds = ds, |
---|
135 | ) |
---|
136 | |
---|