def test_quote_calls(self): # Create q = Quote.Create(self.u, 'Overheard', "I think therefore I am") q.put() self.assertEqual(q.content, "I think therefore I am") self.assertEqual(q.source, "Overheard") # List response = self.get_json("/api/quote", {}, headers=self.api_headers) q = response.get('quotes')[0] self.assertEqual(q.get('content'), "I think therefore I am") # Update response = self.post_json("/api/quote", { 'id': q.get('id'), 'source': 'Somewhere else' }, headers=self.api_headers) q = response.get('quote') self.assertEqual(q.get('source'), 'Somewhere else') # Search response = self.get_json("/api/quote/search", {'term': "think"}, headers=self.api_headers) quotes = response.get('quotes') self.assertEqual(len(quotes), 1)
def test_quote_readable_matching(self): volley = [ ('1000', "Crony Beliefs", "Kevin Simler", "CRONY BELIEFS (SIMLER)", "I contend that the best way to understand all the crazy beliefs out there — aliens, conspiracies, and all the rest — is to analyze them as crony beliefs. Beliefs that have been \"hired\" not for the legitimate purpose of accurately modeling the world, but rather for social and political kickbacks." ), ('1001', "Thinking in Systems: A Primer", "Donna H. Meadows", "THINKING IN SYSTEMS A PRIMER (MEADOWS)", "XXX."), ] for v in volley: source_id, title, author, exp_slug, content = v r = Readable.CreateOrUpdate(self.u, source_id, title=title, author=author, source="test") r.put() Readable.put_sd_batch([r]) self.assertEqual(r.slug, exp_slug) author_names = author.split(' ') source = "%s (%s, %s)" % (title, author_names[-1], author_names[0]) q = Quote.Create(self.u, source, content) q.put() self.assertIsNotNone(q.readable) self.assertEqual(q.readable, r.key) self.assertEqual(q.source_slug(), exp_slug) r = Readable.GetByTitleAuthor(self.u, author, title) self.assertIsNotNone(r) self.assertEqual(r.source_id, source_id) # Create another quote with no readable to link to q = Quote.Create(self.u, "xxx", "content...") q.put() self.assertIsNone(q.readable) # Fetch quotes for readable quotes = Quote.Fetch(self.u, readable_id=r.key.id()) self.assertEqual(len(quotes), 1) self.assertEqual(quotes[0].source, source)
def test_quote_readable_matching(self): volley = [ ('1000', CRONY_TITLE, CRONY_AUTHOR, "CRONY BELIEFS (SIMLER)", CRONY_QUOTE), ('1001', "Thinking in Systems: A Primer", "Donna H. Meadows", "THINKING IN SYSTEMS A PRIMER (MEADOWS)", "XXX."), ] for v in volley: source_id, title, author, exp_slug, content = v r = Readable.CreateOrUpdate(self.u, source_id, title=title, author=author, source="test") r.put() Readable.put_sd_batch([r]) self.assertEqual(r.slug, exp_slug) author_names = author.split(' ') source = "%s (%s, %s)" % (title, author_names[-1], author_names[0]) q = Quote.Create(self.u, source, content) q.put() self.assertIsNotNone(q.readable) self.assertEqual(q.readable, r.key) self.assertEqual(q.source_slug(), exp_slug) r = Readable.GetByTitleAuthor(self.u, author, title) self.assertIsNotNone(r) self.assertEqual(r.source_id, source_id) # Create another quote with no readable to link to q = Quote.Create(self.u, "xxx", "content...") q.put() self.assertIsNone(q.readable) # Fetch quotes for readable quotes = Quote.Fetch(self.u, readable_id=r.key.id()) self.assertEqual(len(quotes), 1) self.assertEqual(quotes[0].source, source)
def batch_create(self, d): quotes = json.loads(self.request.get('quotes')) dbp = [] for q in quotes: if 'dt_added' in q and isinstance(q['dt_added'], basestring): q['dt_added'] = tools.fromISODate(q['dt_added']) q = Quote.Create(self.user, **q) dbp.append(q) if dbp: ndb.put_multi(dbp) self.success = True self.message = "Putting %d" % len(dbp) self.set_response()
def update(self, d): id = self.request.get('id') params = tools.gets( self, strings=['source', 'content', 'link', 'location', 'date'], lists=['tags']) quote = None if id: quote = self.user.get(Quote, id=id) else: if 'date' in params: params['dt_added'] = tools.fromISODate(params.get('date')) quote = Quote.Create(self.user, **params) self.message = "Quote saved!" if quote else "Couldn't create quote" self.success = quote is not None quote.Update(**params) quote.put() self.set_response({'quote': quote.json() if quote else None})
def evernote_webhook(self, d): ''' Evernote notifies us of a change Webhook request for note creation of the form: [base URL]/?userId=[user ID]&guid=[note GUID]¬ebookGuid=[notebook GUID]&reason=create ''' from services import flow_evernote from models import Quote ENABLED_REASONS = ['create'] note_guid = self.request.get('guid') evernote_id = self.request.get('userId') notebook_guid = self.request.get('notebookGuid') reason = self.request.get('reason') data = {} if reason in ENABLED_REASONS: user = User.query().filter(User.evernote_id == evernote_id).get() if user: config_notebook_ids = user.get_integration_prop( 'evernote_notebook_ids').split(',') # Comma sep if not config_notebook_ids or notebook_guid in config_notebook_ids: title, content, url = flow_evernote.get_note( user, note_guid) if title and content: # TODO: Tags (come in as guids) q = Quote.Create(user, source=title, content=content) q.Update(link=url) q.put() self.success = True else: self.message = "Failed to parse note" else: logging.warning( "Note from ignored notebook or user not found") else: logging.warning("User not found") else: logging.debug("Ignoring, reason: %s not enabled" % reason) self.set_response(data=data, debug=True)
def test_quote_calls(self): # Create q = Quote.Create(self.u, 'Overheard', "I think therefore I am") q.put() self.assertEqual(q.content, "I think therefore I am") self.assertEqual(q.source, "Overheard") # List response = self.get_json("/api/quote", {}, headers=self.api_headers) q = response.get('quotes')[0] self.assertEqual(q.get('content'), "I think therefore I am") # Update params = { 'id': q.get('id'), 'source': 'Somewhere else', 'content': "I think therefore I'm not", 'link': 'http://www.example.com', 'location': 'Location 100 of 1200', 'tags': 'tag1,tag2' } response = self.post_json("/api/quote", params, headers=self.api_headers) q = response.get('quote') for key, val in params.items(): if key == 'tags': self.assertEqual(q.get(key), val.split(',')) else: self.assertEqual(q.get(key), val) # Search response = self.get_json("/api/quote/search", {'term': "think"}, headers=self.api_headers) quotes = response.get('quotes') self.assertEqual(len(quotes), 1)