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 3170 2008-02-15 08:20:49Z 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 | break |
---|
46 | allocated_bed = res[0] |
---|
47 | if allocated_bed.bed_type == status: |
---|
48 | logger.info('Status %s of %s has not changed.' % (status,student_id)) |
---|
49 | psm='Student status has not changed!' |
---|
50 | break |
---|
51 | #return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm)) |
---|
52 | logger.info('Bed status %s of %s has changed to %s.' % (allocated_bed.bed_type,student_id,status)) |
---|
53 | query = Eq('bed_type',status) & Eq('student',context.portal_accommodation.not_occupied) |
---|
54 | records = context.portal_accommodation.evalAdvancedQuery(query) |
---|
55 | if len(records) < 1: |
---|
56 | psm='Bed change for %s failed, no free bed in category %s.' % (student_id,status) |
---|
57 | logger.info(psm) |
---|
58 | break |
---|
59 | beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied) |
---|
60 | logger.info('Bed %s released' % (allocated_bed.bed)) |
---|
61 | code,bed = beds.searchAndReserveBed(student_id,status) |
---|
62 | if code > 0: |
---|
63 | d = {} |
---|
64 | d['bed'] = bed |
---|
65 | d['student_status'] = status |
---|
66 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
67 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
68 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
69 | acco_doc = info['acco_doc'] |
---|
70 | acco_doc.edit(mapping=d) |
---|
71 | logger.info('Bed %s allocated to %s' % (bed,student_id)) |
---|
72 | psm='Bed changed!' |
---|
73 | break |
---|
74 | #return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm)) |
---|
75 | #acco_doc = info['acco_doc'] |
---|
76 | #acco_doc.edit(mapping={'bed':"-- cancelled by officer due to failed bed change request --"}) |
---|
77 | logger.info('Bed change for %s failed, code = %s' % (student_id,code)) |
---|
78 | #logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id)) |
---|
79 | psm='Bed change failed!' |
---|
80 | return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm)) |
---|
81 | |
---|
82 | info = {} |
---|
83 | records = [r for r in beds() if r.student and r.student != beds.not_occupied] |
---|
84 | list = [] |
---|
85 | to_modify = [] |
---|
86 | for r in records[1:100]: |
---|
87 | info = context.getAccommodationInfo(r.student) |
---|
88 | sbt = info.get('student_status',None) |
---|
89 | if sbt is None: |
---|
90 | continue |
---|
91 | elif r.bed_type == sbt: |
---|
92 | #list.append("Student %s bed_type %s ok" % (r.student,r.bed_type)) |
---|
93 | continue |
---|
94 | message = "Student %s bed_type %s != %s" % (r.student, |
---|
95 | r.bed_type, |
---|
96 | info['student_status']) |
---|
97 | list.append(message) |
---|
98 | #to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc'])) |
---|
99 | logger.info(message) |
---|
100 | |
---|
101 | return "\r".join(list) |
---|
102 | |
---|
103 | for former_bed, student, status,acco_doc in to_modify: |
---|
104 | beds.modifyRecord(bed=former_bed,student='') |
---|
105 | code,bed = beds.searchAndReserveBed(student,status) |
---|
106 | if code > 0: |
---|
107 | #from Products.zdb import set_trace; set_trace() |
---|
108 | d = {} |
---|
109 | d['bed'] = bed |
---|
110 | d['student_status'] = status |
---|
111 | acco_info = context.waeup_tool.getHallInfo(bed) |
---|
112 | d['acco_maint_code'] = acco_info.get('maintenance_code') |
---|
113 | d['acco_maint_fee'] = acco_info.get('maintenance_fee') |
---|
114 | acco_doc.edit(mapping=d) |
---|
115 | list.append("New bed %s allocated to %s, code = %s" % (bed, |
---|
116 | student, |
---|
117 | code)) |
---|
118 | return "\r".join(list) |
---|