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 fetch_daily_panel_data(self, since=None, until=None): self._maybe_get_habits() self._maybe_get_journal_questions() if not since: since = datetime.combine( (datetime.now() - timedelta(days=self.days_ago)).date(), time(0, 0)) if not until: until = datetime.combine( (datetime.now() - timedelta(days=self.days_ago_end)).date(), time(0, 0)) rows = [] habitdays_by_day = tools.partition( HabitDay.Range(self.user, self.habits.values(), since, until_date=until), lambda hd: tools.iso_date(hd.date)) tasks_by_day = tools.partition( Task.DueInRange(self.user, since, until, limit=500), lambda t: tools.iso_date(t.dt_due)) readables_by_day = tools.partition( Readable.Fetch(self.user, read=True, since=tools.iso_date(since), until=tools.iso_date(until)), lambda r: tools.iso_date(r.dt_read)) journals, iso_dates = MiniJournal.Fetch(self.user, start=since, end=until) journals_by_day = tools.partition( journals, lambda jrnl: tools.iso_date(jrnl.date)) cursor = since while cursor <= until: iso_date = tools.iso_date(cursor) tasks = tasks_by_day.get(iso_date, []) habitdays = habitdays_by_day.get(iso_date, []) readables = readables_by_day.get(iso_date, []) journals = journals_by_day.get(iso_date, []) journal = journals[0] if journals else None tasks_done = tasks_undone = habits_done = habits_cmt = habits_cmt_undone = items_read = 0 row = {} for t in tasks: if t.is_done(): tasks_done += 1 else: tasks_undone += 1 habits_checklist = self.habits.keys() # list of habit IDs for hd in habitdays: hid = hd.habit.id() h = self.habits.get(hid) if h: habits_checklist.remove(hid) row[self._habit_col(h)] = 'true' if hd.done else 'false' if hd.done: habits_done += 1 if hd.committed: habits_cmt += 1 if not hd.done: habits_cmt_undone += 1 if habits_checklist: # Missing habit-days, need to create columns anyway for hid in habits_checklist: h = self.habits.get(hid) if h: row[self._habit_col(h)] = 'false' items_read = len(readables) fav_items_read = len([r for r in readables if r.favorite]) row.update({ "id": iso_date, "date": iso_date, "tasks_done": tasks_done, "tasks_undone": tasks_undone, "habits_done": habits_done, "habits_cmt": habits_cmt, "habits_cmt_undone": habits_cmt_undone, "items_read": items_read, "fav_items_read": fav_items_read }) for q in self.journal_questions: name = q.get('name') value = None if journal: value = journal.get_data_value(name) numeric = q.get( 'response_type') in JOURNAL.NUMERIC_RESPONSES if numeric: value = tools.safe_number(value, default=0) elif isinstance(value, basestring): value = tools.removeNonAscii(value) else: value = str(value) if value else "" row[self._journal_col(q)] = value rows.append(row) cursor += timedelta(days=1) return rows