Skip to content

Instantly share code, notes, and snippets.

@pascalc
Last active August 29, 2015 13:57
Show Gist options
  • Save pascalc/9495098 to your computer and use it in GitHub Desktop.
Save pascalc/9495098 to your computer and use it in GitHub Desktop.
# Schema: src/db/schema.py
class Perspective(Base, MyMixin):
__tablename__ = 'perspective'
id = Column(Integer, primary_key=True)
gender = Column(String(6), nullable=False)
text = Column(Text, nullable=False)
social_text = Column(Text, nullable=True)
created_at = Column(DateTime, default=datetime.now)
submissions = orm.relationship('Submission', backref='perspective',
lazy='dynamic')
rounds = orm.relationship('Round', backref='perspective',
lazy='dynamic')
# REST API: src/handlers/rest.py
manager.create_api(Perspective, methods=["GET", "PUT", "POST", "DELETE"])
# Admin interface: src/handlers/admin.py
admin.add_view(ModelView(Perspective))
# Custom Flask Route: src/handlers/custom.py
@app.route('/api/perspective/random')
def random_perspective():
random_id = session.query(Perspective)\
.order_by(sql.func.rand())\
.limit(1)\
.first()\
.id
return redirect('/api/perspective/%s' % random_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment