1 | ##parameters=REQUEST |
---|
2 | # $Id: book_reserved_bed.py 1444 2007-02-19 17:16:34Z joachim $ |
---|
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 | acco_cat = context.portal_accommodation |
---|
14 | import logging |
---|
15 | logger = logging.getLogger('Accommodation.BookReservedBed') |
---|
16 | |
---|
17 | change = REQUEST.has_key("reserve_and_change") |
---|
18 | release = REQUEST.has_key("release") |
---|
19 | reserve = REQUEST.has_key("reserve") |
---|
20 | validate = release or change or (reserve and |
---|
21 | REQUEST.get("reserve").startswith('Reserve')) |
---|
22 | mode = 'create' |
---|
23 | d = {} |
---|
24 | rendered,psm,ds = lt.renderLayout(layout_id= 'acco_bed_booking', |
---|
25 | schema_id= 'acco_bed_booking', |
---|
26 | context=context, |
---|
27 | mapping=validate and REQUEST, |
---|
28 | ob=d, |
---|
29 | layout_mode='edit', |
---|
30 | commit = False |
---|
31 | ) |
---|
32 | if psm == 'invalid': |
---|
33 | psm = "Please correct your input." |
---|
34 | return context.book_reserved_bed_form(rendered = rendered, |
---|
35 | psm = psm, |
---|
36 | #psm = "%s, %s" % (psm,ds), |
---|
37 | mode = mode, |
---|
38 | formaction = "book_reserved_bed", |
---|
39 | button = "Reserve Bed", |
---|
40 | ds = ds, |
---|
41 | ) |
---|
42 | elif psm == '': |
---|
43 | return context.book_reserved_bed_form(rendered = rendered, |
---|
44 | psm = psm, |
---|
45 | mode = mode, |
---|
46 | formaction = "book_reserved_bed", |
---|
47 | button = "Reserve Bed", |
---|
48 | ds = ds, |
---|
49 | ) |
---|
50 | elif psm == 'valid': |
---|
51 | pass |
---|
52 | #from Products.zdb import set_trace;set_trace() |
---|
53 | hall_id = context.getId() |
---|
54 | doc = context.getContent() |
---|
55 | student = ds.get('student') |
---|
56 | sid = ds.get('student_id') |
---|
57 | br = ds.get('block_room') |
---|
58 | block = br[0] |
---|
59 | bl = ds.get('bed_letter') |
---|
60 | bid = "%s_%s_%s" % (hall_id,br,bl) |
---|
61 | res = acco_cat(bed = bid) |
---|
62 | psm = '' |
---|
63 | while True: |
---|
64 | if not res: |
---|
65 | psm = "No bed with id %s" % bid |
---|
66 | break |
---|
67 | bed_brain = res[0] |
---|
68 | already = acco_cat(student=sid) |
---|
69 | if reserve or change: |
---|
70 | if already and not change: |
---|
71 | psm = "Student %s already reserved bed %s" % (sid, already[0].bed) |
---|
72 | break |
---|
73 | if not bed_brain.bed_type.endswith("reserved"): |
---|
74 | psm = "Not a reserved bed %s" % bid |
---|
75 | break |
---|
76 | if bed_brain.student and not change: |
---|
77 | psm = "Bed %s already reserved for %s" % (bid,bed_brain.student) |
---|
78 | break |
---|
79 | if student.sex and not block in doc.blocks_for_female: |
---|
80 | psm = "Sex does not match %s" % bid |
---|
81 | break |
---|
82 | if release: |
---|
83 | if not already: |
---|
84 | psm = "Student %s already has no reserved beds" % (sid, already[0].bed) |
---|
85 | break |
---|
86 | if not bed_brain.bed_type.endswith("reserved"): |
---|
87 | psm = "Not a reserved bed %s" % bid |
---|
88 | break |
---|
89 | if not bed_brain.student: |
---|
90 | psm = "Bed %s not reserved for %s" % (bid,bed_brain.student) |
---|
91 | break |
---|
92 | break |
---|
93 | |
---|
94 | if psm != '': |
---|
95 | return context.book_reserved_bed_form(rendered = rendered, |
---|
96 | psm = psm, |
---|
97 | mode = mode, |
---|
98 | ds = ds, |
---|
99 | formaction = "book_reserved_bed", |
---|
100 | button = "Reserve Bed", |
---|
101 | ) |
---|
102 | if change: |
---|
103 | for bed in already: |
---|
104 | acco_cat.modifyRecord(bed=bed.bed,student='') |
---|
105 | |
---|
106 | students = context.portal_url.getPortalObject().campus.students |
---|
107 | student_obj = getattr(students,sid) |
---|
108 | acco_id = "accommodation_%s" % context.getSessionId() |
---|
109 | if reserve or change: |
---|
110 | acco_cat.modifyRecord(bed = bid, student = sid) |
---|
111 | if acco_id in student_obj.objectIds(): |
---|
112 | acco_doc = getattr(student_obj, acco_id).getContent() |
---|
113 | acco_doc.edit(mapping={'bed': bid}) |
---|
114 | logger.info('"%s","booked reserved bed %s for","%s"' % (member,bid,sid)) |
---|
115 | psm = "Bed reserved for %s %s" % (sid,student.name) |
---|
116 | elif release: |
---|
117 | acco_cat.modifyRecord(bed=bid,student='') |
---|
118 | if acco_id in student_obj.objectIds(): |
---|
119 | acco_doc = getattr(student_obj, acco_id).getContent() |
---|
120 | acco_doc.edit(mapping={'bed': bid + "(removed)"}) |
---|
121 | logger.info('"%s","released reserved bed %s for","%s"' % (member,bid,sid)) |
---|
122 | psm = "Bed %s released for %s" % (sid,student.name) |
---|
123 | |
---|
124 | mode = 'view' |
---|
125 | return context.book_reserved_bed_form(rendered = "", |
---|
126 | psm = psm, |
---|
127 | mode = mode, |
---|
128 | formaction = "book_reserved_bed", |
---|
129 | button = "Next Reservation", |
---|
130 | ds = ds, |
---|
131 | ) |
---|
132 | |
---|