-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquotes.py
45 lines (32 loc) · 925 Bytes
/
quotes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
dburi = os.getenv('QUOTES_DB_URI')
if not dburi:
raise Exception('QUOTES_DB_URI environment variable not set')
app = Flask('fakeapp')
app.config['SQLALCHEMY_DATABASE_URI'] = dburi
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Quote(db.Model):
id = db.Column(db.Integer, primary_key=True)
quote = db.Column(db.Text)
author = db.Column(db.String(60))
def __repr__(self):
return '<User %r - %r>' % (self.author, self.quote)
@app.route('/')
def hello_world():
return {'message': 'Hello, World!'}
@app.route('/quote')
def quote():
quotes = Quote.query.all()
res = []
for q in quotes:
res.append({
'author': q.author,
'quote': q.quote
})
return {'quotes': res}
@app.cli.command("initdb")
def initdb():
db.create_all()