1 | ## Script (Python) "change_bed" |
---|
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 1596 2007-03-19 21:45:44Z joachim $ |
---|
11 | """ |
---|
12 | relocate to a new bed if allocated bed is wrong |
---|
13 | """ |
---|
14 | import logging |
---|
15 | logger = logging.getLogger('Skins.change_bed') |
---|
16 | |
---|
17 | request = context.REQUEST |
---|
18 | redirect = request.RESPONSE.redirect |
---|
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 |
---|
27 | students = context.portal_url.getPortalObject().campus.students |
---|
28 | beds = context.portal_accommodation |
---|
29 | studs = context.students_catalog |
---|
30 | student_id = context.getStudentId() |
---|
31 | |
---|
32 | if student_id is not None: |
---|
33 | logger.info('%s requests bed change for %s' % (member_id,student_id)) |
---|
34 | info = context.getAccommodationInfo(student_id) |
---|
35 | res = beds(student=student_id) |
---|
36 | if len(res) == 0: |
---|
37 | logger.info('No bed found for %s' % (student_id)) |
---|
38 | return redirect("%s/%s" % (students.absolute_url(),student_id)) |
---|
39 | allocated_bed = res[0] |
---|
40 | status = info['student_status'] |
---|
41 | student = student_id |
---|
42 | if allocated_bed.bed_type == status: |
---|
43 | logger.info('Status %s of %s has not changed' % (status,student_id)) |
---|
44 | return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id'])) |
---|
45 | logger.info('Bed status %s of %s has changed to %s' % (allocated_bed.bed_type,student_id,status)) |
---|
46 | beds.modifyRecord(bed=allocated_bed.bed,student='') |
---|
47 | logger.info('Bed %s released' % (allocated_bed.bed)) |
---|
48 | code,bed = beds.searchAndReserveBed(student_id,status) |
---|
49 | if code > 0: |
---|
50 | #from Products.zdb import set_trace; set_trace() |
---|
51 | d = {} |
---|
52 | d['bed'] = bed |
---|
53 | d['student_status'] = status |
---|
54 | acco_info = context.waeup_tool.getAccommodationInfo(bed) |
---|
55 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
56 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
57 | acco_doc = info['acco_doc'] |
---|
58 | acco_doc.edit(mapping=d) |
---|
59 | logger.info('Bed %s allocated to %s' % (bed,student_id)) |
---|
60 | return redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id'])) |
---|
61 | student_obj = getattr(students,student_id) |
---|
62 | acco_id = "accommodation_%s" % context.getSessionId() |
---|
63 | if acco_id in student_obj.objectIds(): |
---|
64 | acco_doc = getattr(student_obj, acco_id).getContent() |
---|
65 | if acco_doc.bed == bed: |
---|
66 | acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"}) |
---|
67 | logger.info('New bed allocation for %s failed, code = %s' % (student_id,code)) |
---|
68 | logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id)) |
---|
69 | redirect("%s/%s/%s" % (students.absolute_url(),student,info['acco_id'])) |
---|
70 | |
---|
71 | info = {} |
---|
72 | records = [r for r in beds() if r.student] |
---|
73 | list = [] |
---|
74 | to_modify = [] |
---|
75 | for r in records: |
---|
76 | info = context.getAccommodationInfo(r.student) |
---|
77 | sbt = info.get('student_status',None) |
---|
78 | if sbt is None: |
---|
79 | continue |
---|
80 | elif r.bed_type == sbt: |
---|
81 | list.append("Student %s bed_type %s ok" % (r.student,r.bed_type)) |
---|
82 | continue |
---|
83 | list.append("Student %s bed_type %s != %s" % (r.student, |
---|
84 | r.bed_type, |
---|
85 | info['student_status'])) |
---|
86 | to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc'])) |
---|
87 | for former_bed, student, status,acco_doc in to_modify: |
---|
88 | beds.modifyRecord(bed=former_bed,student='') |
---|
89 | code,bed = beds.searchAndReserveBed(student,status) |
---|
90 | if code > 0: |
---|
91 | #from Products.zdb import set_trace; set_trace() |
---|
92 | d = {} |
---|
93 | d['bed'] = bed |
---|
94 | d['student_status'] = status |
---|
95 | acco_info = context.waeup_tool.getAccommodationInfo(bed) |
---|
96 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
97 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
98 | acco_doc.edit(mapping=d) |
---|
99 | list.append("New bed %s allocated to %s, code = %s" % (bed, |
---|
100 | student, |
---|
101 | code)) |
---|
102 | return "\r".join(list) |
---|