Move database to a separate file

master
Nikola Forró 7 years ago
parent d461dac0cc
commit d326ad2607

@ -4,49 +4,24 @@ import flask_login
import flask_restful import flask_restful
import flask_restful.fields import flask_restful.fields
import flask_restful.reqparse import flask_restful.reqparse
import flask_sqlalchemy
import itsdangerous import itsdangerous
import sqlalchemy import sqlalchemy
from db import db, Quote
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config.from_envvar('SETTINGS') app.config.from_envvar('SETTINGS')
db = flask_sqlalchemy.SQLAlchemy(app) db.init_app(app)
db.create_all() db.create_all(app=app)
api = flask_restful.Api(app)
login_manager = flask_login.LoginManager() login_manager = flask_login.LoginManager()
login_manager.init_app(app) login_manager.init_app(app)
flask_cors.CORS(app) api = flask_restful.Api(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'
id = db.Column(db.Integer, primary_key=True) flask_cors.CORS(app)
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)
quote_fields = { quote_fields = {
@ -76,6 +51,20 @@ filter_parser.add_argument('page_number', type=int)
filter_parser.add_argument('page_size', 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): class QuoteResource(flask_restful.Resource):
@flask_restful.marshal_with(quote_fields) @flask_restful.marshal_with(quote_fields)
def get(self, id): def get(self, id):

@ -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)
Loading…
Cancel
Save