def protect_db(): # used to have the 'db' fixture as a param """ Creates a new database session for a test. Note you must use this fixture if your test connects to db. Here we not only support commit calls but also rollback calls in tests, :coolguy:. """ connection = engine.connect() transaction = connection.begin() db_session.begin_nested() # session is actually a scoped_session # for the `after_transaction_end` event, we need a session instance to # listen for, hence the `session()` call @event.listens_for(db_session(), 'after_transaction_end') def restart_savepoint(sess, trans): if trans.nested and not trans._parent.nested: db_session.expire_all() db_session.begin_nested() yield db_session db_session.remove() transaction.rollback() connection.close()
def add_location(): content = request.get_json() r = Restaurant(content['id'], content['title'], content['coords']['latitude'], content['coords']['longitude'], content['google_ratings'], content['types']) db_session.add(r) db_session.commit() db_session.remove() return 'success'
def shutdown_session(exception=None): db_session.remove()
''' print ("Title: " + title) print ("Date: " + str(datetime_obj)) print ("Content: " + markdown_content) ''' new_post = Post(title = title, content = markdown_content, date = datetime_obj) return new_post # Delete current db_session.query(Post).delete() # Seeding code # TODO grab all the posts posts = get_formatted_posts() # TODO Iterate through each and push it to the database for post in posts: parsed_post = parse_post(post) db_session.add(parsed_post) # Commit to db db_session.commit() # Show new Posts Post.query.all() # End db code db_session.remove()