def random_batch(self, d): ''' Return a random batch, optionally filtered ''' BATCH_SIZE = 50 sample_keys = Readable.Fetch(self.user, with_notes=True, limit=500, keys_only=True) if len(sample_keys) > BATCH_SIZE: sample_keys = random.sample(sample_keys, BATCH_SIZE) readables = ndb.get_multi(sample_keys) self.set_response({ 'readables': [r.json() for r in readables] }, success=True)
def list(self, d): page, max, offset = tools.paging_params(self.request) favorites = self.request.get_range('favorites') == 1 with_notes = self.request.get_range('with_notes') == 1 unread = self.request.get_range('unread') == 1 read = self.request.get_range('read') == 1 since = self.request.get('since') # ISO readables = Readable.Fetch(self.user, favorites=favorites, unread=unread, read=read, with_notes=with_notes, since=since, limit=max, offset=offset) self.set_response({ 'readables': [r.json() for r in readables] }, success=True)
def test_evernote_webhook(self, get_note_mocked): EN_NOTE_GUID = "1000-0815-aefe-b8a0-8888" EN_USER_ID = "1001" EN_NOTEBOOK_ID = "ffff-0000" self.u.evernote_id = EN_USER_ID self.u.set_integration_prop('evernote_notebook_ids', EN_NOTEBOOK_ID) self.u.put() # Test article clip # Mock return from Evernote service get_note_mocked.return_value = (EN_NOTE_GUID, MEDIUM_TITLE, MEDIUM_FULL_CONTENT, MEDIUM_URL) self.get_json("/api/integrations/evernote/webhook", { 'reason': 'create', 'guid': EN_NOTE_GUID, 'notebookGuid': EN_NOTEBOOK_ID, 'userId': self.u.evernote_id }, headers=self.api_headers) readables = Readable.Fetch(self.u) self.assertEqual(len(readables), 1) r = readables[0] self.assertEqual(r.title, MEDIUM_TITLE) self.assertEqual(r.url, MEDIUM_URL) # Test quote/excerpt clip get_note_mocked.return_value = (EN_NOTE_GUID, CRONY_TITLE, CRONY_QUOTE, CRONY_URL) self.get_json("/api/integrations/evernote/webhook", { 'reason': 'create', 'guid': EN_NOTE_GUID, 'notebookGuid': EN_NOTEBOOK_ID, 'userId': self.u.evernote_id }, headers=self.api_headers) quotes = Quote.Fetch(self.u) self.assertEqual(len(quotes), 1) q = quotes[0] self.assertEqual(q.source, CRONY_TITLE) self.assertEqual(q.content, CRONY_QUOTE)
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