diff --git a/backend/app.py b/backend/app.py index fb1dfc6..e60f95c 100644 --- a/backend/app.py +++ b/backend/app.py @@ -4,49 +4,24 @@ import flask_login import flask_restful import flask_restful.fields import flask_restful.reqparse -import flask_sqlalchemy import itsdangerous import sqlalchemy +from db import db, Quote + app = flask.Flask(__name__) app.config.from_envvar('SETTINGS') -db = flask_sqlalchemy.SQLAlchemy(app) -db.create_all() - -api = flask_restful.Api(app) +db.init_app(app) +db.create_all(app=app) login_manager = flask_login.LoginManager() login_manager.init_app(app) -flask_cors.CORS(app) - - -@login_manager.request_loader -def load_user(request): - key = request.headers.get('X-Quotes-API-Key') - if not key: - return None - s = itsdangerous.TimedJSONWebSignatureSerializer(app.config['SECRET_KEY']) - try: - user = flask_login.UserMixin() - user.id = s.loads(key) - return user - except (itsdangerous.SignatureExpired, itsdangerous.BadSignature): - return None - - -class Quote(db.Model): - __tablename__ = 'quotes' +api = flask_restful.Api(app) - id = db.Column(db.Integer, primary_key=True) - video_id = db.Column(db.Integer) - date = db.Column(db.Date) - game = db.Column(db.String) - text = db.Column(db.String) - created_at = db.Column(db.DateTime) - updated_at = db.Column(db.DateTime) +flask_cors.CORS(app) quote_fields = { @@ -76,6 +51,20 @@ filter_parser.add_argument('page_number', type=int) filter_parser.add_argument('page_size', type=int) +@login_manager.request_loader +def load_user(request): + key = request.headers.get('X-Quotes-API-Key') + if not key: + return None + s = itsdangerous.TimedJSONWebSignatureSerializer(app.config['SECRET_KEY']) + try: + user = flask_login.UserMixin() + user.id = s.loads(key) + return user + except (itsdangerous.SignatureExpired, itsdangerous.BadSignature): + return None + + class QuoteResource(flask_restful.Resource): @flask_restful.marshal_with(quote_fields) def get(self, id): diff --git a/backend/db.py b/backend/db.py new file mode 100644 index 0000000..da416c3 --- /dev/null +++ b/backend/db.py @@ -0,0 +1,16 @@ +import flask_sqlalchemy + + +db = flask_sqlalchemy.SQLAlchemy() + + +class Quote(db.Model): + __tablename__ = 'quotes' + + id = db.Column(db.Integer, primary_key=True) + video_id = db.Column(db.Integer) + date = db.Column(db.Date) + game = db.Column(db.String) + text = db.Column(db.String) + created_at = db.Column(db.DateTime) + updated_at = db.Column(db.DateTime)