-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.py
90 lines (72 loc) · 2.61 KB
/
application.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
from flask import Flask
from flask.ext.restful import Resource, reqparse, Api
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import IntegrityError
app = Flask("flasktestexample")
api = Api(app)
app.debug = True
if os.environ.get('DATABASE_URL') is None:
engine = create_engine('postgres://flaskexample:flask@localhost:5432/flaskexample', convert_unicode=True)
else:
engine = create_engine(os.environ['DATABASE_URL'], convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
@app.teardown_request
def teardown_request(exception):
db_session.remove()
def init_db():
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
#User Model
class User(Base):
__tablename__ = 'users'
#from http://stackoverflow.com/a/11884806
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
id = Column(Integer, primary_key=True)
first_name = Column(String(200))
last_name = Column(String(200))
email = Column(String(200), unique=True)
#Parser arguments that Flask-Restful will check for
parser = reqparse.RequestParser()
parser.add_argument('first_name', type=str, required=True, help="First Name Cannot Be Blank")
parser.add_argument('last_name', type=str, required=True, help="Last Name Cannot Be Blank")
parser.add_argument('email', type=str, required=True, help="Email Cannot Be Blank")
#Flask Restful Views
class UserView(Resource):
def get(self, id):
e = User.query.filter(User.id == id).first()
if e is not None:
return e.as_dict()
else:
return {}
class UserViewList(Resource):
def get(self):
results = []
for row in User.query.all():
results.append(row.as_dict())
return results
def post(self):
args = parser.parse_args()
o = User()
o.first_name = args["first_name"]
o.last_name = args["last_name"]
o.email = args["email"]
try:
db_session.add(o)
db_session.commit()
except IntegrityError, exc:
return {"error": exc.message}, 500
return o.as_dict(), 201
#Flask Restful Routes
api.add_resource(UserViewList, '/users')
api.add_resource(UserView, '/users/<string:id>')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)