def post(self): if not self.request.get('Caller'): raise ValueError('No caller available for call %s' % self.request.get('CallGuid')) caller = Subscriber.get_by_key_name(self.request.get('Caller')) if self.request.get('Digits'): if not caller: # create new subscriber caller = Subscriber( key_name = str(self.request.get('Caller')), phone_number = self.request.get('Caller') ) caller.days_subscribed = int(self.request.get('Digits')) if caller.days_subscribed > app_settings.MAX_DAYS: caller.days_subscribed = app_settings.MAX_DAYS caller.call_guid = self.request.get('CallGuid') caller.zip_code = int(self.request.get('CallerZip')) caller.put() # add call scheduler as background task defer(methods.schedule_checks, caller.key().name(), caller.days_subscribed) if caller: days_subscribed = caller.days_subscribed else: days_subscribed = None self.context = { 'days_subscribed': days_subscribed, 'base_url': 'http://' + os.environ['HTTP_HOST'] } return xml_response(self, 'gather.xml', self.context)
def schedule_checks(phone_number, days_subscribed): """ schedule checks at precise intervals throughout day for a subscriber """ caller = Subscriber.get_by_key_name(str(phone_number)) # TODO: use caller.zip_code to adjust call times for timezones now = datetime.datetime.now() if now.hour < 17: now_day = (now - datetime.timedelta(days=1)) else: now_day = now for day in range(days_subscribed): now_day += datetime.timedelta(days=1) for check_time in (13, 15, 17): check_time -= 4 #for New York. 7 for San Francisco eta = datetime.datetime(year=now_day.year, month=now_day.month,day=now_day.day, hour=check_time) if eta < now: continue defer(scheduled_check, phone_number,caller.call_guid, _eta=eta)