def populate(): "Populates the database with seed data for testing" db.metadata.drop_all(db.engine) db.create_all() seed = open(os.path.join(os.path.abspath(os.curdir), 'db/data.sql')) for line in seed: if line[0]!='#': db.engine.execute(line)
for question in questions: new_answer = Answer(user_id = user_id, task_id = new_task.id, question_id = question.id) db.session.add(new_answer) db.session.commit() pass def tally_user(result=None, **kw): user = User.query.filter_by(id = result['user_id']).first() num_answers = Answer.query.filter(and_(Answer.user_id==result['user_id'], Answer.value!=None)).count() user.tally = num_answers db.session.commit(); pass # Create the database tables. db.create_all() # Create the Flask-Restless API manager. manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db) # Create API endpoints, which will be available at /api/<tablename> manager.create_api(User, methods=['GET', 'POST', 'PATCH'], include_columns=['id', 'tally'], preprocessors={'POST': [new_user]}, postprocessors={'POST': [assign_task, notify_hash_id]}) manager.create_api(Task, methods=['GET'], include_columns=['id', 'title', 'content', 'questions']) manager.create_api(Question, methods=['GET'], include_columns=['id', 'title', 'choices'])