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 3408 2008-04-02 16:07:32Z henrik $ |
---|
11 | """ |
---|
12 | relocate to a new bed if allocated bed is wrong |
---|
13 | """ |
---|
14 | import logging |
---|
15 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
16 | |
---|
17 | logger = logging.getLogger('Skins.change_bed') |
---|
18 | request = context.REQUEST |
---|
19 | redirect = request.RESPONSE.redirect |
---|
20 | mtool = context.portal_membership |
---|
21 | wf = context.portal_workflow |
---|
22 | member = mtool.getAuthenticatedMember() |
---|
23 | member_id = str(member) |
---|
24 | path_info = request.get('PATH_INFO').split('/') |
---|
25 | |
---|
26 | if mtool.isAnonymousUser(): |
---|
27 | return None |
---|
28 | students = context.portal_url.getPortalObject().campus.students |
---|
29 | beds = context.portal_accommodation |
---|
30 | studs = context.students_catalog |
---|
31 | student_id = context.getStudentId() |
---|
32 | |
---|
33 | if student_id is not None: |
---|
34 | logger.info('%s requests bed change for %s' % (member_id,student_id)) |
---|
35 | info = context.getAccommodationInfo(student_id) |
---|
36 | if info['acco'] is None: |
---|
37 | logger.info('No accommodation object for %s' % (student_id)) |
---|
38 | return redirect("%s/%s/no_booking_allowed" % (students.absolute_url(),student_id)) |
---|
39 | res = beds(student=student_id) |
---|
40 | status = info['student_status'] |
---|
41 | while True: |
---|
42 | if len(res) == 0: |
---|
43 | psm = 'No bed allocated for %s' % (student_id) |
---|
44 | logger.info(psm) |
---|
45 | pass |
---|
46 | #break |
---|
47 | elif res[0].bed_type.endswith("reserved"): |
---|
48 | logger.info("found reserved bed %s which won't be released" % res[0].bed) |
---|
49 | pass |
---|
50 | else: |
---|
51 | allocated_bed = res[0] |
---|
52 | if allocated_bed.bed_type == status: |
---|
53 | logger.info('Status %s of %s has not changed.' % (status,student_id)) |
---|
54 | psm='Student status has not changed!' |
---|
55 | break |
---|
56 | #return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm)) |
---|
57 | logger.info('Bed status %s of %s has changed to %s.' % (allocated_bed.bed_type,student_id,status)) |
---|
58 | query = Eq('bed_type',status) & Eq('student',context.portal_accommodation.not_occupied) |
---|
59 | records = context.portal_accommodation.evalAdvancedQuery(query) |
---|
60 | if len(records) < 1: |
---|
61 | psm='Bed change for %s failed, no free bed in category %s.' % (student_id,status) |
---|
62 | logger.info(psm) |
---|
63 | break |
---|
64 | beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied) |
---|
65 | logger.info('Bed %s released' % (allocated_bed.bed)) |
---|
66 | code,bed = beds.searchAndReserveBed(student_id,status) |
---|
67 | if code > -2: |
---|
68 | d = {} |
---|
69 | d['bed'] = bed |
---|
70 | d['student_status'] = status |
---|
71 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
72 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
73 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
74 | acco_doc = info['acco_doc'] |
---|
75 | acco_doc.edit(mapping=d) |
---|
76 | if code > 0: |
---|
77 | psm = 'Bed changed!' |
---|
78 | logger.info('Bed %s allocated to %s' % (bed,student_id)) |
---|
79 | elif code == -1: |
---|
80 | psm = 'A reserved bed has been allocated.' |
---|
81 | elif code == -2: |
---|
82 | psm = 'No bed %s for %s available!' % (status,student_id) |
---|
83 | else: |
---|
84 | psm = 'Bed change failed!' |
---|
85 | break |
---|
86 | return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm)) |
---|
87 | |
---|
88 | info = {} |
---|
89 | records = [r for r in beds() if r.student and r.student != beds.not_occupied] |
---|
90 | list = [] |
---|
91 | to_modify = [] |
---|
92 | logger.info('started without student context') |
---|
93 | for r in records: |
---|
94 | info = context.getAccommodationInfo(r.student) |
---|
95 | sbt = info.get('student_status',None) |
---|
96 | if sbt is None: |
---|
97 | continue |
---|
98 | elif r.bed_type == sbt: |
---|
99 | #list.append("Student %s bed_type %s ok" % (r.student,r.bed_type)) |
---|
100 | continue |
---|
101 | message = "Student %s bed_type %s != %s" % (r.student, |
---|
102 | r.bed_type, |
---|
103 | info['student_status']) |
---|
104 | list.append(message) |
---|
105 | #to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc'])) |
---|
106 | logger.info(message) |
---|
107 | logger.info('finished') |
---|
108 | |
---|
109 | return "\r".join(list) |
---|
110 | |
---|
111 | for former_bed, student, status,acco_doc in to_modify: |
---|
112 | beds.modifyRecord(bed=former_bed,student='') |
---|
113 | code,bed = beds.searchAndReserveBed(student,status) |
---|
114 | if code > 0: |
---|
115 | #from Products.zdb import set_trace; set_trace() |
---|
116 | d = {} |
---|
117 | d['bed'] = bed |
---|
118 | d['student_status'] = status |
---|
119 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
120 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
121 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
122 | acco_doc.edit(mapping=d) |
---|
123 | list.append("New bed %s allocated to %s, code = %s" % (bed, |
---|
124 | student, |
---|
125 | code)) |
---|
126 | return "\r".join(list) |
---|