diff --git a/onlylegs/__init__.py b/onlylegs/__init__.py index ac5a72a..e15e3e1 100644 --- a/onlylegs/__init__.py +++ b/onlylegs/__init__.py @@ -15,7 +15,13 @@ from werkzeug.security import generate_password_hash from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR from onlylegs.models import User -from onlylegs.views import index as view_index, image as view_image, group as view_group, settings as view_settings, profile as view_profile +from onlylegs.views import ( + index as view_index, + image as view_image, + group as view_group, + settings as view_settings, + profile as view_profile, +) from onlylegs.api import media as api_media, group as api_group, account as api_account from onlylegs import auth as view_auth from onlylegs import gwagwa @@ -114,7 +120,7 @@ def create_app(): # pylint: disable=R0914 app.register_blueprint(view_group.blueprint) app.register_blueprint(view_profile.blueprint) app.register_blueprint(view_settings.blueprint) - + # APIS app.register_blueprint(api_media.blueprint) app.register_blueprint(api_group.blueprint) diff --git a/onlylegs/api/account.py b/onlylegs/api/account.py index 03754b2..0ac8fb9 100644 --- a/onlylegs/api/account.py +++ b/onlylegs/api/account.py @@ -43,12 +43,14 @@ def account_picture(user_id): if img_ext not in current_app.config["ALLOWED_EXTENSIONS"].keys(): logging.info("File extension not allowed: %s", img_ext) return jsonify({"error": "File extension not allowed"}), 403 - + if user.picture: # Delete cached files and old image os.remove(os.path.join(current_app.config["PFP_FOLDER"], user.picture)) cache_name = user.picture.rsplit(".")[0] - for cache_file in pathlib.Path(current_app.config["CACHE_FOLDER"]).glob(cache_name + "*"): + for cache_file in pathlib.Path(current_app.config["CACHE_FOLDER"]).glob( + cache_name + "*" + ): os.remove(cache_file) # Save file @@ -76,7 +78,7 @@ def account_username(user_id): """ user = db.get_or_404(User, user_id) new_name = request.form["name"] - + username_regex = re.compile(r"\b[A-Za-z0-9._-]+\b") # Validate the form @@ -84,7 +86,7 @@ def account_username(user_id): return jsonify({"error": "Username is invalid"}), 400 elif user.id != current_user.id: return jsonify({"error": "You are not allowed to do this, go away"}), 403 - + # Save to database user.username = new_name db.session.commit() diff --git a/onlylegs/api/group.py b/onlylegs/api/group.py index 24ee23e..9be39a5 100644 --- a/onlylegs/api/group.py +++ b/onlylegs/api/group.py @@ -45,7 +45,12 @@ def modify_group(): if group.author_id != current_user.id: return jsonify({"message": "You are not the owner of this group"}), 403 - if (action == "add" and not GroupJunction.query.filter_by(group_id=group_id, post_id=image_id).first()): + if ( + action == "add" + and not GroupJunction.query.filter_by( + group_id=group_id, post_id=image_id + ).first() + ): db.session.add(GroupJunction(group_id=group_id, post_id=image_id)) elif request.form["action"] == "remove": GroupJunction.query.filter_by(group_id=group_id, post_id=image_id).delete() diff --git a/onlylegs/api/media.py b/onlylegs/api/media.py index a933caa..60b90ce 100644 --- a/onlylegs/api/media.py +++ b/onlylegs/api/media.py @@ -8,7 +8,15 @@ import os import pathlib import logging -from flask import Blueprint, flash, abort, send_from_directory, jsonify, request, current_app +from flask import ( + Blueprint, + flash, + abort, + send_from_directory, + jsonify, + request, + current_app, +) from flask_login import login_required, current_user from colorthief import ColorThief @@ -61,7 +69,9 @@ def upload(): # Get file extension, generate random name and set file path img_ext = pathlib.Path(form_file.filename).suffix.replace(".", "").lower() img_name = "GWAGWA_" + str(uuid4()) - img_path = os.path.join(current_app.config["UPLOAD_FOLDER"], img_name + "." + img_ext) + img_path = os.path.join( + current_app.config["UPLOAD_FOLDER"], img_name + "." + img_ext + ) # Check if file extension is allowed if img_ext not in current_app.config["ALLOWED_EXTENSIONS"].keys(): @@ -106,17 +116,24 @@ def delete_image(image_id): # Check if image exists and if user is allowed to delete it (author) if post.author_id != current_user.id: logging.info("User %s tried to delete image %s", current_user.id, image_id) - return jsonify({"message": "You are not allowed to delete this image, heck off"}), 403 + return ( + jsonify({"message": "You are not allowed to delete this image, heck off"}), + 403, + ) # Delete file try: os.remove(os.path.join(current_app.config["UPLOAD_FOLDER"], post.filename)) except FileNotFoundError: - logging.warning("File not found: %s, already deleted or never existed", post.filename) + logging.warning( + "File not found: %s, already deleted or never existed", post.filename + ) # Delete cached files cache_name = post.filename.rsplit(".")[0] - for cache_file in pathlib.Path(current_app.config["CACHE_FOLDER"]).glob(cache_name + "*"): + for cache_file in pathlib.Path(current_app.config["CACHE_FOLDER"]).glob( + cache_name + "*" + ): os.remove(cache_file) GroupJunction.query.filter_by(post_id=image_id).delete() diff --git a/onlylegs/gwagwa.py b/onlylegs/gwagwa.py index c449bc7..6fb58f6 100644 --- a/onlylegs/gwagwa.py +++ b/onlylegs/gwagwa.py @@ -1,4 +1,4 @@ """ Gwa Gwa! """ -print("Gwa Gwa!") \ No newline at end of file +print("Gwa Gwa!")