mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 03:10:00 +00:00
Cleanip init.py, utils.py and decorators.py
This commit is contained in:
4
app.py
4
app.py
@@ -1,11 +1,9 @@
|
|||||||
from application import app
|
from application import app
|
||||||
from flask import redirect, url_for
|
from flask import redirect, url_for
|
||||||
from flask_login import login_required # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
# home route
|
# home route, place holder in case we want a home page
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
@login_required
|
|
||||||
def index():
|
def index():
|
||||||
return redirect(url_for("dash.index"))
|
return redirect(url_for("dash.index"))
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,6 @@ db.init_app(app)
|
|||||||
|
|
||||||
migrate = Migrate(app, db)
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
# bp import
|
|
||||||
from application.auth.views import auth_blueprint
|
|
||||||
from application.dash.views import dash_blueprint
|
|
||||||
|
|
||||||
# Login manager
|
# Login manager
|
||||||
from application.auth.models import User
|
from application.auth.models import User
|
||||||
@@ -32,6 +29,7 @@ login_manager.init_app(app) # type: ignore
|
|||||||
login_manager.login_view = "auth.login" # type: ignore
|
login_manager.login_view = "auth.login" # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
# Gets all the user data
|
||||||
@login_manager.user_loader # type: ignore
|
@login_manager.user_loader # type: ignore
|
||||||
def load_user(user_id): # type: ignore
|
def load_user(user_id): # type: ignore
|
||||||
return User.query.get(int(user_id)) # type: ignore
|
return User.query.get(int(user_id)) # type: ignore
|
||||||
@@ -39,5 +37,14 @@ def load_user(user_id): # type: ignore
|
|||||||
|
|
||||||
# Blueprint magic
|
# Blueprint magic
|
||||||
|
|
||||||
|
# bp import
|
||||||
|
# Would like to do this at the top of the file,
|
||||||
|
# but can't easily figure out how to do this.
|
||||||
|
# I think everything that the views depend on have to be moved
|
||||||
|
# into a seperate .py and imported.
|
||||||
|
from application.auth.views import auth_blueprint
|
||||||
|
from application.dash.views import dash_blueprint
|
||||||
|
|
||||||
|
# Register blueprints
|
||||||
app.register_blueprint(dash_blueprint, url_prefix="/dash")
|
app.register_blueprint(dash_blueprint, url_prefix="/dash")
|
||||||
app.register_blueprint(auth_blueprint, url_prefix="/auth")
|
app.register_blueprint(auth_blueprint, url_prefix="/auth")
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ from functools import wraps
|
|||||||
from flask import redirect, url_for
|
from flask import redirect, url_for
|
||||||
|
|
||||||
|
|
||||||
|
# Decorator that checks if the current user is logged in and an admin
|
||||||
|
# Could be shortened by adding the login_required decorator
|
||||||
|
# and removing the logic here
|
||||||
def admin_required(f: Callable[..., Any]) -> Callable[..., Any]:
|
def admin_required(f: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args: ..., **kwargs: ...):
|
def decorated_function(*args: ..., **kwargs: ...):
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
from werkzeug.utils import secure_filename
|
|
||||||
import os
|
import os
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
from application import app
|
from application import app
|
||||||
from flask_login import current_user # type: ignore
|
from flask_login import current_user # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
# save image to static folder
|
||||||
def saveImage(image: ...):
|
def saveImage(image: ...):
|
||||||
filename = secure_filename(image.filename)
|
filename = secure_filename(image.filename)
|
||||||
|
# Path should be /application/static/[user_id]/[filename]
|
||||||
save_path = os.path.join(
|
save_path = os.path.join(
|
||||||
app.config["UPLOAD_FOLDER"], # type: ignore
|
app.config["UPLOAD_FOLDER"], # type: ignore
|
||||||
str(current_user.id),
|
str(current_user.id),
|
||||||
filename,
|
filename,
|
||||||
)
|
)
|
||||||
|
# Create path is it doesn't exist
|
||||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||||
|
# Save the image
|
||||||
image.save(save_path) # type: ignore
|
image.save(save_path) # type: ignore
|
||||||
filename2 = str(current_user.id) + "/" + filename
|
# Return the filename that is stored in database.
|
||||||
|
# Only done to keep a single default image, this should be done differently
|
||||||
|
filename2 = str(current_user.id) + "/" + filename # [user_id]/[filename]
|
||||||
return filename2
|
return filename2
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user