mirror of
https://github.com/Fluffy-Bean/GameExpo23.git
synced 2025-05-17 17:04:54 +00:00
Move routes to own folder
This commit is contained in:
parent
7b86a6b6bb
commit
c1f8f67e64
9 changed files with 63 additions and 44 deletions
|
@ -3,6 +3,7 @@ Flask
|
||||||
Flask-SQLAlchemy
|
Flask-SQLAlchemy
|
||||||
Flask-Migrate
|
Flask-Migrate
|
||||||
Flask-Login
|
Flask-Login
|
||||||
|
WTForms
|
||||||
Flask-WTF
|
Flask-WTF
|
||||||
Flask-Assets
|
Flask-Assets
|
||||||
Flask-Caching
|
Flask-Caching
|
||||||
|
|
|
@ -8,7 +8,7 @@ then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if there are any changes to the database
|
# Check if there are any changes to the database
|
||||||
if ! $(flask --app website db check | grep -q "No changes in schema detected.");
|
if ! $(flask --app website db check) | grep -q "No changes in schema detected.";
|
||||||
then
|
then
|
||||||
echo "Database changes detected! Migrating..."
|
echo "Database changes detected! Migrating..."
|
||||||
flask --app website db migrate
|
flask --app website db migrate
|
||||||
|
|
|
@ -1,30 +1,21 @@
|
||||||
from flask import Flask, render_template, request, redirect, flash
|
from flask import Flask
|
||||||
from flask_login import login_required, login_user, logout_user, current_user
|
|
||||||
from flask_assets import Bundle
|
from flask_assets import Bundle
|
||||||
from flask_wtf import FlaskForm
|
|
||||||
from wtforms import StringField
|
|
||||||
from wtforms.validators import DataRequired
|
|
||||||
from website.models import Users
|
from website.models import Users
|
||||||
from website.extensions import db, migrate, login_manager, assets
|
from website.extensions import db, migrate, login_manager, assets
|
||||||
from website.config import INSTANCE_DIR, MIGRATION_DIR
|
from website.config import INSTANCE_DIR, MIGRATION_DIR
|
||||||
|
from website import routes
|
||||||
|
|
||||||
|
|
||||||
class LoginForm(FlaskForm):
|
app = Flask(__name__) # instance_path=INSTANCE_DIR
|
||||||
uuid = StringField(
|
|
||||||
"uuid",
|
|
||||||
validators=[DataRequired()],
|
|
||||||
render_kw={"placeholder": "12345678-ABCD-ABCD-ABCD-123456789EFG"},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__, instance_path=INSTANCE_DIR)
|
|
||||||
app.config.from_pyfile("config.py")
|
app.config.from_pyfile("config.py")
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
migrate.init_app(app, db, directory=MIGRATION_DIR)
|
migrate.init_app(app, db) # directory=MIGRATION_DIR
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
|
login_manager.init_app(app)
|
||||||
|
|
||||||
assets.init_app(app)
|
assets.init_app(app)
|
||||||
styles = Bundle(
|
styles = Bundle(
|
||||||
"sass/styles.sass",
|
"sass/styles.sass",
|
||||||
|
@ -36,26 +27,9 @@ assets.register("styles", styles)
|
||||||
scripts = Bundle("js/*.js", filters="jsmin", output="gen/packed.js")
|
scripts = Bundle("js/*.js", filters="jsmin", output="gen/packed.js")
|
||||||
assets.register("scripts", scripts)
|
assets.register("scripts", scripts)
|
||||||
|
|
||||||
|
app.register_blueprint(routes.blueprint)
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(user_id):
|
def load_user(user_id):
|
||||||
return Users.get(user_id)
|
return Users.query.filter_by(id=user_id).first()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def index():
|
|
||||||
return render_template("index.html")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/login", methods=["GET", "POST"])
|
|
||||||
def login():
|
|
||||||
form = LoginForm()
|
|
||||||
|
|
||||||
if form.validate_on_submit():
|
|
||||||
if user := Users.query.filter_by(uuid=str(form.uuid)).first():
|
|
||||||
login_user(user, remember=True)
|
|
||||||
return redirect(index)
|
|
||||||
else:
|
|
||||||
flash("Inncorrect login")
|
|
||||||
|
|
||||||
return render_template("login.html", form=form)
|
|
||||||
|
|
|
@ -6,7 +6,8 @@ def env(key):
|
||||||
return os.getenv(key)
|
return os.getenv(key)
|
||||||
|
|
||||||
|
|
||||||
SECRET_KEY = env("FLASK_KEY")
|
# SECRET_KEY = env("FLASK_KEY")
|
||||||
|
SECRET_KEY = "dev"
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URI = "sqlite:///site.db"
|
SQLALCHEMY_DATABASE_URI = "sqlite:///site.db"
|
||||||
|
|
||||||
|
|
|
@ -38,3 +38,6 @@ class Authors(db.Model):
|
||||||
class Users(db.Model, UserMixin):
|
class Users(db.Model, UserMixin):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
uuid = db.Column(db.String, nullable=False)
|
uuid = db.Column(db.String, nullable=False)
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
return int(self.id)
|
||||||
|
|
42
DV8-Expo/website/routes.py
Normal file
42
DV8-Expo/website/routes.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
from flask import Blueprint, render_template, redirect, flash
|
||||||
|
from flask_login import login_user, logout_user, login_required
|
||||||
|
from flask_wtf import FlaskForm
|
||||||
|
from wtforms import StringField
|
||||||
|
from wtforms.validators import DataRequired
|
||||||
|
from website.models import Users
|
||||||
|
|
||||||
|
|
||||||
|
blueprint = Blueprint("website", __name__)
|
||||||
|
|
||||||
|
|
||||||
|
class LoginForm(FlaskForm):
|
||||||
|
uuid = StringField(
|
||||||
|
"uuid",
|
||||||
|
validators=[DataRequired()],
|
||||||
|
render_kw={"placeholder": "12345678-ABCD-ABCD-ABCD-123456789EFG"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route("/")
|
||||||
|
def index():
|
||||||
|
return render_template("index.html")
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route("/login", methods=["GET", "POST"])
|
||||||
|
def login():
|
||||||
|
form = LoginForm()
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
if user := Users.query.filter_by(uuid=str(form.uuid.data)).first():
|
||||||
|
login_user(user, remember=True)
|
||||||
|
return redirect("/")
|
||||||
|
else:
|
||||||
|
flash("Incorrect login")
|
||||||
|
|
||||||
|
return render_template("login.html", form=form)
|
||||||
|
|
||||||
|
@blueprint.route("/logout")
|
||||||
|
@login_required
|
||||||
|
def logout():
|
||||||
|
logout_user()
|
||||||
|
return redirect("/")
|
File diff suppressed because one or more lines are too long
|
@ -25,12 +25,10 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Home</a>
|
<a href="{{ url_for('website.index') }}">Home</a>
|
||||||
|
|
||||||
<span><!-- This is a separator --></span>
|
<span><!-- This is a separator --></span>
|
||||||
|
<a href="{{ url_for('website.index') }}">About</a>
|
||||||
<a href="/">About</a>
|
{% if current_user.is_authenticated %}<a href="{{ url_for('website.logout') }}">Logout</a>{% endif %}
|
||||||
<a href="/login">Login</a>
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<span class="background">
|
<span class="background">
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<p>Do not share your UUID</p>
|
<p>Do not share your UUID</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
<form action="{{ url_for('login') }}" method="POST">
|
<form action="{{ url_for('website.login') }}" method="POST">
|
||||||
{{ form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
{{ form.uuid(size=36) }}
|
{{ form.uuid(size=36) }}
|
||||||
<button type="submit">Login</button>
|
<button type="submit">Login</button>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue