| 1 | ##parameters=REQUEST |
|---|
| 2 | # $Id: allocate_reserved_bed.py 1451 2007-02-20 22:17:41Z 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('Skins.allocate_reserved_bed') |
|---|
| 16 | |
|---|
| 17 | #change = REQUEST.has_key("allocate_and_change") |
|---|
| 18 | change = False |
|---|
| 19 | release = REQUEST.has_key("release") |
|---|
| 20 | allocate = REQUEST.has_key("allocate") |
|---|
| 21 | validate = release or change or (allocate and |
|---|
| 22 | REQUEST.get("allocate").startswith('Allocate')) |
|---|
| 23 | mode = 'create' |
|---|
| 24 | d = {} |
|---|
| 25 | rendered,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 | ) |
|---|
| 33 | if 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 | ) |
|---|
| 43 | elif 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 | ) |
|---|
| 51 | elif psm == 'valid': |
|---|
| 52 | pass |
|---|
| 53 | #from Products.zdb import set_trace;set_trace() |
|---|
| 54 | hall_id = context.getId() |
|---|
| 55 | doc = context.getContent() |
|---|
| 56 | student = ds.get('student') |
|---|
| 57 | sid = ds.get('student_id') |
|---|
| 58 | br = ds.get('block_room') |
|---|
| 59 | block = br[0] |
|---|
| 60 | bl = ds.get('bed_letter') |
|---|
| 61 | bid = "%s_%s_%s" % (hall_id,br,bl) |
|---|
| 62 | res = acco_cat(bed = bid) |
|---|
| 63 | psm = '' |
|---|
| 64 | students = context.portal_url.getPortalObject().campus.students |
|---|
| 65 | student_obj = getattr(students,sid) |
|---|
| 66 | acco_id = "accommodation_%s" % context.getSessionId()[0] |
|---|
| 67 | while 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 has already reserved bed %s" % (sid, already[0].bed) |
|---|
| 76 | break |
|---|
| 77 | if not "reserved" in bed_brain.bed_type: |
|---|
| 78 | psm = "%s is not a reserved bed" % bid |
|---|
| 79 | break |
|---|
| 80 | if bed_brain.student and bed_brain.student != acco_cat.not_occupied 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 (and 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 "reserved" in bed_brain.bed_type: |
|---|
| 96 | psm = "%s is not a reserved bed" % bid |
|---|
| 97 | break |
|---|
| 98 | if not bed_brain.student: |
|---|
| 99 | psm = "Bed %s not allocated" % (bid) |
|---|
| 100 | break |
|---|
| 101 | break |
|---|
| 102 | |
|---|
| 103 | if 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 | ) |
|---|
| 111 | if change: |
|---|
| 112 | for bed in already: |
|---|
| 113 | acco_cat.modifyRecord(bed=bed.bed,student=acco_cat.not_occupied) |
|---|
| 114 | |
|---|
| 115 | if 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) |
|---|
| 122 | elif 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=acco_cat.not_occupied) |
|---|
| 132 | logger.info('%s released bed %s booked by %s' % (member,bid,sid)) |
|---|
| 133 | psm = "%s removed from %s" % (student.name,bid) |
|---|
| 134 | else: |
|---|
| 135 | psm = "Removing of %s from bed %s failed" % (student.name,bid) |
|---|
| 136 | |
|---|
| 137 | mode = 'view' |
|---|
| 138 | return 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 | |
|---|