def _habit_or_task_report(self, item_name): ''' Mark a habit or a task as complete ''' handled = False speech = None if item_name: habits = Habit.Active(self.user) for h in habits: if item_name.lower() in h.name.lower(): # TODO: Timezone? done, hd = HabitDay.Toggle(h, datetime.today().date(), force_done=True) encourage = random.choice(HABIT_DONE_REPLIES) speech = "%s '%s' is marked as complete." % (encourage, h.name) handled = True break if not handled: # Check tasks tasks = Task.Recent(self.user) for t in tasks: if item_name.lower() in t.title.lower(): t.mark_done() t.put() speech = "Task '%s' is marked as complete." % (t.title) handled = True break if not handled: speech = "I'm not sure what you mean by '%s'." % item_name else: speech = "I couldn't tell what habit or task you completed." return speech
def range(self, d): ''' Return recent days of all active habits ''' start = self.request.get('start_date') end = self.request.get('end_date') habits = Habit.Active(self.user) habitdays = HabitDay.Range(self.user, habits, tools.fromISODate(start), until_date=tools.fromISODate(end)) self.set_response({ 'habits': [habit.json() for habit in habits], 'habitdays': tools.lookupDict(habitdays, keyprop="key_id", valueTransform=lambda hd: hd.json()) }, success=True)
def recent(self, d): ''' Return recent days of all active habits ''' self.success = True days = self.request.get_range('days', default=5) habits = Habit.Active(self.user) start_date = datetime.today() - timedelta(days=days) habitdays = HabitDay.Range(self.user, habits, start_date) self.set_response({ 'habits': [habit.json() for habit in habits], 'habitdays': tools.lookupDict(habitdays, keyprop="key_id", valueTransform=lambda hd: hd.json()) })
def _habit_commit(self, habit_param_raw): handled = False speech = None if habit_param_raw: habits = Habit.Active(self.user) for h in habits: if habit_param_raw.lower() in h.name.lower(): # TODO: Timezone? hd = HabitDay.Commit(h, datetime.today().date()) encourage = random.choice(HABIT_COMMIT_REPLIES) speech = "You've committed to '%s' today. %s" % (h.name, encourage) handled = True break if not handled: speech = "I'm not sure what you mean by '%s'. You may need to create a habit before you can commit to it." % habit_param_raw else: speech = "I couldn't tell what habit you want to commit to." return speech
def _habit_or_task_report(self, item_name): ''' Mark a habit or a task as complete ''' handled = False speech = None if item_name: habits = Habit.Active(self.user) for h in habits: if item_name.lower() in h.name.lower(): d = self.user.local_time().date() encourage = random.choice(HABIT_DONE_REPLIES) if h.has_daily_count(): done, hd = HabitDay.Increment(h, d) speech = "%s The count of '%s' has been increased to %d " % ( encourage, h.name, hd.count) remaining = h.tgt_daily - hd.count speech += "(habit complete!)" if not remaining else "(%d to go)" % remaining else: done, hd = HabitDay.Toggle(h, d, force_done=True) speech = "%s '%s' is marked as complete." % (encourage, h.name) handled = True break if not handled: # Check tasks tasks = Task.Recent(self.user) for t in tasks: if item_name.lower() in t.title.lower(): t.mark_done() t.put() speech = "Task '%s' is marked as complete." % (t.title) handled = True break if not handled: speech = "I'm not sure what you mean by '%s'." % item_name else: speech = "I couldn't tell what habit or task you completed." return speech
def get(self, d): # TODO: Async fetches with_habits = self.request.get_range('with_habits', default=0) == 1 with_tracking = self.request.get_range('with_tracking', default=1) == 1 with_goals = self.request.get_range('with_goals', default=1) == 1 with_tasks = self.request.get_range('with_tasks', default=1) == 1 date_start = self.request.get('date_start') date_end = self.request.get('date_end') dt_start, dt_end = tools.fromISODate(date_start), tools.fromISODate( date_end) iso_dates = [] habits = [] today = datetime.today() habitdays = [] goals = [] journals, iso_dates = MiniJournal.Fetch(self.user, dt_start, dt_end) if with_habits: habits = Habit.Active(self.user) habitdays = HabitDay.Range(self.user, habits, dt_start, dt_end) if with_tracking: tracking_days = TrackingDay.Range(self.user, dt_start, dt_end) if with_goals: goals = Goal.Year(self.user, today.year) if with_tasks: tasks = Task.DueInRange(self.user, dt_start, dt_end, limit=100) self.set_response( { 'dates': iso_dates, 'journals': [j.json() for j in journals if j], 'habits': [h.json() for h in habits], 'goals': [g.json() for g in goals], 'tasks': [t.json() for t in tasks], 'tracking_days': [p.json() for p in tracking_days], 'habitdays': tools.lookupDict(habitdays, keyprop="key_id", valueTransform=lambda hd: hd.json()) }, success=True)
def _maybe_get_habits(self): if self.habits is None: self.habits = tools.lookupDict(Habit.Active(self.user), "key_id")