source: WAeUP_SRP/base/skins/waeup_accommodation/change_bed.py @ 3325

Last change on this file since 3325 was 3206, checked in by Henrik Bettermann, 17 years ago

2 old fixes (in custom)

  • Property svn:keywords set to Id
File size: 5.0 KB
RevLine 
[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 3206 2008-02-23 22:59:10Z henrik $
11"""
[1135]12relocate to a new bed if allocated bed is wrong
[1104]13"""
14import logging
[3156]15from Products.AdvancedQuery import Eq, Between, Le,In
[3165]16
[1566]17logger = logging.getLogger('Skins.change_bed')
[1104]18request = context.REQUEST
[1135]19redirect = request.RESPONSE.redirect
[1104]20mtool = context.portal_membership
21wf = context.portal_workflow
22member = mtool.getAuthenticatedMember()
23member_id = str(member)
24path_info = request.get('PATH_INFO').split('/')
25
26if mtool.isAnonymousUser():
27    return None
[1135]28students = context.portal_url.getPortalObject().campus.students
[1104]29beds = context.portal_accommodation
30studs = context.students_catalog
[1135]31student_id = context.getStudentId()
32
33if student_id is not None:
[1566]34    logger.info('%s requests bed change for %s' % (member_id,student_id))
[1135]35    info = context.getAccommodationInfo(student_id)
[3047]36    if info['acco'] is None:
37        logger.info('No accommodation object for %s' % (student_id))
[3151]38        return redirect("%s/%s/no_booking_allowed" % (students.absolute_url(),student_id))
[1135]39    res = beds(student=student_id)
[3047]40    status = info['student_status']
[3156]41    while True:
42        if len(res) == 0:
43            psm = 'No bed allocated for %s' % (student_id)
44            logger.info(psm)
45            break
[3047]46        allocated_bed = res[0]
[3156]47        if allocated_bed.bed_type == status:
[3169]48            logger.info('Status %s of %s has not changed.' % (status,student_id))
[3156]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))
[3169]52        logger.info('Bed status %s of %s has changed to %s.' % (allocated_bed.bed_type,student_id,status))
[3156]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:
[3169]56            psm='Bed change for %s failed, no free bed in category %s.' % (student_id,status)
[3156]57            logger.info(psm)
58            break
59        beds.modifyRecord(bed=allocated_bed.bed,student=beds.not_occupied)
[3165]60        logger.info('Bed %s released' % (allocated_bed.bed))
[3156]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 --"})
[3169]77        logger.info('Bed change for %s failed, code = %s' % (student_id,code))
[3156]78        #logger.info('%s cancelled booking of bed %s by %s' % (member,bed,student_id))
79        psm='Bed change failed!'
[3151]80    return redirect("%s/%s/%s/waeup_document_view?portal_status_message=%s" % (students.absolute_url(),student_id,info['acco_id'],psm))
[1164]81
[1135]82info = {}
[3169]83records = [r for r in beds() if  r.student and  r.student != beds.not_occupied]
[1104]84list = []
85to_modify = []
[3206]86logger.info('started without student context')
87for r in records:
[1104]88    info = context.getAccommodationInfo(r.student)
[1206]89    sbt = info.get('student_status',None)
90    if sbt is None:
91        continue
92    elif r.bed_type == sbt:
[3169]93        #list.append("Student %s bed_type %s ok" % (r.student,r.bed_type))
[1104]94        continue
[3170]95    message = "Student %s bed_type %s != %s" % (r.student,
[1104]96                                                  r.bed_type,
[3170]97                                                  info['student_status'])
98    list.append(message)
[3169]99    #to_modify.append((r.bed,r.student,info['student_status'],info['acco_doc']))
[3170]100    logger.info(message)
[3206]101logger.info('finished')
[3170]102
[3169]103return "\r".join(list)
[3170]104
[1117]105for former_bed, student, status,acco_doc in to_modify:
[1104]106    beds.modifyRecord(bed=former_bed,student='')
107    code,bed = beds.searchAndReserveBed(student,status)
108    if code > 0:
[1117]109        #from Products.zdb import set_trace; set_trace()
[1104]110        d = {}
111        d['bed'] = bed
112        d['student_status'] = status
[2000]113        acco_info = context.waeup_tool.getHallInfo(bed)
[1104]114        d['acco_maint_code'] = acco_info.get('maintenance_code')
115        d['acco_maint_fee'] = acco_info.get('maintenance_fee')
[1117]116        acco_doc.edit(mapping=d)
[1566]117        list.append("New bed %s allocated to %s, code = %s" % (bed,
118                                                              student,
[1104]119                                                              code))
120return "\r".join(list)
Note: See TracBrowser for help on using the repository browser.