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