mirror of
https://github.com/Fluffy-Bean/GameExpo23.git
synced 2025-05-18 17:34:52 +00:00
Fix __init__ file
Style code with Black
This commit is contained in:
parent
e7bbb02aba
commit
dcc9247ba0
8 changed files with 84 additions and 62 deletions
|
@ -1,16 +1,20 @@
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from flask import Flask, render_template, abort
|
from flask import Flask, render_template, abort
|
||||||
from flask_assets import Bundle
|
from flask_assets import Bundle
|
||||||
from werkzeug.exceptions import HTTPException
|
from werkzeug.exceptions import HTTPException
|
||||||
|
|
||||||
|
from server.config import MIGRATION_DIR, INSTANCE_DIR
|
||||||
from server.extensions import db, migrate, cache, assets, login_manager
|
from server.extensions import db, migrate, cache, assets, login_manager
|
||||||
from server.models import Users
|
from server.models import Users
|
||||||
from server import views, auth, api
|
from server import views, auth, api
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
app.config.from_pyfile('config.py')
|
app = Flask(__name__, instance_path=INSTANCE_DIR)
|
||||||
|
app.config.from_pyfile("config.py")
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
migrate.init_app(app, db)
|
migrate.init_app(app, db, directory=MIGRATION_DIR)
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
@ -23,7 +27,12 @@ assets.init_app(app)
|
||||||
scripts = Bundle("js/*.js", filters="jsmin", output="gen/scripts.js", depends="js/*.js")
|
scripts = Bundle("js/*.js", filters="jsmin", output="gen/scripts.js", depends="js/*.js")
|
||||||
assets.register("scripts", scripts)
|
assets.register("scripts", scripts)
|
||||||
|
|
||||||
styles = Bundle("sass/style.sass", filters="libsass, cssmin", output="gen/styles.css", depends="sass/*.sass")
|
styles = Bundle(
|
||||||
|
"sass/style.sass",
|
||||||
|
filters="libsass, cssmin",
|
||||||
|
output="gen/styles.css",
|
||||||
|
depends="sass/*.sass",
|
||||||
|
)
|
||||||
assets.register("styles", styles)
|
assets.register("styles", styles)
|
||||||
|
|
||||||
cache.init_app(app)
|
cache.init_app(app)
|
||||||
|
@ -42,9 +51,8 @@ def error_page(err):
|
||||||
if not isinstance(err, HTTPException):
|
if not isinstance(err, HTTPException):
|
||||||
abort(500)
|
abort(500)
|
||||||
return (
|
return (
|
||||||
render_template("error.html",
|
render_template(
|
||||||
error=err.code,
|
"error.html", error=err.code, msg=err.description, image=str(randint(1, 3))
|
||||||
msg=err.description,
|
),
|
||||||
image=str(randint(1, 3))),
|
|
||||||
err.code,
|
err.code,
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,14 +8,14 @@ from server.extensions import db
|
||||||
from server.config import BEARER_TOKEN
|
from server.config import BEARER_TOKEN
|
||||||
|
|
||||||
|
|
||||||
blueprint = Blueprint('api', __name__, url_prefix='/api')
|
blueprint = Blueprint("api", __name__, url_prefix="/api")
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/tokens', methods=['DELETE', 'POST'])
|
@blueprint.route("/tokens", methods=["DELETE", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def tokens():
|
def tokens():
|
||||||
if request.method == 'DELETE':
|
if request.method == "DELETE":
|
||||||
token_id = request.form['token_id']
|
token_id = request.form["token_id"]
|
||||||
if not token_id:
|
if not token_id:
|
||||||
return jsonify({"error": "No token ID provided!"}), 400
|
return jsonify({"error": "No token ID provided!"}), 400
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ def tokens():
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return jsonify({"success": "Token deleted!"}), 200
|
return jsonify({"success": "Token deleted!"}), 200
|
||||||
elif request.method == 'POST':
|
elif request.method == "POST":
|
||||||
if len(Tokens.query.filter_by(holder=current_user.id).all()) >= 5:
|
if len(Tokens.query.filter_by(holder=current_user.id).all()) >= 5:
|
||||||
return jsonify({"error": "You already have 5 tokens!"}), 403
|
return jsonify({"error": "You already have 5 tokens!"}), 403
|
||||||
|
|
||||||
|
@ -40,20 +40,20 @@ def tokens():
|
||||||
return jsonify({"success": "Token added!"}), 200
|
return jsonify({"success": "Token added!"}), 200
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/post', methods=['POST'])
|
@blueprint.route("/post", methods=["POST"])
|
||||||
def post():
|
def post():
|
||||||
form = request.form
|
form = request.form
|
||||||
|
|
||||||
if not form:
|
if not form:
|
||||||
return "Invalid form", 400
|
return "Invalid form", 400
|
||||||
if not request.headers.get('Authentication'):
|
if not request.headers.get("Authentication"):
|
||||||
return "Invalid authentication", 401
|
return "Invalid authentication", 401
|
||||||
|
|
||||||
if not isinstance(form['score'], int):
|
if not isinstance(form["score"], int):
|
||||||
return "Score must be an integer", 400
|
return "Score must be an integer", 400
|
||||||
if int(form['score']) < 0:
|
if int(form["score"]) < 0:
|
||||||
return "Score must be greater than 0", 400
|
return "Score must be greater than 0", 400
|
||||||
if form['difficulty'] not in [0, 1, 2, 3, 4]:
|
if form["difficulty"] not in [0, 1, 2, 3, 4]:
|
||||||
# 0 = Easy, Level 1
|
# 0 = Easy, Level 1
|
||||||
# 1 = Easy, Level 2
|
# 1 = Easy, Level 2
|
||||||
# 2 = Easy, Level 3
|
# 2 = Easy, Level 3
|
||||||
|
@ -61,32 +61,34 @@ def post():
|
||||||
# 4 = Hard
|
# 4 = Hard
|
||||||
return "Invalid difficulty", 400
|
return "Invalid difficulty", 400
|
||||||
|
|
||||||
if token_data := Tokens.query.filter_by(token=request.headers.get('Authentication')).first():
|
if token_data := Tokens.query.filter_by(
|
||||||
|
token=request.headers.get("Authentication")
|
||||||
|
).first():
|
||||||
# User is authenticated
|
# User is authenticated
|
||||||
# This is a registered user
|
# This is a registered user
|
||||||
|
|
||||||
score = Scores(
|
score = Scores(
|
||||||
score=form['score'],
|
score=form["score"],
|
||||||
difficulty=form['difficulty'],
|
difficulty=form["difficulty"],
|
||||||
achievements=form['achievements'],
|
achievements=form["achievements"],
|
||||||
user_id=token_data.holder,
|
user_id=token_data.holder,
|
||||||
)
|
)
|
||||||
db.session.add(score)
|
db.session.add(score)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return "Success!", 200
|
return "Success!", 200
|
||||||
elif request.headers.get('Authentication') == BEARER_TOKEN:
|
elif request.headers.get("Authentication") == BEARER_TOKEN:
|
||||||
# User is not authenticated, but has the correct token
|
# User is not authenticated, but has the correct token
|
||||||
# This is an anonymous user
|
# This is an anonymous user
|
||||||
|
|
||||||
if not form['playerName'] or len(form['playerId']) != 4:
|
if not form["playerName"] or len(form["playerId"]) != 4:
|
||||||
return "Invalid player name", 400
|
return "Invalid player name", 400
|
||||||
|
|
||||||
score = Scores(
|
score = Scores(
|
||||||
anonymous=True,
|
anonymous=True,
|
||||||
username=form['playerName'],
|
username=form["playerName"],
|
||||||
score=form['score'],
|
score=form["score"],
|
||||||
difficulty=form['difficulty'],
|
difficulty=form["difficulty"],
|
||||||
)
|
)
|
||||||
db.session.add(score)
|
db.session.add(score)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -9,18 +9,18 @@ from server.extensions import db
|
||||||
from server.models import Users, Tokens
|
from server.models import Users, Tokens
|
||||||
|
|
||||||
|
|
||||||
blueprint = Blueprint('auth', __name__)
|
blueprint = Blueprint("auth", __name__)
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/auth', methods=['GET'])
|
@blueprint.route("/auth", methods=["GET"])
|
||||||
def auth():
|
def auth():
|
||||||
return render_template('auth.html')
|
return render_template("auth.html")
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/account', methods=['GET'])
|
@blueprint.route("/account", methods=["GET"])
|
||||||
@login_required
|
@login_required
|
||||||
def account():
|
def account():
|
||||||
action = request.args.get('action', None)
|
action = request.args.get("action", None)
|
||||||
|
|
||||||
if action == "logout":
|
if action == "logout":
|
||||||
logout_user()
|
logout_user()
|
||||||
|
@ -32,10 +32,10 @@ def account():
|
||||||
flash("Insert password change function", "error")
|
flash("Insert password change function", "error")
|
||||||
|
|
||||||
token_list = Tokens.query.filter_by(holder=current_user.id).all()
|
token_list = Tokens.query.filter_by(holder=current_user.id).all()
|
||||||
return render_template('account.html', token_list=token_list)
|
return render_template("account.html", token_list=token_list)
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/register', methods=['POST'])
|
@blueprint.route("/register", methods=["POST"])
|
||||||
def register():
|
def register():
|
||||||
# Get the form data
|
# Get the form data
|
||||||
username = request.form["username"].strip()
|
username = request.form["username"].strip()
|
||||||
|
@ -46,7 +46,9 @@ def register():
|
||||||
|
|
||||||
# Validate the form
|
# Validate the form
|
||||||
if not username or not username_regex.match(username):
|
if not username or not username_regex.match(username):
|
||||||
error.append("Username is empty or invalid! Must be alphanumeric, and can contain ._-")
|
error.append(
|
||||||
|
"Username is empty or invalid! Must be alphanumeric, and can contain ._-"
|
||||||
|
)
|
||||||
if not password:
|
if not password:
|
||||||
error.append("Password is empty!")
|
error.append("Password is empty!")
|
||||||
elif len(password) < 8:
|
elif len(password) < 8:
|
||||||
|
@ -63,7 +65,7 @@ def register():
|
||||||
register_user = Users(
|
register_user = Users(
|
||||||
alt_id=str(uuid.uuid4()),
|
alt_id=str(uuid.uuid4()),
|
||||||
username=username,
|
username=username,
|
||||||
password=generate_password_hash(password, method="scrypt")
|
password=generate_password_hash(password, method="scrypt"),
|
||||||
)
|
)
|
||||||
db.session.add(register_user)
|
db.session.add(register_user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -72,7 +74,7 @@ def register():
|
||||||
return redirect(url_for("auth.auth"))
|
return redirect(url_for("auth.auth"))
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/login', methods=['POST'])
|
@blueprint.route("/login", methods=["POST"])
|
||||||
def login():
|
def login():
|
||||||
# Get the form data
|
# Get the form data
|
||||||
username = request.form["username"].strip()
|
username = request.form["username"].strip()
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
# Purely to make the code a bit more readable
|
# Purely to make the code a bit more readable
|
||||||
def env(key):
|
def env(key):
|
||||||
return os.getenv(key)
|
return os.getenv(key)
|
||||||
|
|
||||||
SECRET_KEY = env('FLASK_KEY')
|
|
||||||
BEARER_TOKEN = env('BEARER_TOKEN')
|
|
||||||
|
|
||||||
user = env('DB_USER')
|
SECRET_KEY = env("FLASK_KEY")
|
||||||
password = env('DB_PASSWORD')
|
BEARER_TOKEN = env("BEARER_TOKEN")
|
||||||
host = env('DB_HOST')
|
|
||||||
database = env('DB_NAME')
|
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{user}:{password}@{host}:5432/{database}"
|
user = env("DB_USER")
|
||||||
|
password = env("DB_PASSWORD")
|
||||||
|
host = env("DB_HOST")
|
||||||
|
database = env("DB_NAME")
|
||||||
|
|
||||||
|
SQLALCHEMY_DATABASE_URI = (
|
||||||
|
f"postgresql+psycopg2://{user}:{password}@{host}:5432/{database}"
|
||||||
|
)
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
SQLALCHEMY_POOL_RECYCLE = 621
|
SQLALCHEMY_POOL_RECYCLE = 621
|
||||||
|
|
||||||
MIGRATION_DIR = '/data/storage/migrations'
|
MIGRATION_DIR = "/data/storage/migrations"
|
||||||
INSTANCE_DIR = '/data/storage/instance'
|
INSTANCE_DIR = "/data/storage/instance"
|
||||||
|
|
|
@ -7,5 +7,5 @@ from flask_login import LoginManager
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
migrate = Migrate()
|
migrate = Migrate()
|
||||||
assets = Environment()
|
assets = Environment()
|
||||||
cache = Cache(config={'CACHE_TYPE': 'simple'})
|
cache = Cache(config={"CACHE_TYPE": "simple"})
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
|
|
|
@ -12,6 +12,7 @@ class Scores(db.Model):
|
||||||
Scores supports anonymous posting, and instead just wants to post a score,
|
Scores supports anonymous posting, and instead just wants to post a score,
|
||||||
then the username must be provided.Otherwise, it's grabbed from the user table
|
then the username must be provided.Otherwise, it's grabbed from the user table
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "scores"
|
__tablename__ = "scores"
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
@ -27,13 +28,14 @@ class Scores(db.Model):
|
||||||
server_default=db.func.now(),
|
server_default=db.func.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
scorer = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
|
scorer = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class Users(db.Model, UserMixin):
|
class Users(db.Model, UserMixin):
|
||||||
"""
|
"""
|
||||||
User table
|
User table
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "users"
|
__tablename__ = "users"
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
@ -47,8 +49,8 @@ class Users(db.Model, UserMixin):
|
||||||
server_default=db.func.now(),
|
server_default=db.func.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
scores = db.relationship('Scores', backref='user', lazy=True)
|
scores = db.relationship("Scores", backref="user", lazy=True)
|
||||||
tokens = db.relationship('Tokens', backref='user', lazy=True)
|
tokens = db.relationship("Tokens", backref="user", lazy=True)
|
||||||
|
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
return str(self.alt_id)
|
return str(self.alt_id)
|
||||||
|
@ -58,10 +60,11 @@ class Tokens(db.Model):
|
||||||
"""
|
"""
|
||||||
Token table
|
Token table
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "tokens"
|
__tablename__ = "tokens"
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
holder = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
holder = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
|
||||||
token = db.Column(db.String, nullable=False, unique=True)
|
token = db.Column(db.String, nullable=False, unique=True)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime,
|
db.DateTime,
|
||||||
|
|
|
@ -9,6 +9,8 @@ $darkBlue: var(--darkBlue)
|
||||||
|
|
||||||
@mixin button($color)
|
@mixin button($color)
|
||||||
text-decoration: none
|
text-decoration: none
|
||||||
|
text-align: center
|
||||||
|
white-space: nowrap
|
||||||
background-color: RGBA($color, 0.02)
|
background-color: RGBA($color, 0.02)
|
||||||
color: RGB($color)
|
color: RGB($color)
|
||||||
border-radius: 2px
|
border-radius: 2px
|
||||||
|
|
|
@ -2,22 +2,23 @@ from flask import Blueprint, request, render_template
|
||||||
from server.models import Scores
|
from server.models import Scores
|
||||||
|
|
||||||
|
|
||||||
blueprint = Blueprint('views', __name__)
|
blueprint = Blueprint("views", __name__)
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/')
|
@blueprint.route("/")
|
||||||
# @cache.cached(timeout=60)
|
# @cache.cached(timeout=60)
|
||||||
def index():
|
def index():
|
||||||
difficulty = request.args.get('diff', 0)
|
difficulty = request.args.get("diff", 0)
|
||||||
|
|
||||||
top_scores = (Scores.query
|
top_scores = (
|
||||||
.order_by(Scores.score.desc())
|
Scores.query.order_by(Scores.score.desc())
|
||||||
.filter_by(difficulty=difficulty)
|
.filter_by(difficulty=difficulty)
|
||||||
.limit(10)
|
.limit(10)
|
||||||
.all())
|
.all()
|
||||||
return render_template('scores.html', top_scores=top_scores)
|
)
|
||||||
|
return render_template("scores.html", top_scores=top_scores)
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/about')
|
@blueprint.route("/about")
|
||||||
def about():
|
def about():
|
||||||
return render_template('about.html')
|
return render_template("about.html")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue