[1164] | 1 | ## Script (Python) "change_bed" |
---|
[1104] | 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##parameters= |
---|
| 8 | ##title= |
---|
| 9 | ## |
---|
| 10 | # $Id: change_bed.py 3047 2008-01-25 09:55:40Z joachim $ |
---|
| 11 | """ |
---|
[1135] | 12 | relocate to a new bed if allocated bed is wrong |
---|
[1104] | 13 | """ |
---|
| 14 | import logging |
---|
[1566] | 15 | logger = logging.getLogger('Skins.change_bed') |
---|
[1104] | 16 | |
---|
| 17 | request = context.REQUEST |
---|
[1135] | 18 | redirect = request.RESPONSE.redirect |
---|
[1104] | 19 | mtool = context.portal_membership |
---|
| 20 | wf = context.portal_workflow |
---|
| 21 | member = mtool.getAuthenticatedMember() |
---|
| 22 | member_id = str(member) |
---|
| 23 | path_info = request.get('PATH_INFO').split('/') |
---|
| 24 | |
---|
| 25 | if mtool.isAnonymousUser(): |
---|
| 26 | return None |
---|
[1135] | 27 | students = context.portal_url.getPortalObject().campus.students |
---|
[1104] | 28 | beds = context.portal_accommodation |
---|
| 29 | studs = context.students_catalog |
---|
[1135] | 30 | student_id = context.getStudentId() |
---|
| 31 | |
---|
| 32 | if student_id is not None: |
---|
[1566] | 33 | logger.info('%s requests bed change for %s' % (member_id,student_id)) |
---|
[1135] | 34 | info = context.getAccommodationInfo(student_id) |
---|
[3047] | 35 | if info['acco'] is None: |
---|
| 36 | logger.info('No accommodation object for %s' % (student_id)) |
---|
| 37 | return redirect("%s/%s" % (students.absolute_url(),student_id)) |
---|
[1135] | 38 | res = beds(student=student_id) |
---|
[3047] | 39 | status = info['student_status'] |
---|
[1135] | 40 | if len(res) == 0: |
---|
[3047] | 41 | logger.info('No bed allocated for %s' % (student_id)) |
---|
| 42 | #return redirect("%s/%s" % (students.absolute_url(),student_id)) |
---|
| 43 | else: |
---|
| 44 | allocated_bed = res[0] |
---|
| 45 | #student = student_id |
---|
| 46 | if allocated_bed.bed_type == status: |
---|
| 47 | logger.info('Status %s of %s has not changed' % (status,student_id)) |
---|
| 48 | return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id'])) |
---|
| 49 | logger.info('Bed status %s of %s has changed to %s' % (allocated_bed.bed_type,student_id,status)) |
---|
| 50 | beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied) |
---|
| 51 | logger.info('Bed %s released' % (allocated_bed.bed)) |
---|
[1135] | 52 | code,bed = beds.searchAndReserveBed(student_id,status) |
---|
| 53 | if code > 0: |
---|
| 54 | d = {} |
---|
| 55 | d['bed'] = bed |
---|
| 56 | d['student_status'] = status |
---|
[2000] | 57 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
[1135] | 58 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
| 59 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
| 60 | acco_doc = info['acco_doc'] |
---|
| 61 | acco_doc.edit(mapping=d) |
---|
[1566] | 62 | logger.info('Bed %s allocated to %s' % (bed,student_id)) |
---|
[3047] | 63 | return redirect("%s/%s/%s" % (students.absolute_url(),student_id,info['acco_id'])) |
---|
| 64 | # student_obj = getattr(students,student_id) |
---|
| 65 | # acco_id = "accommodation_%s" % context.getSessionId()[0] |
---|
| 66 | # if acco_id in student_obj.objectIds(): |
---|
| 67 | # acco_doc = getattr(student_obj, acco_id).getContent() |
---|
| 68 | # if acco_doc.bed == bed: |
---|
| 69 | # acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"}) |
---|
| 70 | acco_doc = info['acco_doc'] |
---|
| 71 | if acco_doc.bed == bed: |
---|
| 72 | acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"}) |
---|
[1566] | 73 | logger.info('New bed allocation for %s failed, code = %s' % (student_id,code)) |
---|
| 74 | logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id)) |
---|
[1135] | 75 | redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id'])) |
---|
[1164] | 76 | |
---|
[1135] | 77 | info = {} |
---|
[1104] | 78 | records = [r for r in beds() if r.student] |
---|
| 79 | list = [] |
---|
| 80 | to_modify = [] |
---|
| 81 | for r in records: |
---|
| 82 | info = context.getAccommodationInfo(r.student) |
---|
[1206] | 83 | sbt = info.get('student_status',None) |
---|
| 84 | if sbt is None: |
---|
| 85 | continue |
---|
| 86 | elif r.bed_type == sbt: |
---|
[1104] | 87 | list.append("Student %s bed_type %s ok" % (r.student,r.bed_type)) |
---|
| 88 | continue |
---|
| 89 | list.append("Student %s bed_type %s != %s" % (r.student, |
---|
| 90 | r.bed_type, |
---|
| 91 | info['student_status'])) |
---|
[1117] | 92 | to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc'])) |
---|
| 93 | for former_bed, student, status,acco_doc in to_modify: |
---|
[1104] | 94 | beds.modifyRecord(bed=former_bed,student='') |
---|
| 95 | code,bed = beds.searchAndReserveBed(student,status) |
---|
| 96 | if code > 0: |
---|
[1117] | 97 | #from Products.zdb import set_trace; set_trace() |
---|
[1104] | 98 | d = {} |
---|
| 99 | d['bed'] = bed |
---|
| 100 | d['student_status'] = status |
---|
[2000] | 101 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
[1104] | 102 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
| 103 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
[1117] | 104 | acco_doc.edit(mapping=d) |
---|
[1566] | 105 | list.append("New bed %s allocated to %s, code = %s" % (bed, |
---|
| 106 | student, |
---|
[1104] | 107 | code)) |
---|
| 108 | return "\r".join(list) |
---|