def testPush(self): self.setMockUser() self.set_response('http://pubsubhubbub.appspot.com', content='', status_code=200, method="POST") test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', push_hub=True), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=1, schedule_period=5, ), headers=self.authHeaders()) feed = Feed.query().get() resp = self.app.get('/api/feeds/%s/subscribe' % (feed.key.urlsafe(), ), query_string={ "hub.mode": 'subscribe', "hub.topic": feed.feed_url, "hub.challenge": 'testing', "hub.verify_token": feed.verify_token, }) assert resp.data == 'testing' data = get_file_from_data('/data/df_feed.xml') resp = self.app.post('/api/feeds/%s/subscribe' % (feed.key.urlsafe(), ), data=data, headers={ 'Content-Type': 'application/xml', }) assert 2 == Entry.query().count() assert 1 == Entry.query(Entry.published == True, Entry.overflow == False).count() resp = self.app.post('/api/feeds/%s/subscribe' % (feed.key.urlsafe(), )) assert 2 == Entry.query(Entry.published == True).count()
def testPoller(self): self.setMockUser() another_fake_access_token = 'another_banana_stand' self.setMockUser(access_token=another_fake_access_token, username='******', id=2) test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, ), headers=self.authHeaders()) test_feed_url = 'http://example.com/rss2' self.set_rss_response(test_feed_url, content=self.buildRSS('test'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, ), headers={'Authorization': 'Bearer %s' % (another_fake_access_token, )}) assert 2 == Entry.query().count() test_feed_url = 'http://example.com/rss2' self.set_rss_response(test_feed_url, content=self.buildRSS('test2'), status_code=200) self.pollUpdate(2, n=0) assert 2 == Entry.query().count() self.pollUpdate() assert 3 == Entry.query().count()
def feed_push_update(feed_key): feed = ndb.Key(urlsafe=feed_key).get() if not feed: raise ndb.Return(("No feed", 404)) data = request.stream.read() logger.info('Got PuSH body: %s', data) logger.info('Got PuSH headers: %s', request.headers) if feed.hub_secret: server_signature = request.headers.get('X-Hub-Signature', None) signature = hmac.new(feed.hub_secret, data).hexdigest() if server_signature != signature: logger.warn('Got PuSH subscribe POST for feed key=%s w/o valid signature: sent=%s != expected=%s', feed_key, server_signature, signature) raise ndb.Return('') # I want to turn these off for a bit because they seem to be causing double posts raise ndb.Return('') yield feed.clear_error() parsed_feed = feedparser.parse(data) new_guids, old_guids = yield Entry.process_parsed_feed(parsed_feed, feed, overflow=False) yield Entry.publish_for_feed(feed, skip_queue=False) raise ndb.Return('')
def testSchedule(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test1'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=1, schedule_period=5, ), headers=self.authHeaders()) assert 0 == Entry.query(Entry.published == True, Entry.overflow == False).count() self.set_rss_response(test_feed_url, content=self.buildRSS('test2',), status_code=200) self.pollUpdate() assert 1 == Entry.query(Entry.published == True, Entry.overflow == False).count() self.set_rss_response(test_feed_url, content=self.buildRSS('test3'), status_code=200) self.pollUpdate() # Should have been rate limited assert 1 == Entry.query(Entry.published == True, Entry.overflow == False).count() # Set the entry back in time first_entry = Entry.query(Entry.published == True, Entry.overflow == False).get() first_entry.published_at = first_entry.published_at - timedelta(minutes=10) first_entry.put() self.set_rss_response(test_feed_url, content=self.buildRSS('test4'), status_code=200) self.pollUpdate() # Should not have been rate limited assert 2 == Entry.query(Entry.published == True, Entry.overflow == False).count()
def testMulitpleSchedule(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) assert 1 == Entry.query(Entry.published == True, Entry.overflow == True).count() self.pollUpdate() self.set_rss_response(test_feed_url, content=self.buildRSS('test2'), status_code=200) self.pollUpdate() # Should not have been rate limited assert 1 == Entry.query(Entry.published == True, Entry.overflow == False).count() self.set_rss_response(test_feed_url, content=self.buildRSS('test3'), status_code=200) self.pollUpdate() # Should not have been rate limited assert 2 == Entry.query(Entry.published == True, Entry.overflow == False).count() self.set_rss_response(test_feed_url, content=self.buildRSS('test4'), status_code=200) self.pollUpdate() # Should have been rate limited assert 2 == Entry.query(Entry.published == True, Entry.overflow == False).count() # We should have burned off the latest entry burned_entries = Entry.query(Entry.published == True, Entry.overflow == True).fetch(2) assert 1 == len(burned_entries) # So, the first entry was burned because it was already in the feed assert burned_entries[0].overflow_reason == OVERFLOW_REASON.BACKLOG
def feed_push_update(feed_key): feed = ndb.Key(urlsafe=feed_key).get() if not feed: raise ndb.Return(("No feed", 404)) data = request.stream.read() logger.info("Got PuSH body: %s", data) logger.info("Got PuSH headers: %s", request.headers) if feed.hub_secret: server_signature = request.headers.get("X-Hub-Signature", None) signature = hmac.new(feed.hub_secret, data).hexdigest() if server_signature != signature: logger.warn( "Got PuSH subscribe POST for feed key=%s w/o valid signature: sent=%s != expected=%s", feed_key, server_signature, signature, ) raise ndb.Return("") # I want to turn these off for a bit because they seem to be causing double posts raise ndb.Return("") yield feed.clear_error() parsed_feed = feedparser.parse(data) new_guids, old_guids = yield Entry.process_parsed_feed(parsed_feed, feed, overflow=False) yield Entry.publish_for_feed(feed, skip_queue=False) raise ndb.Return("")
def _render_tag(self, entry_name, caller): entry = Entry.query.filter_by(name=entry_name).first() if not entry: entry = Entry() entry.name = entry_name db.session.add(entry) db.session.commit() return entry.value if entry.value else '[%s]' % entry.name
def test_entry_type_must_be_in_models_schema(): entry = Entry() entry.save() log_entry = LogEntry() log_entry.entry_type = 'catfish' log_entry.entry = entry with pytest.raises(ValidationError): log_entry.save()
def test_feedback_with_invalid_fields(): entry = Entry() entry.yikes = 'not good!' log_entry = LogEntry() log_entry.entry_type = 'feedback' log_entry.entry = entry with pytest.raises(ValidationError): log_entry.save()
def get_feedback(): if request.method == 'POST': recipients = request.form.getlist('email') share = request.form.get('share-objectives') share_objectives = share == 'share-objectives' for recipient in recipients: user = current_user._get_current_object() other_user = User.objects.filter(email=recipient).first() entry = Entry() entry.requested_by = user.email entry.requested_from = other_user.email entry.requested_by_name = user.full_name entry.requested_from_name = other_user.full_name entry.template = request.form.get('feedback-template') entry.share_objectives = share_objectives if share_objectives: # get and attach objectives pass entry.save() log_entry = LogEntry() log_entry.entry_type = 'feedback' log_entry.owner = user log_entry.entry = entry log_entry.save() log_entry.add_tag('Feedback') _send_feedback_email(log_entry, user, other_user) flash('Submitted request') return render_template('feedback/get-feedback.html')
def test_log_with_invalid_fields(): entry = Entry() entry.foo = "this is not right there is no foo" entry.save() log_entry = LogEntry() log_entry.entry_type = "log" log_entry.entry = entry with pytest.raises(ValidationError): log_entry.save()
def test_objective_with_invalid_fields(): entry = Entry() entry.how = 'this is how' entry.what = 'this is what' entry.something_not_right = 'this is not right' with pytest.raises(ValidationError): log_entry = LogEntry() log_entry.entry_type = 'objective' log_entry.entry = entry log_entry.save()
def test_log_with_invalid_fields(): entry = Entry() entry.foo = 'this is not right there is no foo' entry.save() log_entry = LogEntry() log_entry.entry_type = 'log' log_entry.entry = entry with pytest.raises(ValidationError): log_entry.save()
def test_save_log_entry_with_content(): entry = Entry() entry.content = 'content is all there is' entry.save() log_entry = LogEntry() log_entry.entry_type = 'log' log_entry.entry = entry log_entry.save() from_db = LogEntry.objects(id=log_entry.id).get() assert from_db.entry.content == 'content is all there is' assert from_db.entry_type == 'log'
def testFeed(self): self.setMockUser() resp = self.app.get('/api/feeds', headers=self.authHeaders()) json_resp = json.loads(resp.data) assert len(json_resp['data']) == 0 self.set_rss_response("http://example.com/rss", content=self.buildRSS('test', items=10), status_code=200) test_feed_url = 'http://example.com/rss' # Should fail validation resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, max_stories_per_period=0, schedule_period=5, ), headers=self.authHeaders()) assert 0 == Feed.query().count() resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary='true', max_stories_per_period=1, schedule_period=5, ), headers=self.authHeaders()) json_resp = json.loads(resp.data) assert json_resp['data']['feed_url'] == test_feed_url assert 10 == Entry.query(Entry.published == True, Entry.overflow == True).count() feed_id = json_resp['data']['feed_id'] resp = self.app.get('/api/feeds/%s' % feed_id, headers=self.authHeaders()) json_resp = json.loads(resp.data) assert len(json_resp['data']['entries']) == 10 assert json_resp['data']['entries'][0]['guid'] == "http://example.com/buster/test_0" # Shouldn't be able to create two feeds for the same user resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary='true', max_stories_per_period=1, schedule_period=5, ), headers=self.authHeaders()) json_resp = json.loads(resp.data) assert 1 == Feed.query().count() resp = self.app.get('/api/feeds', headers=self.authHeaders()) json_resp = json.loads(resp.data) assert len(json_resp['data']) == 1 self.set_rss_response("http://example.com/rss", content=self.buildRSS('test2'), status_code=200) feed = Feed.query().get() Entry.update_for_feed(feed) assert 11 == Entry.query().count()
def test_feedback_with_valid_fields(): entry = Entry() entry.requested_from = '*****@*****.**' entry.requested_by = '*****@*****.**' entry.details = 'details' entry.share_objectives = True entry.sent = True entry.replied = True entry.save() log_entry = LogEntry() log_entry.entry_type = 'feedback' log_entry.entry = entry log_entry.save()
def test_save_log_entry_with_objective(): entry = Entry() entry.how = 'this is how' entry.what = 'this is what' entry.save() log_entry = LogEntry() log_entry.entry_type = 'objective' log_entry.entry = entry log_entry.save() from_db = LogEntry.objects(id=log_entry.id).get() assert from_db.entry_type == 'objective' assert from_db.entry.how == 'this is how' assert from_db.entry.what == 'this is what'
def testLargeOverflow(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=6), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) assert 0 == Entry.query(Entry.published == True, Entry.overflow == False).count() self.set_rss_response(test_feed_url, content=self.buildRSS('test2', items=6), status_code=200) resp = self.app.get('/api/feeds/all/update/1', headers={'X-Appengine-Cron': 'true'}) assert 2 == Entry.query(Entry.published == True, Entry.overflow == False).count() assert 10 == Entry.query(Entry.published == True, Entry.overflow == True).count()
def test_EntryClassFail(entrylist, capsys): with capsys.disabled(): now = datetime.datetime.utcnow() new_entry = Entry(age=entrylist[0], height=entrylist[1], weight=entrylist[2], gender=entrylist[3], s_blood_pressure=entrylist[4], d_blood_pressure=entrylist[5], cholesterol=entrylist[6], glucose=entrylist[7], smoking=entrylist[8], alcohol=entrylist[9], physical=entrylist[10], prediction=entrylist[11], predicted_on=now) assert new_entry.age == entrylist[0] assert new_entry.height == entrylist[1] assert new_entry.weight == entrylist[2] assert new_entry.gender == entrylist[3] assert new_entry.s_blood_pressure == entrylist[4] assert new_entry.d_blood_pressure == entrylist[5] assert new_entry.cholesterol == entrylist[6] assert new_entry.glucose == entrylist[7] assert new_entry.smoking == entrylist[8] assert new_entry.alcohol == entrylist[9] assert new_entry.physical == entrylist[10] assert new_entry.prediction == entrylist[11] assert new_entry.predicted_on == now
def create(self): entry = Entry( title=self.title.data, content=self.content.data) entry.save() note = LogEntry( entry_type='log', owner=current_user._get_current_object(), entry=entry) note.save() for tag in self.tags.data.split(','): note.add_tag(tag) return note
def api_add(): data = request.get_json() age = data['age'] height = data['height'] weight = data['weight'] gender = data['gender'] s_blood_pressure = data['s_blood_pressure'] d_blood_pressure = data['d_blood_pressure'] cholesterol = data['cholesterol'] glucose = data['glucose'] smoking = data['smoking'] alcohol = data['alcohol'] physical = data['physical'] prediction = data['prediction'] predicted_username = data['predicted_username'] new_entry = Entry(age=age, gender=gender, height=height, weight=weight, s_blood_pressure=s_blood_pressure, d_blood_pressure=d_blood_pressure, cholesterol=cholesterol, glucose=glucose, smoking=smoking, alcohol=alcohol, physical=physical, prediction=prediction, predicted_on=datetime.utcnow(), predicted_username=predicted_username) result = add_entry(new_entry) return jsonify({'id': result})
def predict(): # get data from drawing canvas and save as image img_dir = parseImage(request.get_data()) # Decoding and pre-processing base64 image img = image.img_to_array( image.load_img(img_dir + 'output.png', color_mode="grayscale", target_size=(48, 48))) / 255. # reshape data to have a single channel img = img.reshape(1, 48, 48, 1) predictions = make_prediction(img) for i, pred in enumerate(predictions): label_pred = "{}".format(labels[np.argmax(pred)]) new_entry = Entry( image_name=timezone.localize(datetime.now()).strftime("%d %B %Y, ") + label_pred + " by " + session['username'], prediction=label_pred, username=session['username'], predicted_on=timezone.localize(datetime.now())) add_entry(new_entry) response = [] for i in predictions[0]: response.append(i) ret = "" for i, pred in enumerate(predictions): ret = "{}".format(labels[np.argmax(pred)]) response.append(ret) return jsonify(response)
def predict(): form = PredictionForm() if request.method == 'POST': if form.validate_on_submit(): age = form.age.data height = form.height.data weight = form.weight.data gender = int(form.gender.data) s_blood_pressure = form.s_blood_pressure.data d_blood_pressure = form.d_blood_pressure.data cholesterol = int(form.cholesterol.data) glucose = int(form.glucose.data) smoking = int(form.smoking.data) alcohol = int(form.alcohol.data) physical = int(form.physical.data) # Engineer feature engineered columns bmi = weight / ((height / 100)**2) avg_bp = (s_blood_pressure + d_blood_pressure) / 2 X = [[ age, gender, height, weight, s_blood_pressure, d_blood_pressure, cholesterol, glucose, smoking, alcohol, physical, bmi, avg_bp ]] result = ai_model.predict(X) probability = round( ai_model.predict_proba(X)[0][int(result[0])] * 100, 2) new_entry = Entry(age=age, gender=gender, height=height, weight=weight, s_blood_pressure=s_blood_pressure, d_blood_pressure=d_blood_pressure, cholesterol=cholesterol, glucose=glucose, smoking=smoking, alcohol=alcohol, physical=physical, prediction=int(result[0]), predicted_on=datetime.utcnow(), predicted_username=current_user.username) add_entry(new_entry) if result[0] == 0: flash( f"Prediction: {display_result[result[0]]}, Probability: {probability}%", "success") else: flash( f"Prediction: {display_result[result[0]]}, Probability: {probability}%", "danger") else: flash("Error, cannot proceed with prediction", "danger") return render_template("index.html", title="Enter Parameters", form=form, index=True, entries=get_entries())
def delete_feed(feed_type, feed_id): """Delete a feed""" feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: raise ndb.Return(jsonify_error(message="Can't find that feed")) yield Entry.delete_for_feed(feed) yield feed.key.delete_async() raise ndb.Return(jsonify(status='ok'))
def testTags(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=1), status_code=200) self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) entry = Entry.query().order(-Entry.added).get() assert [] == entry.tags self.set_rss_response(test_feed_url, content=self.buildRSS('test1', items=1, tags=['example', 'feed']), status_code=200) self.pollUpdate() entry = Entry.query().order(-Entry.added).fetch(2)[0] assert ['example', 'feed'] == entry.tags
def published_entries_for_feed(feed_type, feed_id): feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") feed_data = feed.to_json() entries = [entry.to_json() for entry in Entry.latest(feed, order_by='-published_at', include_overflow=True).fetch(20)] feed_data['entries'] = entries return jsonify(status='ok', data=feed_data)
def testAuthor(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=1), status_code=200) self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) entry = Entry.query().order(-Entry.added).get() assert None == entry.author self.set_rss_response(test_feed_url, content=self.buildRSS('test1', items=1, author='Alex Kessinger'), status_code=200) self.pollUpdate() entry = Entry.query().order(-Entry.added).get() assert 'Alex Kessinger' == entry.author
def testAuthor(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=1), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) entry = Entry.query().get() assert None == entry.author self.set_rss_response(test_feed_url, content=self.buildRSS('test1', items=1, author='Alex Kessinger'), status_code=200) resp = self.app.get('/api/feeds/all/update/1', headers={'X-Appengine-Cron': 'true'}) entry = Entry.query().fetch(2)[1] assert 'Alex Kessinger' == entry.author
def test_log_with_valid_fields(): entry = Entry() entry.content = 'content is all there is' entry.save() log_entry = LogEntry() log_entry.entry_type = 'log' log_entry.entry = entry entry.save()
def test_EntryClassFail(entrylist, capsys): with capsys.disabled(): now = datetime.datetime.utcnow() new_entry = Entry(image_name=entrylist[0], prediction=entrylist[1], username=entrylist[2], predicted_on=now) assert new_entry.image_name == entrylist[0] assert new_entry.prediction == entrylist[1] assert new_entry.username == entrylist[2] assert new_entry.predicted_on == now
def unpublished_entries_for_feed(feed_type, feed_id): feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") feed_data = feed.to_json() entries = [ entry.to_json() for entry in Entry.latest_unpublished(feed).fetch(20) ] feed_data['entries'] = entries return jsonify(status='ok', data=feed_data)
def add(): if request.method == 'POST': form = request.form title = form.get('title') description = form.get('description') if not title or description: entry = Entry(title = title, description = description) db.session.add(entry) db.session.commit() return redirect('/') return "of the jedi"
def testShortUrl(self): self.setMockUser() self.set_rss_response("https://api-ssl.bitly.com/v3/shorten?login=example&apiKey=R_123&longUrl=http%3A%2F%2Fexample.com%2Fbuster%2Ftest1_0%3Futm_medium%3DApp.net%26utm_source%3DPourOver", content=BIT_LY_RESPONSE) test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=1), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, bitly_login='******', bitly_api_key='R_123', ), headers=self.authHeaders()) entry = Entry.query().get() assert [] == entry.tags self.set_rss_response(test_feed_url, content=self.buildRSS('test1', items=1), status_code=200) resp = self.app.get('/api/feeds/all/update/1', headers={'X-Appengine-Cron': 'true'}) entry = Entry.query().fetch(2)[1] assert entry.short_url == 'http://bit.ly/123'
def save_feed_preview(feed_type, feed_id): """preview a saved feed""" form = FEED_TYPE_TO_CLASS[feed_type].update_form(request.args) logger.info('form errors %s', form.errors) logger.info('form.publish_to_stream errors %s', form.publish_to_stream.errors) for errorMessages, fieldName in enumerate(form.errors): for err in errorMessages: logger.info("Feed errrors, %s", err) if not form.validate(): return jsonify_error(message="Invalid update data") feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") form.populate_obj(feed) feed.preview = True preview_entries = Entry.entry_preview(Entry.latest_for_feed_by_added(feed).fetch(3), feed, format=True) return jsonify(status='ok', data=preview_entries)
def feed(feed_type, feed_id): """Get a feed""" feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") feed_data = feed.to_json() entries = [ entry.to_dict(include=['guid', 'published', 'extra_info']) for entry in Entry.latest_for_feed(feed).fetch(10) ] feed_data['entries'] = entries return jsonify(status='ok', data=feed_data)
def testSingleItemPublish(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) entry = Entry.query().get() feed = Feed.query().get() resp = self.app.post('/api/feeds/%s/entries/%s/publish' % (feed.key.id(), entry.key.id()), headers=self.authHeaders())
def save_feed_preview(feed_type, feed_id): """preview a saved feed""" form = FEED_TYPE_TO_CLASS[feed_type].update_form(request.args) logger.info('form errors %s', form.errors) logger.info('form.publish_to_stream errors %s', form.publish_to_stream.errors) for errorMessages, fieldName in enumerate(form.errors): for err in errorMessages: logger.info("Feed errrors, %s", err) if not form.validate(): return jsonify_error(message="Invalid update data") feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") form.populate_obj(feed) feed.preview = True preview_entries = Entry.entry_preview( Entry.latest_for_feed_by_added(feed).fetch(3), feed, format=True) return jsonify(status='ok', data=preview_entries)
def test_save_log_entry_with_feedback(): entry = Entry() entry.requested_from = '*****@*****.**' entry.requested_by = '*****@*****.**' entry.details = 'details' entry.share_objectives = True entry.sent = True entry.replied = True entry.save() log_entry = LogEntry() log_entry.entry_type = 'feedback' log_entry.entry = entry log_entry.save() from_db = LogEntry.objects(id=log_entry.id).get() assert from_db.entry_type == 'feedback' assert from_db.entry.requested_from == '*****@*****.**' assert from_db.entry.requested_by == '*****@*****.**' assert from_db.entry.details == 'details' assert from_db.entry.share_objectives assert from_db.entry.sent assert from_db.entry.replied
def test_log_with_valid_fields(): entry = Entry() entry.content = "content is all there is" entry.save() log_entry = LogEntry() log_entry.entry_type = "log" log_entry.entry = entry entry.save()
def test_objective_with_valid_fields(): entry = Entry() entry.how = 'this is how' entry.what = 'this is what' entry.save() log_entry = LogEntry() log_entry.entry_type = 'objective' log_entry.entry = entry log_entry.save()
def api_add(): data = request.get_json() image_name = data['image_name'] prediction = data['prediction'] username = data['username'] new_entry = Entry(image_name=image_name, prediction=prediction, username=username, predicted_on=timezone.localize(datetime.now())) #invoke the add entry function to add entry result = add_entry(new_entry) #return the result of the db action return jsonify({'id': result})
def testFeedPreview(self): self.set_rss_response('http://techcrunch.com/feed/', content=self.buildRSS('test'), status_code=200) resp = self.app.get('/api/feed/preview?feed_url=http://techcrunch.com/feed/', headers=self.authHeaders()) assert 1 == len(json.loads(resp.data)['data']) self.set_rss_response('http://techcrunch.com/feed/2', content=self.buildRSS('test'), status_code=500) resp = self.app.get('/api/feed/preview?feed_url=http://techcrunch.com/feed/2', headers=self.authHeaders()) assert json.loads(resp.data)['message'] test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test'), status_code=200) resp = self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) assert 1 == Entry.query(Entry.published == True, Entry.overflow == True).count() feed = Feed.query().get() resp = self.app.get('/api/feeds/%s/preview' % (feed.key.id(), ), headers=self.authHeaders()) assert 'data' in json.loads(resp.data)
def feed_change(feed_type, feed_id): """Change a feed""" form = FEED_TYPE_TO_CLASS[feed_type].update_form(request.form) if not form.validate(): return jsonify_error(message="Invalid update data") feed = FEED_TYPE_TO_CLASS[feed_type].get_by_id(feed_id, parent=g.user.key) if not feed: return jsonify_error(message="Can't find that feed") form.populate_obj(feed) feed.put() feed_data = feed.to_json() entries = [ entry.to_dict(include=['title', 'link', 'published', 'published_at']) for entry in Entry.latest_for_feed(feed).fetch(10) ] feed_data['entries'] = entries return jsonify(status='ok', data=feed_data)
def testDbRaceCondition(self): self.setMockUser() test_feed_url = 'http://example.com/rss' self.set_rss_response(test_feed_url, content=self.buildRSS('test', items=1), status_code=200) self.app.post('/api/feeds', data=dict( feed_url=test_feed_url, include_summary=True, max_stories_per_period=2, schedule_period=5, ), headers=self.authHeaders()) feed = Feed.query().get() key = ndb.Key(Entry, '1', parent=feed.key) entry = Entry(key=key, guid='1') entry.put() entry_2 = Entry(key=key, guid='2') entry_2.put() assert Entry.query().count() == 2
def tq_feed_post_job(): """Post some feeds feed""" if not request.headers.get("X-AppEngine-QueueName"): raise ndb.Return(jsonify_error(message="Not a Task call")) keys = request.form.get("keys") if not keys: logger.info("Task Queue post no keys") raise ndb.Return(jsonify_error(code=500)) success = 0 errors = 0 num_posted = 0 ndb_keys = [ndb.Key(urlsafe=key) for key in keys.split(",")] feeds = yield ndb.get_multi_async(ndb_keys) logger.info("Got %d feed(s) for posting", len(feeds)) futures = [] for feed in feeds: futures.append((feed, Entry.publish_for_feed(feed))) for feed, future in futures: try: num_posts = yield future if num_posts is not None: num_posted += num_posts success += 1 except: errors += 1 if feed: logger.exception("Failed to Publish feed:%s" % (feed.feed_url,)) else: logger.exception("Failed to publish non-exsistant feed") logger.info("Post Feeds success:%s errors: %s num_posted: %s", success, errors, num_posted) raise ndb.Return(jsonify(status="ok"))
def tq_feed_post_job(): """Post some feeds feed""" if not request.headers.get('X-AppEngine-QueueName'): raise ndb.Return(jsonify_error(message='Not a Task call')) keys = request.form.get('keys') if not keys: logger.info('Task Queue post no keys') raise ndb.Return(jsonify_error(code=500)) success = 0 errors = 0 num_posted = 0 ndb_keys = [ndb.Key(urlsafe=key) for key in keys.split(',')] feeds = yield ndb.get_multi_async(ndb_keys) logger.info('Got %d feed(s) for posting', len(feeds)) futures = [] for feed in feeds: futures.append((feed, Entry.publish_for_feed(feed))) for feed, future in futures: try: num_posts = yield future if num_posts is not None: num_posted += num_posts success += 1 except: errors += 1 if feed: logger.exception('Failed to Publish feed:%s' % (feed.feed_url, )) else: logger.exception('Failed to publish non-exsistant feed') logger.info('Post Feeds success:%s errors: %s num_posted: %s', success, errors, num_posted) raise ndb.Return(jsonify(status='ok'))