Exemplo n.º 1
0
 def _get_rendered_response(self):
     values, headers = self.__fetch(
         ''.join(['http://', self.__const.API_HOST, '/reservations.json']),
         'GET', 
         self._get_params(),
         self._package_cookies()
     );
     
     period_str = self._get_param('period')
     selected_period = from_isostring(period_str) if period_str else None
     
     NUM_PERIODS = 3
     from datetime import timedelta
     
     current_period = current_time()
     periods = [current_period]
     for x in range(1, NUM_PERIODS):
         current_period = current_period - timedelta(days=28)
         periods.append(current_period)
     
     values['periods'] = periods
     values['selected_period'] = selected_period
     
     STATUS_CURRENT  = 0
     STATUS_UPCOMING = 1
     STATUS_PAST     = 2
     
     res_list = values.get('reservation_list', None)
     if res_list:
         reservations = res_list['reservations']
         for reservation in reservations:
             now = current_time()
             start_time = from_isostring(reservation['start_time'])
             end_time = from_isostring(reservation['end_time'])
             
             reservation['start_time'] = start_time
             reservation['end_time'] = end_time
             if start_time > now:
                 reservation['status'] = STATUS_UPCOMING
             elif end_time > now:
                 reservation['status'] = STATUS_CURRENT
             else:
                 reservation['status'] = STATUS_PAST
         
         if not period_str:
             reservations.sort(key=lambda r: r['status'])
     
     values['reflect_url'] = self._construct_reflect_path()
     
     content = self.__render('my_reservations.html', values)
     
     return content, headers
Exemplo n.º 2
0
 def _get_rendered_response(self):
     # initialize parameters to send to api
     resid = self._get_param('reservation') or \
         None
     vehid = self._get_param('vehicle') or \
         None
     vehmodel = self._get_param('vehicle_model') or \
         None
     vehpod = self._get_param('vehicle_pod') or \
         None
     start_iso = self._get_param('start_time') or \
         self._build_time_param('start_time') or \
         None
     old_start_iso = self._get_param('old_start_time') or \
         None
     end_iso = self._get_param('end_time') or \
         self._build_time_param('end_time') or \
         None
     old_end_iso = self._get_param('old_end_time') or \
         None
     memo = self._get_param('memo') or \
         None
     status = self._get_param('status') or \
         None
     
     if None in (resid, vehid, vehmodel, vehpod, start_iso, old_start_iso, end_iso, old_end_iso, memo):
         # Only send start and end time if they are not None.  If we leave them 
         # out, the server will just use the default time.
         params = {}
         if start_iso: params['start_time'] = start_iso
         if end_iso: params['end_time'] = end_iso
         
         reservation_json, headers = self.__fetch(
             ''.join(['http://', self.__const.API_HOST, '/reservations/', resid, '.json']),
             'GET', 
             params,
             self._package_cookies()
         );
     else:
         reservation_json = {
             'reservation' : {
                 'liveid' : resid,
                 'vehicle' : {
                     'id' : vehid,
                     'model' : {
                         'name' : vehmodel
                     },
                     'pod' : {
                         'name' : vehpod
                     },
                 },
                 'start_time' : start_iso,
                 'end_time' : end_iso,
                 'old_start_time' : old_start_iso,
                 'old_end_time' : old_end_iso,
                 'memo' : memo
             }
         }
         headers = {}
     
     reservation_json = \
         self._clean_fetched_data(reservation_json)
     
     values = reservation_json
     if not self._is_error(reservation_json):
         values.update(
             self._build_chooser_queries(reservation_json, status))
     
     status_map = {'0':'current','1':'upcoming','2':'past'}
     values['status'] = status_map[status]
     def _ceil_time(dt):
         minutes = dt.minute % 15
         delta_minutes = 15 - minutes
         
         if delta_minutes == 15 and dt.second == 0:
             return dt
         else:
             import datetime
             delta = datetime.timedelta(minutes=delta_minutes)
             ceil_time = dt.replace(second=0) + delta
             
             return ceil_time
         
     values['soonest_end_time'] = _ceil_time(current_time())
     content = self.__render('modify_reservation.html', values)
     
     return content, headers
Exemplo n.º 3
0
 def testMakeChangeAndCancelAReservation(self):
     
     # This is a long test that will exercise the whole process of making,
     # checking, editing, and canceling a reservation.  It really is a long
     # test (on the order of 30 seconds), so make sure you're all unit-tested
     # up before running it; don't expect to make it a part of your regular
     # tests.
     
     session_cookie = self.sign_in_and_get_session_cookie()
     
     #######################################################################
     # First, make a reservation
     from pcs.wsgi_handlers.reservations import ReservationsJsonHandler
     handler = ReservationsJsonHandler()
     self.initialize_handler_for_session(handler, session_cookie)
     
     vehicleid = '96692246' # A Prius at 47th & Baltimore
     
     now = current_time()
     three_days = datetime.timedelta(days=3)
     later = now + three_days
     
     start_time = later.replace(hour=3, minute=00)
     end_time = later.replace(hour=3, minute=15)
     
     handler.request['vehicle'] = vehicleid
     handler.request['start_time'] = to_isostring(start_time)
     handler.request['end_time'] = to_isostring(end_time)
     handler.request['memo'] = 'reservation create test'
     
     handler.post()
     
     response_body = handler.response.out.getvalue()
     
     # I should get a confirmation
     response = json.loads(response_body)
     self.check_for_error(response)
     
     confirmation = response.get('confirmation', None)
     self.assert_(confirmation is not None)
     self.assertEqual(confirmation['reservation']['vehicle']['id'], vehicleid)
     
     liveid = confirmation['reservation'].get('liveid', None)
     self.assert_(liveid is not None, response_body)
     
     #######################################################################
     # Now, use the liveid from above to change the reservation
     from pcs.wsgi_handlers.reservations import ReservationJsonHandler
     handler = ReservationJsonHandler()
     self.initialize_handler_for_session(handler, session_cookie)
     
     start_time = later.replace(hour=3, minute=45)
     end_time = later.replace(hour=4, minute=00)
     
     handler.request['action'] = 'edit'
     handler.request['vehicle'] = vehicleid
     handler.request['start_time'] = to_isostring(start_time)
     handler.request['end_time'] = to_isostring(end_time)
     handler.request['memo'] = 'reservation modify test'
     
     handler.put(liveid)
     
     response_body = handler.response.out.getvalue()
     
     # I should get a confirmation
     response = json.loads(response_body)
     self.check_for_error(response)
     
     confirmation = response.get('confirmation', None)
     self.assert_(confirmation is not None)
     self.assertEqual(confirmation['reservation']['liveid'], liveid)
     self.assertEqual(confirmation['reservation']['vehicle']['id'], vehicleid)
     
     #######################################################################
     # Get the reservation information, using the same liveid
     from pcs.wsgi_handlers.reservations import ReservationJsonHandler
     handler = ReservationJsonHandler()
     self.initialize_handler_for_session(handler, session_cookie)
     
     handler.get(liveid)
     
     response_body = handler.response.out.getvalue()
     
     # I should get a reservation consistent with the one I just modified
     response = json.loads(response_body)
     self.check_for_error(response)
     
     reservation = response.get('reservation', None)
     self.assert_(reservation is not None)
     self.assertEqual(reservation['liveid'], liveid)
     self.assertEqual(reservation['vehicle']['id'], vehicleid)
     self.assertEqual(reservation['start_time'], to_isostring(start_time)[:16])
     self.assertEqual(reservation['end_time'], to_isostring(end_time)[:16])
     self.assertEqual(reservation['vehicle']['model']['name'], 'Prius Liftback')
     self.assertEqual(reservation['vehicle']['pod']['name'], '47th & Baltimore')
     self.assertEqual(reservation['vehicle']['pod']['id'], '30005')
     self.assertEqual(reservation['memo'], 'reservation modify test')
     
     #######################################################################
     # Now cancel the reservation
     handler = ReservationJsonHandler()
     self.initialize_handler_for_session(handler, session_cookie)
     
     handler.request['vehicle'] = vehicleid
     handler.request['start_time'] = to_isostring(start_time)
     handler.request['end_time'] = to_isostring(end_time)
     
     handler.delete(liveid)
     
     response_body = handler.response.out.getvalue()
     
     # I should get a confirmation
     response = json.loads(response_body)
     self.check_for_error(response)
     
     confirmation = response.get('confirmation', None)
     self.assert_(confirmation is not None)
     self.assertEqual(confirmation['reservation']['liveid'], liveid)
     self.assertEqual(confirmation['reservation']['vehicle']['id'], vehicleid)
Exemplo n.º 4
0
    def _get_rendered_response(self):
        # initialize parameters to send to api
        resid = self._get_param('reservation') or \
            None
        vehid = self._get_param('vehicle') or \
            None
        start_iso = self._get_param('start_time') or \
            None
        end_iso = self._get_param('end_time') or \
            None
        old_start_iso = self._get_param('old_start_time') or \
            None
        old_end_iso = self._get_param('old_end_time') or \
            None
        memo = self._get_param('memo') or \
            None
        
        params = {}
        params['start_time'] = start_iso
        params['end_time'] = end_iso
        params['vehicle'] = vehid
        if memo is not None:
            params['memo'] = memo
        
        start_time = from_isostring(start_iso)
        end_time = from_isostring(end_iso)
        old_start_time = from_isostring(old_start_iso)
        old_end_time = from_isostring(old_end_iso)
        
        if current_time() > start_time:
            if old_end_time > end_time:
                params['action'] = 'early'
            else:
                params['action'] = 'extend'
        else:
            params['action'] = 'edit'
        
#        if start_time != old_start_time or end_time != old_end_time:
        try:
            res_confirmation_json, headers = self.__fetch(
                ''.join(['http://', self.__const.API_HOST, '/reservations/', resid, '.json']),
                'PUT', 
                params,
                self._package_cookies()
            );
        except RetryErrors:
            reservation_json, headers = self.__fetch(
                ''.join(['http://', self.__const.API_HOST, '/reservations/', resid, '.json']),
                'GET',
                params,
                self._package_cookies()
            );
            res_confirmation_json = {'confirmation':reservation_json};
            res_confirmation_json['confirmation'].update({'event':'modify'});
#        else:
#            res_confirmation_json = {'confirmation' : {
#                'reservation' : {
#                    'start_time' : to_isostring(start_time),
#                    'end_time' : to_isostring(end_time),
#                    'liveid' : resid }}}
#            headers = {}
        
        res_confirmation_json = \
            self._clean_fetched_data(res_confirmation_json, 
                start_time=start_time, 
                end_time=end_time, 
                liveid=resid)
        
        values = res_confirmation_json
        
        if not self._is_error(res_confirmation_json):
            content = self._redirect_to('reservation_info', {
                'reservation': res_confirmation_json['confirmation']['reservation']['liveid'],
                'event': 'updated'
            })
        else:
            content = self.__render('error_catcher.html', values)
        
        return content, headers