source: WAeUP_SRP/trunk/skins/waeup_fceokene/reserve_accommodation.py @ 3814

Last change on this file since 3814 was 3772, checked in by Henrik Bettermann, 16 years ago

enable random bed booking order

File size: 6.3 KB
Line 
1## Script (Python) "reserve_accommodation.py"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=REQUEST
8# $Id: reserve_accommodation.py 3406 2008-04-02 07:16:08Z henrik $
9"""
10process the the accommodation reservation
11"""
12try:
13    from Products.zdb import set_trace
14except:
15    def set_trace():
16        pass
17import DateTime
18current = DateTime.DateTime()
19pr = context.portal_registration
20wftool = context.portal_workflow
21lt = context.portal_layouts
22info = context.getAccommodationInfo()
23
24import logging
25logger = logging.getLogger('Skins.reserve_accommodation')
26
27#if info is None:
28#    return context.REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())
29
30if info is None:
31    member_id = str(context.portal_membership.getAuthenticatedMember())
32    logger.info('%s tried to reserve accommodation' % (member_id))
33    return context.REQUEST.RESPONSE.redirect("%s/srp_invalid_access" % context.portal_url())
34
35mode = 'create'
36if not info['booking_allowed']:
37    return context.REQUEST.RESPONSE.redirect("%s/no_booking_allowed" % info['student'].absolute_url())
38student = info['student']
39student_id = info['student_id']
40acco_id = info['acco_id']
41#session = info['session'][1]
42session = info['session'][0]
43
44validate = REQUEST.has_key("cpsdocument_create_button")
45if info.has_key('acco') and info['acco']:
46    validate = REQUEST.has_key("cpsdocument_edit_button")
47    if info['maintenance_paid']:
48        if not info['acco_doc'].acco_maint_fee:
49            d_update = {}
50            hall_info = context.waeup_tool.getHallInfo(info['acco_doc'].bed)
51            d_update['acco_maint_fee'] = hall_info.get('maintenance_fee')
52            wftool.doActionFor(info['acco'],'reserve_bed')
53            info['acco_doc'].edit(mapping=d_update)
54            wftool.doActionFor(info['acco'],'pay_maintenance_fee')
55        return context.REQUEST.RESPONSE.redirect("%s/accommodation_view" % info['acco'].absolute_url())
56    # elif info['online_payment']:
57    #     return context.REQUEST.RESPONSE.redirect("%s/accommodation_view" % info['acco'].absolute_url())
58    #     # return context.REQUEST.RESPONSE.redirect("%s/pay_interswitch_acco?paytype=HOM" % info['student'].absolute_url())
59    else:
60        mode = 'edit'
61d = {}
62button = "Book"
63if mode == 'edit':
64    d['acco_res_date'] = info['acco_doc'].acco_res_date
65    d['acco_res_sc_pin'] = info['acco_doc'].acco_res_sc_pin
66    d['acco_maint_fee'] = info['acco_doc'].acco_maint_fee
67    d['student_status'] = info['acco_doc'].student_status
68    d['bed'] = info['acco_doc'].bed
69    d['session'] = info['acco_doc'].session
70    button = "Pay Maintainance Fee"
71res,psm,ds = lt.renderLayout(layout_id= 'student_accommodation_fe',
72                      schema_id= 'student_accommodation',
73                      context=context,
74                      mapping=validate and REQUEST,
75                      ob=d,
76                      layout_mode=mode,
77                      formaction = "reserve_accommodation",
78                      button = button
79                      )
80if psm == 'invalid':                      # or info['error'] is not None:
81    member_id = str(context.portal_membership.getAuthenticatedMember())
82    #logger.info('%s, %s' % (member_id,info['error'] ))
83    logger.info('%s entered invalid data' % (member_id))
84    if psm == 'invalid':
85        psm = "Please correct your input."
86    else:
87        psm = "Error in data, unable to reserve bed."
88    return context.accommodation_edit_form(rendered = res,
89                                 psm = psm,
90                                 #psm = "%s, %s" % (psm,ds),
91                                 mode = mode,
92                                 ds = ds,
93                                 )
94elif psm == '':
95    return context.accommodation_edit_form(rendered = res,
96                                 psm = None,
97                                 mode = mode,
98                                 ds = ds,
99                                 )
100elif psm == 'valid':
101    if mode == 'create':
102        #set_trace()
103        pin = str(ds.get('acco_res_sc_pin'))
104        code,bed = context.portal_accommodation.searchAndReserveBed(student_id,
105                                                                    "%s" % (info['student_status']),
106                                                                    random_order=True)
107        while True:
108            if code == 1:
109                break
110            if code == -1:
111                break
112                #return context.accommodation_edit_form(rendered = res,
113                #                        psm = "%s" % bed,
114                #                        mode = mode,
115                #                        ds = ds,
116                #                        )
117            elif code == -2:
118                return context.accommodation_edit_form(rendered = res,
119                                        psm = "No bed available. Your category is already fully booked.",
120                                        mode = mode,
121                                        ds = ds,
122                                        )
123            else: # unknown error
124                return context.accommodation_edit_form(rendered = res,
125                                        psm = "Unexpected Error!",
126                                        mode = mode,
127                                        ds = ds,
128                                        )
129        student.invokeFactory('StudentAccommodation',acco_id)
130        hall_info = context.waeup_tool.getHallInfo(bed)
131        ds.set('acco_maint_code', hall_info.get('maintenance_code'))
132        ds.set('acco_maint_fee', hall_info.get('maintenance_fee'))
133        ds.set('acco_res_date', current)
134        ds.set('bed', bed)
135        ds.set('session', session)
136        ds.set('student_status',info['student_status'])
137        acco = getattr(student,acco_id)
138        acco.getContent().edit(mapping=ds)
139        #wftool.doActionFor(acco,'pay_maintenance_fee',dest_container=acco)
140        return context.REQUEST.RESPONSE.redirect("%s/reserve_accommodation" % student.absolute_url())
141#pin = str(ds.get('acco_main_sc_pin'))
142#pp = context.portal_pins
143#acco = getattr(student,acco_id)
144d = {}
145d['acco_maint_sc_pin'] = ds.get('acco_maint_sc_pin')
146d['acco_maint_date'] = current
147info['acco_doc'].edit(mapping=d)
148wftool.doActionFor(info['acco'],'pay_maintenance_fee')
149return context.REQUEST.RESPONSE.redirect("%s/accommodation_view" % student.absolute_url())
150
Note: See TracBrowser for help on using the repository browser.