def _clean_fetched_data(self, json_data):
     allowed_error_codes = ()
     
     if self._is_error(json_data):
         if json_data['error']['code'] in allowed_error_codes:
             # Handle the errors
             pass
         
         # All other errors should just be sent to the default handler.
         else:
             return json_data
     
     res = json_data['reservation']
     
     # convert the old times as well, unless the old times don't exist yet;
     # in that case, copy them from the current times.
     old_start = res.get('old_start_time', res['start_time'])
     old_end   = res.get('old_end_time', res['end_time'])
     res['old_start_time'] = from_isostring(old_start)
     res['old_end_time']   = from_isostring(old_end)
     
     # convert all iso to datetime
     res_start = res['start_time']
     res_end   = res['end_time']
     res['start_time'] = from_isostring(res_start)
     res['end_time']   = from_isostring(res_end)
     
     return json_data
    def _clean_fetched_data(self, vehicle_availability_json, vehid, vehmodel, vehpod, start_iso, end_iso):
        if self._is_error(vehicle_availability_json):
#            # There are a couple of errors that we'll let through...
#            if vehicle_availability_json['error']['code'] in (
#                'start_time_in_past', 'end_time_earlier_than_start'):
#                
#                lajson = {'location_availability':{
#                    'start_time': start_iso,
#                    'end_time': end_iso,
#                    'location': {
#                        'name': locname,
#                        'id': locid
#                    },
#                    'vehicle_availabilities': []
#                },
#                'alert':
#                    location_availability_json['error']['msg']
#                }
#                
#                location_availability_json = lajson
#            
#            # All other errors should just be sent to the default handler.
#            else:
                return vehicle_availability_json
        
        veh_avail = vehicle_availability_json['vehicle_availability']
        
        # convert all iso to datetime
        avail_start = veh_avail['start_time']
        avail_end   = veh_avail['end_time']
        veh_avail['start_time'] = from_isostring(avail_start)
        veh_avail['end_time']   = from_isostring(avail_end)
        
        return vehicle_availability_json
Beispiel #3
0
 def get_separate_iso_date_and_time_range(self):
     """
     A custom redefinition of get_time_range, as we expect the results from
     HTML date and time form fields for the start and end times.  The normal
     method operates on UTC timestamps.
     """
     import datetime
     start_date_str = self.request.get('start_date')
     end_date_str = self.request.get('end_date')
     start_time_str = self.request.get('start_time')
     end_time_str = self.request.get('end_time')
     
     now_time = datetime.datetime.now(Eastern) + datetime.timedelta(minutes=1)
     if start_date_str and start_time_str:
         start_dt_str = "%sT%s" % (start_date_str, start_time_str)
         start_time = from_isostring(start_dt_str)
     else:
         start_time = now_time
     
     if end_date_str and end_time_str:
         end_dt_str = "%sT%s" % (end_date_str, end_time_str)
         end_time = from_isostring(end_dt_str)
     else:
         end_time = now_time + datetime.timedelta(hours=3)
     
     return start_time, end_time
Beispiel #4
0
 def _clean_fetched_data(self, json_data, **defaults):
     allowed_error_codes = ('no_change_requested',)
     
     if self._is_error(json_data):
         if json_data['error']['code'] in allowed_error_codes:
             json_data = {'confirmation' : {
                 'reservation' : {
                     'start_time' : to_isostring(defaults['start_time']),
                     'end_time' : to_isostring(defaults['end_time']),
                     'liveid' : defaults['liveid'] }
             }}
         
         # All other errors should just be sent to the default handler.
         else:
             return json_data
     
     conf = json_data['confirmation']
     
     # convert all iso to datetime
     res_start = conf['reservation']['start_time']
     res_end   = conf['reservation']['end_time']
     conf['reservation']['start_time'] = from_isostring(res_start)
     conf['reservation']['end_time']   = from_isostring(res_end)
     
     return json_data
Beispiel #5
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
Beispiel #6
0
 def _get_rendered_response(self):
     values = self._build_chooser_values()
     
     headers = {}
     
     current_iso = self._get_param('current_value')
     current_dt = from_isostring(current_iso)
     
     values['current_year'] = '%d' % current_dt.year
     values['current_month'] = current_dt.month
     values['current_day'] = '%d' % current_dt.day
     values['current_hour'] = '%02d' % (current_dt.hour%12 or 12)
     values['current_minute'] = '%02d' % current_dt.minute
     values['current_midi'] = 'AM' if current_dt.hour < 12 else 'PM'
     
     values['years'] = ['%d' % year for year in xrange(current_dt.year, current_dt.year+3)]
     values['months'] = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
     values['days'] = ['%d' % (day+1) for day in xrange(31)]
     values['hours'] = ['%02d' % (hour+1) for hour in xrange(12)]
     values['minutes'] = ['%02d' % minute for minute in xrange(0,60,15)]
     values['midis'] = ['AM', 'PM']
     
     values.update(self._get_params())
     
     content = self.__render('choose_datetime.html', values)
     
     return content, headers
Beispiel #7
0
 def _clean_fetched_data(self, json_data, defaults):
     allowed_error_codes = ()
     
     if self._is_error(json_data):
         if json_data['error']['code'] in allowed_error_codes:
             # Handle the errors
             pass
         
         # All other errors should just be sent to the default handler.
         else:
             return json_data
     
     conf = json_data['confirmation']
     
     # convert all iso to datetime
     res_start = conf['reservation']['start_time']
     res_end   = conf['reservation']['end_time']
     conf['reservation']['start_time'] = from_isostring(res_start)
     conf['reservation']['end_time']   = from_isostring(res_end)
     
     return json_data
Beispiel #8
0
 def get_single_iso_datetime_range(self):
     """
     A custom redefinition of get_time_range.  This method operates on start
     and end dates and times specified as single ISO8601 datetime strings.
     """
     import datetime
     start_time_str = self.request.get('start_time')
     end_time_str = self.request.get('end_time')
     
     now_time = datetime.datetime.now(Eastern) + datetime.timedelta(minutes=1)
     if start_time_str:
         start_time = from_isostring(start_time_str)
     else:
         start_time = now_time
     
     if end_time_str:
         end_time = from_isostring(end_time_str)
     else:
         end_time = now_time + datetime.timedelta(hours=3)
     
     return start_time, end_time
    def _clean_fetched_data(self, json_data, defaults):
        allowed_error_codes = ()

        if self._is_error(json_data):
            if json_data["error"]["code"] in allowed_error_codes:
                # Handle the errors
                pass

            # All other errors should just be sent to the default handler.
            else:
                return json_data

        res = json_data["reservation"]

        # convert all iso to datetime
        res_start = res["start_time"]
        res_end = res["end_time"]
        res["start_time"] = from_isostring(res_start)
        res["end_time"] = from_isostring(res_end)

        return json_data
Beispiel #10
0
 def _clean_fetched_data(self, location_availability_json, locname, locid, start_iso, end_iso):
     if self._is_error(location_availability_json):
         # There are a couple of errors that we'll let through...
         if location_availability_json['error']['code'] in (
             'start_time_in_past', 'end_time_earlier_than_start'):
             
             lajson = {'location_availability':{
                 'start_time': start_iso,
                 'end_time': end_iso,
                 'location': {
                     'name': locname,
                     'id': locid
                 },
                 'vehicle_availabilities': []
             },
             'alert':
                 location_availability_json['error']['msg']
             }
             
             location_availability_json = lajson
         
         # All other errors should just be sent to the default handler.
         else:
             return location_availability_json
     
     loc_avail = location_availability_json['location_availability']
     
     # convert all iso to datetime
     avail_start = loc_avail['start_time']
     avail_end   = loc_avail['end_time']
     loc_avail['start_time'] = from_isostring(avail_start)
     loc_avail['end_time']   = from_isostring(avail_end)
     
     for veh_avail in loc_avail['vehicle_availabilities']:
         earliest = veh_avail.get('earliest', None)
         latest = veh_avail.get('latest', None)
         if earliest: veh_avail['earliest'] = from_isostring(earliest)
         if latest:   veh_avail['latest']   = from_isostring(latest)
     
     return location_availability_json
Beispiel #11
0
 def get_period(self):
     period = self.request.get('period', None)
     return from_isostring(period) if period else None
Beispiel #12
0
 def testShouldRecognizeYMDHi(self):
     iso = "2010-11-12T06:15"
     expected = datetime.datetime(2010,11,12,6,15,tzinfo=Eastern)
     
     self.assertEqual(from_isostring(iso), expected)
Beispiel #13
0
 def testShouldRecognizeY(self):
     iso = "2010"
     expected = datetime.datetime(2010,1,1,tzinfo=Eastern)
     
     self.assertEqual(from_isostring(iso), expected)
Beispiel #14
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