def create_reminder(time, user): reminder_event_time = watervibe_time.event_time_from_date(time) if (last_reminder_for_user(user) is None): time = watervibe_time.now_in_user_timezone(user) + timedelta(minutes=1) reminder = Reminder.objects.create( time=watervibe_time.string_for_date(time), user=user) reminder.save() for event in Event.objects.filter(user=user, day_of_week=time.isoweekday()): if watervibe_time.time_is_between_period(reminder_event_time, event.start_time, event.end_time): print "%f is during event from %f to %f" % ( reminder_event_time, event.start_time, event.end_time) reminder_event_time = events.adjust_reminder_at( reminder_event_time, event) print "Adjusted to %f" % reminder_event_time hour = int(reminder_event_time) minute = float(reminder_event_time - hour) * 60 time = time.replace(hour=int(hour), minute=int(minute)) if time < watervibe_time.now_in_user_timezone( user) or time < watervibe_time.date_for_string( last_reminder_for_user(user).time): time = time + timedelta(days=1) print "Adding reminder at " + watervibe_time.string_for_date(time) reminder = Reminder.objects.create( time=watervibe_time.string_for_date(time), user=user) reminder.save() return reminder
def sync(user): app = importlib.import_module(user.app + "." + user.app) for reminder in users.user_reminders(user): if reminder.app_id is None: if reminders.date(reminder) > now_in_user_timezone(user): alarm_app_id = app.set_alarm(user.app_id, reminders.date(reminder)) reminder.app_id = alarm_app_id reminder.save() if alarm_app_id is None: break user.last_sync = string_for_date(now_in_user_timezone(user)) user.next_sync_time = users.sync_time(user) user.save()
def fitbit_dashboard_alarms (app_id): try: user = User.objects.get(app_id = app_id, app = "fitbit") except: return [] alarms = [] count = 0 save = 0 for reminder in Reminder.objects.filter(user = user).order_by("time"): if save is not 1: if reminders.date(reminder) > now_in_user_timezone(user): save = 1 if save is 1: if count >= 5: break date = reminders.date(reminder) time_string = date.strftime("%I:%M%p") date_string = date.strftime("%m/%d/%Y") alarm = (time_string, date_string) alarms.append(alarm) count += 1 return alarms
def next_reminder_for(user): reminders = users.user_reminders(user) now = watervibe_time.now_in_user_timezone(user) for reminder in reminders: reminder_date = watervibe_time.date_for_string(reminder.time) if now < reminder_date: return reminder return None
def create_reminders_for_user(user): last_reminder = last_reminder_for_user(user) if last_reminder is None: soon = watervibe_time.now_in_user_timezone(user) + timedelta( minutes=30) last_reminder = create_reminder(soon, user) last_reminder_date = watervibe_time.date_for_string(last_reminder.time) hour = last_reminder_date.hour minute = last_reminder_date.minute start_of_period = last_reminder_date end_of_period = last_reminder_date + timedelta(days=1) ounces_drunk_in_period = users.ounces_to_drink_in_period( user, start_of_period, end_of_period) required_ounces = users.ounces_in_period(user, start_of_period, end_of_period) time_between_reminders = users.maximum_time_between_reminders( user, start_of_period) last_distance = 0 print "Setting reminders every %d seconds" % time_between_reminders.total_seconds( ) while ounces_drunk_in_period < required_ounces: next_reminder_date = last_reminder_date + time_between_reminders print next_reminder_date if next_reminder_date < watervibe_time.now_in_user_timezone(user): next_reminder_date = watervibe_time.now_in_user_timezone( user) + timedelta(minutes=5) last_reminder = create_reminder(next_reminder_date, user) if (next_reminder_date > end_of_period): break last_reminder_date = watervibe_time.date_for_string(last_reminder.time) print last_reminder_date ounces_drunk_in_period = users.ounces_to_drink_in_period( user, start_of_period, end_of_period) print ounces_drunk_in_period
def fitbit_dashboard_ounces(app_id): try: user = User.objects.get(app_id = app_id, app = "fitbit") except: return 0.0 now = now_in_user_timezone(user) start_of_day = now.replace(hour = 0, minute = 0) end_of_day = start_of_day + timedelta(days = 1) return users.ounces_in_period(user, start_of_day, end_of_day)
def fitbit_dashboard_sleep_times (app_id): try: user = User.objects.get(app_id = app_id, app = "fitbit") except: return [] today = now_in_user_timezone(user).isoweekday() sleep_times = [] sleeps = Event.objects.filter(tag = "sleep", day_of_week = today, user = user, is_active = 1) for sleep in sleeps: sleep_time = time_from_event_time(sleep.start_time) end_time = time_from_event_time(sleep.end_time) time_string = sleep_time.strftime("%I:%M%p") end_time_string = end_time.strftime("%I:%M%p") sleep_times.append((time_string, end_time_string)) return sleep_times
def sync_time(user): an_hour_from_now = now_in_user_timezone(user) + timedelta(hours=1) beginning_of_hour = an_hour_from_now.replace( minute=0) # Sets to beginning of hour return string_for_date(beginning_of_hour)