def test_users_with_notifications(self): user1, user2, notes = self.create_many() start = datetime.datetime.now() - datetime.timedelta(hours=12) end = datetime.datetime.now() + datetime.timedelta(hours=12) user_ids = Notification.users_with_notifications( util.datelike_to_iso_string(start), util.datelike_to_iso_string(end), ) self.assertEqual({user1.uid, user2.uid}, set(user_ids))
def get(self): # Takes query string param `today`, which defaults to a date string for # today, if today is a Monday, else next Monday. start = self.request.get('start', None) end = self.request.get('end', None) # What time period is of interest? if start and end: # Make sure inputs are formatted correctly datetime.datetime.strptime(start, config.iso_datetime_format) datetime.datetime.strptime(end, config.iso_datetime_format) if start > end: raise Exception("DigestNotifications requires end > start.") logging.info("Received custom start and end times: {} - {}".format( start, end)) else: # most recent midnight, pacific standard time today = datetime.date.today() end = datetime.datetime(today.year, today.month, today.day, 8) start = end - datetime.timedelta(hours=24) # from here forward we'll work in ISO 8601 strings. start = util.datelike_to_iso_string(start) end = util.datelike_to_iso_string(end) logging.info("Using default start and end times: {} - {}".format( start, end)) user_ids = Notification.users_with_notifications(start, end) # Launch a task to process each user. This way we can handle arbitrary # growth in the number of users without worrying about time or memory # limits, assuming number of notifications per user is fairly constant. for id in user_ids: taskqueue.add( url='/task/{}/digest_notifications'.format(id), params={ 'start': start, 'end': end }, queue_name='default', ) logging.info(user_ids) self.response.write(json.dumps({'user_ids': user_ids}))