Format code with Black

This commit is contained in:
Michał 2023-04-07 12:35:30 +00:00
parent fef8a557d2
commit 128464d43f
18 changed files with 697 additions and 604 deletions

View file

@ -25,14 +25,14 @@ from sqlalchemy.orm import sessionmaker
from gallery import db from gallery import db
USER_DIR = platformdirs.user_config_dir('onlylegs') USER_DIR = platformdirs.user_config_dir("onlylegs")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
login_manager = LoginManager() login_manager = LoginManager()
assets = Environment() assets = Environment()
cache = Cache(config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300}) cache = Cache(config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 300})
compress = Compress() compress = Compress()
@ -40,37 +40,37 @@ def create_app(test_config=None):
""" """
Create and configure the main app Create and configure the main app
""" """
app = Flask(__name__, instance_path=os.path.join(USER_DIR, 'instance')) app = Flask(__name__, instance_path=os.path.join(USER_DIR, "instance"))
# Get environment variables # Get environment variables
load_dotenv(os.path.join(USER_DIR, '.env')) load_dotenv(os.path.join(USER_DIR, ".env"))
print("Loaded environment variables") print("Loaded environment variables")
# Get config file # Get config file
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8', mode='r') as file: with open(os.path.join(USER_DIR, "conf.yml"), encoding="utf-8", mode="r") as file:
conf = safe_load(file) conf = safe_load(file)
print("Loaded config") print("Loaded config")
# App configuration # App configuration
app.config.from_mapping( app.config.from_mapping(
SECRET_KEY=os.environ.get('FLASK_SECRET'), SECRET_KEY=os.environ.get("FLASK_SECRET"),
DATABASE=os.path.join(app.instance_path, 'gallery.sqlite3'), DATABASE=os.path.join(app.instance_path, "gallery.sqlite3"),
UPLOAD_FOLDER=os.path.join(USER_DIR, 'uploads'), UPLOAD_FOLDER=os.path.join(USER_DIR, "uploads"),
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'], ALLOWED_EXTENSIONS=conf["upload"]["allowed-extensions"],
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'], MAX_CONTENT_LENGTH=1024 * 1024 * conf["upload"]["max-size"],
ADMIN_CONF=conf['admin'], ADMIN_CONF=conf["admin"],
UPLOAD_CONF=conf['upload'], UPLOAD_CONF=conf["upload"],
WEBSITE_CONF=conf['website'], WEBSITE_CONF=conf["website"],
) )
if test_config is None: if test_config is None:
app.config.from_pyfile('config.py', silent=True) app.config.from_pyfile("config.py", silent=True)
else: else:
app.config.from_mapping(test_config) app.config.from_mapping(test_config)
login_manager.init_app(app) login_manager.init_app(app)
login_manager.login_view = 'gallery.index' login_manager.login_view = "gallery.index"
login_manager.session_protection = 'strong' login_manager.session_protection = "strong"
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):
@ -79,49 +79,46 @@ def create_app(test_config=None):
@login_manager.unauthorized_handler @login_manager.unauthorized_handler
def unauthorized(): def unauthorized():
error = 401 error = 401
msg = 'You are not authorized to view this page!!!!' msg = "You are not authorized to view this page!!!!"
return render_template('error.html', error=error, msg=msg), error return render_template("error.html", error=error, msg=msg), error
js_pre = Bundle( js_pre = Bundle(
'js/pre/*.js', "js/pre/*.js", filters="jsmin", output="gen/pre_packed.js", depends="**"
filters='jsmin',
output='gen/pre_packed.js',
depends='**'
) )
js_post = Bundle( js_post = Bundle(
'js/post/*.js', "js/post/*.js", filters="jsmin", output="gen/post_packed.js", depends="**"
filters='jsmin',
output='gen/post_packed.js',
depends='**'
) )
styles = Bundle( styles = Bundle(
'sass/*.sass', "sass/*.sass", filters="libsass,cssmin", output="gen/styles.css", depends="**"
filters='libsass,cssmin',
output='gen/styles.css',
depends='**'
) )
assets.register('js_pre', js_pre) assets.register("js_pre", js_pre)
assets.register('js_post', js_post) assets.register("js_post", js_post)
assets.register('styles', styles) assets.register("styles", styles)
# Error handlers, if the error is not a HTTP error, return 500 # Error handlers, if the error is not a HTTP error, return 500
@app.errorhandler(Exception) @app.errorhandler(Exception)
def error_page(err): # noqa def error_page(err): # noqa
if not isinstance(err, HTTPException): if not isinstance(err, HTTPException):
abort(500) abort(500)
return render_template('error.html', error=err.code, msg=err.description), err.code return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
# Load login, registration and logout manager # Load login, registration and logout manager
from gallery import auth from gallery import auth
app.register_blueprint(auth.blueprint) app.register_blueprint(auth.blueprint)
# Load the API # Load the API
from gallery import api from gallery import api
app.register_blueprint(api.blueprint) app.register_blueprint(api.blueprint)
# Load the different views # Load the different views
from gallery.views import index, image, group, settings, profile from gallery.views import index, image, group, settings, profile
app.register_blueprint(index.blueprint) app.register_blueprint(index.blueprint)
app.register_blueprint(image.blueprint) app.register_blueprint(image.blueprint)
app.register_blueprint(group.blueprint) app.register_blueprint(group.blueprint)
@ -129,7 +126,7 @@ def create_app(test_config=None):
app.register_blueprint(settings.blueprint) app.register_blueprint(settings.blueprint)
# Log to file that the app has started # Log to file that the app has started
logging.info('Gallery started successfully!') logging.info("Gallery started successfully!")
# Initialize extensions and return app # Initialize extensions and return app
assets.init_app(app) assets.init_app(app)

View file

@ -21,26 +21,28 @@ from gallery.utils import metadata as mt
from gallery.utils.generate_image import generate_thumbnail from gallery.utils.generate_image import generate_thumbnail
blueprint = Blueprint('api', __name__, url_prefix='/api') blueprint = Blueprint("api", __name__, url_prefix="/api")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/file/<file_name>', methods=['GET']) @blueprint.route("/file/<file_name>", methods=["GET"])
def file(file_name): def file(file_name):
""" """
Returns a file from the uploads folder Returns a file from the uploads folder
r for resolution, 400x400 or thumb for thumbnail r for resolution, 400x400 or thumb for thumbnail
""" """
res = request.args.get('r', default=None, type=str) # Type of file (thumb, etc) res = request.args.get("r", default=None, type=str) # Type of file (thumb, etc)
ext = request.args.get('e', default=None, type=str) # File extension ext = request.args.get("e", default=None, type=str) # File extension
file_name = secure_filename(file_name) # Sanitize file name file_name = secure_filename(file_name) # Sanitize file name
# if no args are passed, return the raw file # if no args are passed, return the raw file
if not res and not ext: if not res and not ext:
if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)): if not os.path.exists(
os.path.join(current_app.config["UPLOAD_FOLDER"], file_name)
):
abort(404) abort(404)
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name) return send_from_directory(current_app.config["UPLOAD_FOLDER"], file_name)
thumb = generate_thumbnail(file_name, res, ext) thumb = generate_thumbnail(file_name, res, ext)
if not thumb: if not thumb:
@ -91,13 +93,13 @@ def file(file_name):
# return jsonify(image_list) # return jsonify(image_list)
@blueprint.route('/upload', methods=['POST']) @blueprint.route("/upload", methods=["POST"])
@login_required @login_required
def upload(): def upload():
""" """
Uploads an image to the server and saves it to the database Uploads an image to the server and saves it to the database
""" """
form_file = request.files['file'] form_file = request.files["file"]
form = request.form form = request.form
# If no image is uploaded, return 404 error # If no image is uploaded, return 404 error
@ -105,41 +107,45 @@ def upload():
return abort(404) return abort(404)
# Get file extension, generate random name and set file path # Get file extension, generate random name and set file path
img_ext = pathlib.Path(form_file.filename).suffix.replace('.', '').lower() img_ext = pathlib.Path(form_file.filename).suffix.replace(".", "").lower()
img_name = "GWAGWA_" + str(uuid4()) 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 # Check if file extension is allowed
if img_ext not in current_app.config['ALLOWED_EXTENSIONS'].keys(): if img_ext not in current_app.config["ALLOWED_EXTENSIONS"].keys():
logging.info('File extension not allowed: %s', img_ext) logging.info("File extension not allowed: %s", img_ext)
abort(403) abort(403)
# Save file # Save file
try: try:
form_file.save(img_path) form_file.save(img_path)
except OSError as err: except OSError as err:
logging.info('Error saving file %s because of %s', img_path, err) logging.info("Error saving file %s because of %s", img_path, err)
abort(500) abort(500)
img_exif = mt.Metadata(img_path).yoink() # Get EXIF data img_exif = mt.Metadata(img_path).yoink() # Get EXIF data
img_colors = ColorThief(img_path).get_palette(color_count=3) # Get color palette img_colors = ColorThief(img_path).get_palette(color_count=3) # Get color palette
# Save to database # Save to database
query = db.Posts(author_id=current_user.id, query = db.Posts(
filename=img_name + '.' + img_ext, author_id=current_user.id,
filename=img_name + "." + img_ext,
mimetype=img_ext, mimetype=img_ext,
exif=img_exif, exif=img_exif,
colours=img_colors, colours=img_colors,
description=form['description'], description=form["description"],
alt=form['alt']) alt=form["alt"],
)
db_session.add(query) db_session.add(query)
db_session.commit() db_session.commit()
return 'Gwa Gwa' # Return something so the browser doesn't show an error return "Gwa Gwa" # Return something so the browser doesn't show an error
@blueprint.route('/delete/<int:image_id>', methods=['POST']) @blueprint.route("/delete/<int:image_id>", methods=["POST"])
@login_required @login_required
def delete_image(image_id): def delete_image(image_id):
""" """
@ -155,14 +161,16 @@ def delete_image(image_id):
# Delete file # Delete file
try: try:
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], img.filename)) os.remove(os.path.join(current_app.config["UPLOAD_FOLDER"], img.filename))
except FileNotFoundError: except FileNotFoundError:
logging.warning('File not found: %s, already deleted or never existed', img.filename) logging.warning(
"File not found: %s, already deleted or never existed", img.filename
)
# Delete cached files # Delete cached files
cache_path = os.path.join(platformdirs.user_config_dir('onlylegs'), 'cache') cache_path = os.path.join(platformdirs.user_config_dir("onlylegs"), "cache")
cache_name = img.filename.rsplit('.')[0] cache_name = img.filename.rsplit(".")[0]
for cache_file in pathlib.Path(cache_path).glob(cache_name + '*'): for cache_file in pathlib.Path(cache_path).glob(cache_name + "*"):
os.remove(cache_file) os.remove(cache_file)
# Delete from database # Delete from database
@ -176,36 +184,38 @@ def delete_image(image_id):
# Commit all changes # Commit all changes
db_session.commit() db_session.commit()
logging.info('Removed image (%s) %s', image_id, img.filename) logging.info("Removed image (%s) %s", image_id, img.filename)
flash(['Image was all in Le Head!', '1']) flash(["Image was all in Le Head!", "1"])
return 'Gwa Gwa' return "Gwa Gwa"
@blueprint.route('/group/create', methods=['POST']) @blueprint.route("/group/create", methods=["POST"])
@login_required @login_required
def create_group(): def create_group():
""" """
Creates a group Creates a group
""" """
new_group = db.Groups(name=request.form['name'], new_group = db.Groups(
description=request.form['description'], name=request.form["name"],
author_id=current_user.id) description=request.form["description"],
author_id=current_user.id,
)
db_session.add(new_group) db_session.add(new_group)
db_session.commit() db_session.commit()
return ':3' return ":3"
@blueprint.route('/group/modify', methods=['POST']) @blueprint.route("/group/modify", methods=["POST"])
@login_required @login_required
def modify_group(): def modify_group():
""" """
Changes the images in a group Changes the images in a group
""" """
group_id = request.form['group'] group_id = request.form["group"]
image_id = request.form['image'] image_id = request.form["image"]
action = request.form['action'] action = request.form["action"]
group = db_session.query(db.Groups).filter_by(id=group_id).first() group = db_session.query(db.Groups).filter_by(id=group_id).first()
@ -214,28 +224,31 @@ def modify_group():
elif group.author_id != current_user.id: elif group.author_id != current_user.id:
abort(403) abort(403)
if action == 'add': if action == "add":
if not (db_session.query(db.GroupJunction) if not (
db_session.query(db.GroupJunction)
.filter_by(group_id=group_id, post_id=image_id) .filter_by(group_id=group_id, post_id=image_id)
.first()): .first()
db_session.add(db.GroupJunction(group_id=group_id, ):
post_id=image_id)) db_session.add(db.GroupJunction(group_id=group_id, post_id=image_id))
elif request.form['action'] == 'remove': elif request.form["action"] == "remove":
(db_session.query(db.GroupJunction) (
db_session.query(db.GroupJunction)
.filter_by(group_id=group_id, post_id=image_id) .filter_by(group_id=group_id, post_id=image_id)
.delete()) .delete()
)
db_session.commit() db_session.commit()
return ':3' return ":3"
@blueprint.route('/group/delete', methods=['POST']) @blueprint.route("/group/delete", methods=["POST"])
def delete_group(): def delete_group():
""" """
Deletes a group Deletes a group
""" """
group_id = request.form['group'] group_id = request.form["group"]
group = db_session.query(db.Groups).filter_by(id=group_id).first() group = db_session.query(db.Groups).filter_by(id=group_id).first()
@ -248,5 +261,5 @@ def delete_group():
db_session.query(db.GroupJunction).filter_by(group_id=group_id).delete() db_session.query(db.GroupJunction).filter_by(group_id=group_id).delete()
db_session.commit() db_session.commit()
flash(['Group yeeted!', '1']) flash(["Group yeeted!", "1"])
return ':3' return ":3"

View file

@ -14,39 +14,39 @@ from sqlalchemy.orm import sessionmaker
from gallery import db from gallery import db
blueprint = Blueprint('auth', __name__, url_prefix='/auth') blueprint = Blueprint("auth", __name__, url_prefix="/auth")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/login', methods=['POST']) @blueprint.route("/login", methods=["POST"])
def login(): def login():
""" """
Log in a registered user by adding the user id to the session Log in a registered user by adding the user id to the session
""" """
error = [] error = []
username = request.form['username'].strip() username = request.form["username"].strip()
password = request.form['password'].strip() password = request.form["password"].strip()
remember = bool(request.form['remember-me']) remember = bool(request.form["remember-me"])
user = db_session.query(db.Users).filter_by(username=username).first() user = db_session.query(db.Users).filter_by(username=username).first()
if not user or not check_password_hash(user.password, password): if not user or not check_password_hash(user.password, password):
logging.error('Login attempt from %s', request.remote_addr) logging.error("Login attempt from %s", request.remote_addr)
error.append('Username or Password is incorrect!') error.append("Username or Password is incorrect!")
if error: if error:
abort(403) abort(403)
login_user(user, remember=remember) login_user(user, remember=remember)
logging.info('User %s logged in from %s', username, request.remote_addr) logging.info("User %s logged in from %s", username, request.remote_addr)
flash(['Logged in successfully!', '4']) flash(["Logged in successfully!", "4"])
return 'ok', 200 return "ok", 200
@blueprint.route('/register', methods=['POST']) @blueprint.route("/register", methods=["POST"])
def register(): def register():
""" """
Register a new user Register a new user
@ -54,55 +54,58 @@ def register():
error = [] error = []
# Thanks Fennec for reminding me to strip out the whitespace lol # Thanks Fennec for reminding me to strip out the whitespace lol
username = request.form['username'].strip() username = request.form["username"].strip()
email = request.form['email'].strip() email = request.form["email"].strip()
password = request.form['password'].strip() password = request.form["password"].strip()
password_repeat = request.form['password-repeat'].strip() password_repeat = request.form["password-repeat"].strip()
email_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') email_regex = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
username_regex = re.compile(r'\b[A-Za-z0-9._-]+\b') username_regex = re.compile(r"\b[A-Za-z0-9._-]+\b")
# 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 invalid!') error.append("Username is invalid!")
if not email or not email_regex.match(email): if not email or not email_regex.match(email):
error.append('Email is invalid!') error.append("Email is invalid!")
if not password: if not password:
error.append('Password is empty!') error.append("Password is empty!")
elif len(password) < 8: elif len(password) < 8:
error.append('Password is too short! Longer than 8 characters pls') error.append("Password is too short! Longer than 8 characters pls")
if not password_repeat: if not password_repeat:
error.append('Enter password again!') error.append("Enter password again!")
elif password_repeat != password: elif password_repeat != password:
error.append('Passwords do not match!') error.append("Passwords do not match!")
user_exists = db_session.query(db.Users).filter_by(username=username).first() user_exists = db_session.query(db.Users).filter_by(username=username).first()
if user_exists: if user_exists:
error.append('User already exists!') error.append("User already exists!")
# If there are errors, return them # If there are errors, return them
if error: if error:
print(error) print(error)
return jsonify(error), 400 return jsonify(error), 400
register_user = db.Users(username=username, email=email, register_user = db.Users(
password=generate_password_hash(password, method='sha256')) username=username,
email=email,
password=generate_password_hash(password, method="sha256"),
)
db_session.add(register_user) db_session.add(register_user)
db_session.commit() db_session.commit()
logging.info('User %s registered', username) logging.info("User %s registered", username)
return 'ok', 200 return "ok", 200
@blueprint.route('/logout') @blueprint.route("/logout")
@login_required @login_required
def logout(): def logout():
""" """
Clear the current session, including the stored user id Clear the current session, including the stored user id
""" """
logout_user() logout_user()
flash(['Goodbye!!!', '4']) flash(["Goodbye!!!", "4"])
return redirect(url_for('gallery.index')) return redirect(url_for("gallery.index"))

View file

@ -5,27 +5,36 @@ from uuid import uuid4
import os import os
import platformdirs import platformdirs
from sqlalchemy import (create_engine, Column, Integer, String, from sqlalchemy import (
DateTime, ForeignKey, PickleType, func) create_engine,
Column,
Integer,
String,
DateTime,
ForeignKey,
PickleType,
func,
)
from sqlalchemy.orm import declarative_base, relationship from sqlalchemy.orm import declarative_base, relationship
from flask_login import UserMixin from flask_login import UserMixin
USER_DIR = platformdirs.user_config_dir('onlylegs') USER_DIR = platformdirs.user_config_dir("onlylegs")
DB_PATH = os.path.join(USER_DIR, 'instance', 'gallery.sqlite3') DB_PATH = os.path.join(USER_DIR, "instance", "gallery.sqlite3")
# In the future, I want to add support for other databases # In the future, I want to add support for other databases
engine = create_engine(f'sqlite:///{DB_PATH}', echo=False) engine = create_engine(f"sqlite:///{DB_PATH}", echo=False)
base = declarative_base() base = declarative_base()
class Users (base, UserMixin): # pylint: disable=too-few-public-methods, C0103 class Users(base, UserMixin): # pylint: disable=too-few-public-methods, C0103
""" """
User table User table
Joins with post, groups, session and log Joins with post, groups, session and log
""" """
__tablename__ = 'users'
__tablename__ = "users"
# Gallery used information # Gallery used information
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@ -34,26 +43,31 @@ class Users (base, UserMixin): # pylint: disable=too-few-public-methods, C0103
username = Column(String, unique=True, nullable=False) username = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False) email = Column(String, unique=True, nullable=False)
password = Column(String, nullable=False) password = Column(String, nullable=False)
joined_at = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 joined_at = Column(
DateTime, nullable=False, server_default=func.now()
) # pylint: disable=E1102
posts = relationship('Posts', backref='users') posts = relationship("Posts", backref="users")
groups = relationship('Groups', backref='users') groups = relationship("Groups", backref="users")
log = relationship('Logs', backref='users') log = relationship("Logs", backref="users")
def get_id(self): def get_id(self):
return str(self.alt_id) return str(self.alt_id)
class Posts (base): # pylint: disable=too-few-public-methods, C0103 class Posts(base): # pylint: disable=too-few-public-methods, C0103
""" """
Post table Post table
Joins with group_junction Joins with group_junction
""" """
__tablename__ = 'posts'
__tablename__ = "posts"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
author_id = Column(Integer, ForeignKey('users.id')) author_id = Column(Integer, ForeignKey("users.id"))
created_at = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 created_at = Column(
DateTime, nullable=False, server_default=func.now()
) # pylint: disable=E1102
filename = Column(String, unique=True, nullable=False) filename = Column(String, unique=True, nullable=False)
mimetype = Column(String, nullable=False) mimetype = Column(String, nullable=False)
exif = Column(PickleType, nullable=False) exif = Column(PickleType, nullable=False)
@ -61,66 +75,79 @@ class Posts (base): # pylint: disable=too-few-public-methods, C0103
description = Column(String, nullable=False) description = Column(String, nullable=False)
alt = Column(String, nullable=False) alt = Column(String, nullable=False)
junction = relationship('GroupJunction', backref='posts') junction = relationship("GroupJunction", backref="posts")
class Groups (base): # pylint: disable=too-few-public-methods, C0103
class Groups(base): # pylint: disable=too-few-public-methods, C0103
""" """
Group table Group table
Joins with group_junction Joins with group_junction
""" """
__tablename__ = 'groups'
__tablename__ = "groups"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
name = Column(String, nullable=False) name = Column(String, nullable=False)
description = Column(String, nullable=False) description = Column(String, nullable=False)
author_id = Column(Integer, ForeignKey('users.id')) author_id = Column(Integer, ForeignKey("users.id"))
created_at = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 created_at = Column(
DateTime, nullable=False, server_default=func.now()
) # pylint: disable=E1102
junction = relationship('GroupJunction', backref='groups') junction = relationship("GroupJunction", backref="groups")
class GroupJunction (base): # pylint: disable=too-few-public-methods, C0103 class GroupJunction(base): # pylint: disable=too-few-public-methods, C0103
""" """
Junction table for posts and groups Junction table for posts and groups
Joins with posts and groups Joins with posts and groups
""" """
__tablename__ = 'group_junction'
__tablename__ = "group_junction"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
date_added = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 date_added = Column(
group_id = Column(Integer, ForeignKey('groups.id')) DateTime, nullable=False, server_default=func.now()
post_id = Column(Integer, ForeignKey('posts.id')) ) # pylint: disable=E1102
group_id = Column(Integer, ForeignKey("groups.id"))
post_id = Column(Integer, ForeignKey("posts.id"))
class Logs (base): # pylint: disable=too-few-public-methods, C0103 class Logs(base): # pylint: disable=too-few-public-methods, C0103
""" """
Log table Log table
Joins with user Joins with user
""" """
__tablename__ = 'logs'
__tablename__ = "logs"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id')) user_id = Column(Integer, ForeignKey("users.id"))
ip_address = Column(String, nullable=False) ip_address = Column(String, nullable=False)
code = Column(Integer, nullable=False) code = Column(Integer, nullable=False)
note = Column(String, nullable=False) note = Column(String, nullable=False)
created_at = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 created_at = Column(
DateTime, nullable=False, server_default=func.now()
) # pylint: disable=E1102
class Bans (base): # pylint: disable=too-few-public-methods, C0103 class Bans(base): # pylint: disable=too-few-public-methods, C0103
""" """
Bans table Bans table
""" """
__tablename__ = 'bans'
__tablename__ = "bans"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
ip_address = Column(String, nullable=False) ip_address = Column(String, nullable=False)
code = Column(Integer, nullable=False) code = Column(Integer, nullable=False)
note = Column(String, nullable=False) note = Column(String, nullable=False)
banned_at = Column(DateTime, nullable=False, server_default=func.now()) # pylint: disable=E1102 banned_at = Column(
DateTime, nullable=False, server_default=func.now()
) # pylint: disable=E1102
# check if database file exists, if not create it # check if database file exists, if not create it
if not os.path.isfile(DB_PATH): if not os.path.isfile(DB_PATH):
base.metadata.create_all(engine) base.metadata.create_all(engine)
print('Database created') print("Database created")

View file

@ -16,7 +16,10 @@ def contrast(background, light, dark, threshold=0.179):
# Calculate contrast # Calculate contrast
uicolors = [red / 255, green / 255, blue / 255] uicolors = [red / 255, green / 255, blue / 255]
cont = [col / 12.92 if col <= 0.03928 else ((col + 0.055) / 1.055) ** 2.4 for col in uicolors] cont = [
col / 12.92 if col <= 0.03928 else ((col + 0.055) / 1.055) ** 2.4
for col in uicolors
]
lightness = (0.2126 * cont[0]) + (0.7152 * cont[1]) + (0.0722 * cont[2]) lightness = (0.2126 * cont[0]) + (0.7152 * cont[1]) + (0.0722 * cont[2])
return light if lightness > threshold else dark return light if lightness > threshold else dark

View file

@ -8,8 +8,8 @@ from PIL import Image, ImageOps
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
CACHE_PATH = platformdirs.user_config_dir('onlylegs') + '/cache' CACHE_PATH = os.path.join(platformdirs.user_config_dir("onlylegs"), "cache")
UPLOAD_PATH = platformdirs.user_config_dir('onlylegs') + '/uploads' UPLOAD_PATH = os.path.join(platformdirs.user_config_dir("onlylegs"), "uploads")
def generate_thumbnail(file_name, resolution, ext=None): def generate_thumbnail(file_name, resolution, ext=None):
@ -25,34 +25,34 @@ def generate_thumbnail(file_name, resolution, ext=None):
os.makedirs(CACHE_PATH) os.makedirs(CACHE_PATH)
# no sussy business # no sussy business
file_name, file_ext = secure_filename(file_name).rsplit('.') file_name, file_ext = secure_filename(file_name).rsplit(".")
if not ext: if not ext:
ext = file_ext.strip('.') ext = file_ext.strip(".")
# PIL doesnt like jpg so we convert it to jpeg # PIL doesnt like jpg so we convert it to jpeg
if ext.lower() == "jpg": if ext.lower() == "jpg":
ext = "jpeg" ext = "jpeg"
# Set resolution based on preset resolutions # Set resolution based on preset resolutions
if resolution in ['prev', 'preview']: if resolution in ["prev", "preview"]:
res_x, res_y = (1920, 1080) res_x, res_y = (1920, 1080)
elif resolution in ['thumb', 'thumbnail']: elif resolution in ["thumb", "thumbnail"]:
res_x, res_y = (400, 400) res_x, res_y = (400, 400)
elif resolution in ['icon', 'favicon']: elif resolution in ["icon", "favicon"]:
res_x, res_y = (10, 10) res_x, res_y = (10, 10)
else: else:
return None return None
# If image has been already generated, return it from the cache # If image has been already generated, return it from the cache
if os.path.exists(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}')): if os.path.exists(os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}")):
return os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}') return os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}")
# Check if image exists in the uploads directory # Check if image exists in the uploads directory
if not os.path.exists(os.path.join(UPLOAD_PATH, f'{file_name}.{file_ext}')): if not os.path.exists(os.path.join(UPLOAD_PATH, f"{file_name}.{file_ext}")):
return None return None
# Open image and rotate it based on EXIF data and get ICC profile so colors are correct # Open image and rotate it based on EXIF data and get ICC profile so colors are correct
image = Image.open(os.path.join(UPLOAD_PATH, f'{file_name}.{file_ext}')) image = Image.open(os.path.join(UPLOAD_PATH, f"{file_name}.{file_ext}"))
image_icc = image.info.get("icc_profile") image_icc = image.info.get("icc_profile")
img_x, img_y = image.size img_x, img_y = image.size
@ -62,16 +62,20 @@ def generate_thumbnail(file_name, resolution, ext=None):
# Save image to cache directory # Save image to cache directory
try: try:
image.save(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}'), image.save(
icc_profile=image_icc) os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}"),
icc_profile=image_icc,
)
except OSError: except OSError:
# This usually happens when saving a JPEG with an ICC profile, # This usually happens when saving a JPEG with an ICC profile,
# so we convert to RGB and try again # so we convert to RGB and try again
image = image.convert('RGB') image = image.convert("RGB")
image.save(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}'), image.save(
icc_profile=image_icc) os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}"),
icc_profile=image_icc,
)
# No need to keep the image in memory, learned the hard way # No need to keep the image in memory, learned the hard way
image.close() image.close()
return os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}') return os.path.join(CACHE_PATH, f"{file_name}_{res_x}x{res_y}.{ext}")

View file

@ -16,6 +16,7 @@ class Metadata:
""" """
Metadata parser Metadata parser
""" """
def __init__(self, file_path): def __init__(self, file_path):
""" """
Initialize the metadata parser Initialize the metadata parser
@ -32,17 +33,17 @@ class Metadata:
if tag in tags: if tag in tags:
img_exif[value] = tags[tag] img_exif[value] = tags[tag]
img_exif['FileName'] = os.path.basename(file_path) img_exif["FileName"] = os.path.basename(file_path)
img_exif['FileSize'] = os.path.getsize(file_path) img_exif["FileSize"] = os.path.getsize(file_path)
img_exif['FileFormat'] = img_exif['FileName'].split('.')[-1] img_exif["FileFormat"] = img_exif["FileName"].split(".")[-1]
img_exif['FileWidth'], img_exif['FileHeight'] = file.size img_exif["FileWidth"], img_exif["FileHeight"] = file.size
file.close() file.close()
except TypeError: except TypeError:
img_exif['FileName'] = os.path.basename(file_path) img_exif["FileName"] = os.path.basename(file_path)
img_exif['FileSize'] = os.path.getsize(file_path) img_exif["FileSize"] = os.path.getsize(file_path)
img_exif['FileFormat'] = img_exif['FileName'].split('.')[-1] img_exif["FileFormat"] = img_exif["FileName"].split(".")[-1]
img_exif['FileWidth'], img_exif['FileHeight'] = file.size img_exif["FileWidth"], img_exif["FileHeight"] = file.size
self.encoded = img_exif self.encoded = img_exif
@ -60,10 +61,10 @@ class Metadata:
Formats the data into a dictionary Formats the data into a dictionary
""" """
exif = { exif = {
'Photographer': {}, "Photographer": {},
'Camera': {}, "Camera": {},
'Software': {}, "Software": {},
'File': {}, "File": {},
} }
# Thanks chatGPT xP # Thanks chatGPT xP
@ -73,26 +74,29 @@ class Metadata:
if len(mapping_val[key]) == 2: if len(mapping_val[key]) == 2:
# the helper function works, so not sure why it triggers pylint # the helper function works, so not sure why it triggers pylint
exif[mapping_name][mapping_val[key][0]] = { exif[mapping_name][mapping_val[key][0]] = {
'raw': value, "raw": value,
'formatted': ( "formatted": (
getattr(helpers, mapping_val[key][1]) # pylint: disable=E0602 getattr(
(value) helpers, mapping_val[key][1]
)( # pylint: disable=E0602
value
)
), ),
} }
else: else:
exif[mapping_name][mapping_val[key][0]] = { exif[mapping_name][mapping_val[key][0]] = {
'raw': value, "raw": value,
} }
continue continue
# Remove empty keys # Remove empty keys
if not exif['Photographer']: if not exif["Photographer"]:
del exif['Photographer'] del exif["Photographer"]
if not exif['Camera']: if not exif["Camera"]:
del exif['Camera'] del exif["Camera"]
if not exif['Software']: if not exif["Software"]:
del exif['Software'] del exif["Software"]
if not exif['File']: if not exif["File"]:
del exif['File'] del exif["File"]
return exif return exif

View file

@ -21,28 +21,28 @@ def date_format(value):
""" """
Formats the date into a standard format Formats the date into a standard format
""" """
return str(datetime.strptime(value, '%Y:%m:%d %H:%M:%S')) return str(datetime.strptime(value, "%Y:%m:%d %H:%M:%S"))
def fnumber(value): def fnumber(value):
""" """
Formats the f-number into a standard format Formats the f-number into a standard format
""" """
return 'ƒ/' + str(value) return "ƒ/" + str(value)
def iso(value): def iso(value):
""" """
Formats the ISO into a standard format Formats the ISO into a standard format
""" """
return 'ISO ' + str(value) return "ISO " + str(value)
def shutter(value): def shutter(value):
""" """
Formats the shutter speed into a standard format Formats the shutter speed into a standard format
""" """
return str(value) + 's' return str(value) + "s"
def focal_length(value): def focal_length(value):
@ -50,27 +50,23 @@ def focal_length(value):
Formats the focal length into a standard format Formats the focal length into a standard format
""" """
try: try:
return str(value[0] / value[1]) + 'mm' return str(value[0] / value[1]) + "mm"
except TypeError: except TypeError:
return str(value) + 'mm' return str(value) + "mm"
def exposure(value): def exposure(value):
""" """
Formats the exposure value into a standard format Formats the exposure value into a standard format
""" """
return str(value) + 'EV' return str(value) + "EV"
def color_space(value): def color_space(value):
""" """
Maps the value of the color space to a human readable format Maps the value of the color space to a human readable format
""" """
value_map = { value_map = {0: "Reserved", 1: "sRGB", 65535: "Uncalibrated"}
0: 'Reserved',
1: 'sRGB',
65535: 'Uncalibrated'
}
try: try:
return value_map[int(value)] return value_map[int(value)]
except KeyError: except KeyError:
@ -82,28 +78,28 @@ def flash(value):
Maps the value of the flash to a human readable format Maps the value of the flash to a human readable format
""" """
value_map = { value_map = {
0: 'Flash did not fire', 0: "Flash did not fire",
1: 'Flash fired', 1: "Flash fired",
5: 'Strobe return light not detected', 5: "Strobe return light not detected",
7: 'Strobe return light detected', 7: "Strobe return light detected",
9: 'Flash fired, compulsory flash mode', 9: "Flash fired, compulsory flash mode",
13: 'Flash fired, compulsory flash mode, return light not detected', 13: "Flash fired, compulsory flash mode, return light not detected",
15: 'Flash fired, compulsory flash mode, return light detected', 15: "Flash fired, compulsory flash mode, return light detected",
16: 'Flash did not fire, compulsory flash mode', 16: "Flash did not fire, compulsory flash mode",
24: 'Flash did not fire, auto mode', 24: "Flash did not fire, auto mode",
25: 'Flash fired, auto mode', 25: "Flash fired, auto mode",
29: 'Flash fired, auto mode, return light not detected', 29: "Flash fired, auto mode, return light not detected",
31: 'Flash fired, auto mode, return light detected', 31: "Flash fired, auto mode, return light detected",
32: 'No flash function', 32: "No flash function",
65: 'Flash fired, red-eye reduction mode', 65: "Flash fired, red-eye reduction mode",
69: 'Flash fired, red-eye reduction mode, return light not detected', 69: "Flash fired, red-eye reduction mode, return light not detected",
71: 'Flash fired, red-eye reduction mode, return light detected', 71: "Flash fired, red-eye reduction mode, return light detected",
73: 'Flash fired, compulsory flash mode, red-eye reduction mode', 73: "Flash fired, compulsory flash mode, red-eye reduction mode",
77: 'Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected', 77: "Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected",
79: 'Flash fired, compulsory flash mode, red-eye reduction mode, return light detected', 79: "Flash fired, compulsory flash mode, red-eye reduction mode, return light detected",
89: 'Flash fired, auto mode, red-eye reduction mode', 89: "Flash fired, auto mode, red-eye reduction mode",
93: 'Flash fired, auto mode, return light not detected, red-eye reduction mode', 93: "Flash fired, auto mode, return light not detected, red-eye reduction mode",
95: 'Flash fired, auto mode, return light detected, red-eye reduction mode' 95: "Flash fired, auto mode, return light detected, red-eye reduction mode",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -116,15 +112,15 @@ def exposure_program(value):
Maps the value of the exposure program to a human readable format Maps the value of the exposure program to a human readable format
""" """
value_map = { value_map = {
0: 'Not defined', 0: "Not defined",
1: 'Manual', 1: "Manual",
2: 'Normal program', 2: "Normal program",
3: 'Aperture priority', 3: "Aperture priority",
4: 'Shutter priority', 4: "Shutter priority",
5: 'Creative program', 5: "Creative program",
6: 'Action program', 6: "Action program",
7: 'Portrait mode', 7: "Portrait mode",
8: 'Landscape mode' 8: "Landscape mode",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -137,14 +133,14 @@ def metering_mode(value):
Maps the value of the metering mode to a human readable format Maps the value of the metering mode to a human readable format
""" """
value_map = { value_map = {
0: 'Unknown', 0: "Unknown",
1: 'Average', 1: "Average",
2: 'Center-Weighted Average', 2: "Center-Weighted Average",
3: 'Spot', 3: "Spot",
4: 'Multi-Spot', 4: "Multi-Spot",
5: 'Pattern', 5: "Pattern",
6: 'Partial', 6: "Partial",
255: 'Other' 255: "Other",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -156,11 +152,7 @@ def resolution_unit(value):
""" """
Maps the value of the resolution unit to a human readable format Maps the value of the resolution unit to a human readable format
""" """
value_map = { value_map = {1: "No absolute unit of measurement", 2: "Inch", 3: "Centimeter"}
1: 'No absolute unit of measurement',
2: 'Inch',
3: 'Centimeter'
}
try: try:
return value_map[int(value)] return value_map[int(value)]
except KeyError: except KeyError:
@ -172,27 +164,27 @@ def light_source(value):
Maps the value of the light source to a human readable format Maps the value of the light source to a human readable format
""" """
value_map = { value_map = {
0: 'Unknown', 0: "Unknown",
1: 'Daylight', 1: "Daylight",
2: 'Fluorescent', 2: "Fluorescent",
3: 'Tungsten (incandescent light)', 3: "Tungsten (incandescent light)",
4: 'Flash', 4: "Flash",
9: 'Fine weather', 9: "Fine weather",
10: 'Cloudy weather', 10: "Cloudy weather",
11: 'Shade', 11: "Shade",
12: 'Daylight fluorescent (D 5700 - 7100K)', 12: "Daylight fluorescent (D 5700 - 7100K)",
13: 'Day white fluorescent (N 4600 - 5400K)', 13: "Day white fluorescent (N 4600 - 5400K)",
14: 'Cool white fluorescent (W 3900 - 4500K)', 14: "Cool white fluorescent (W 3900 - 4500K)",
15: 'White fluorescent (WW 3200 - 3700K)', 15: "White fluorescent (WW 3200 - 3700K)",
17: 'Standard light A', 17: "Standard light A",
18: 'Standard light B', 18: "Standard light B",
19: 'Standard light C', 19: "Standard light C",
20: 'D55', 20: "D55",
21: 'D65', 21: "D65",
22: 'D75', 22: "D75",
23: 'D50', 23: "D50",
24: 'ISO studio tungsten', 24: "ISO studio tungsten",
255: 'Other light source', 255: "Other light source",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -205,10 +197,10 @@ def scene_capture_type(value):
Maps the value of the scene capture type to a human readable format Maps the value of the scene capture type to a human readable format
""" """
value_map = { value_map = {
0: 'Standard', 0: "Standard",
1: 'Landscape', 1: "Landscape",
2: 'Portrait', 2: "Portrait",
3: 'Night scene', 3: "Night scene",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -221,8 +213,8 @@ def white_balance(value):
Maps the value of the white balance to a human readable format Maps the value of the white balance to a human readable format
""" """
value_map = { value_map = {
0: 'Auto white balance', 0: "Auto white balance",
1: 'Manual white balance', 1: "Manual white balance",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -235,9 +227,9 @@ def exposure_mode(value):
Maps the value of the exposure mode to a human readable format Maps the value of the exposure mode to a human readable format
""" """
value_map = { value_map = {
0: 'Auto exposure', 0: "Auto exposure",
1: 'Manual exposure', 1: "Manual exposure",
2: 'Auto bracket', 2: "Auto bracket",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -250,22 +242,14 @@ def sensitivity_type(value):
Maps the value of the sensitivity type to a human readable format Maps the value of the sensitivity type to a human readable format
""" """
value_map = { value_map = {
0: 0: "Unknown",
'Unknown', 1: "Standard Output Sensitivity",
1: 2: "Recommended Exposure Index",
'Standard Output Sensitivity', 3: "ISO Speed",
2: 4: "Standard Output Sensitivity and Recommended Exposure Index",
'Recommended Exposure Index', 5: "Standard Output Sensitivity and ISO Speed",
3: 6: "Recommended Exposure Index and ISO Speed",
'ISO Speed', 7: "Standard Output Sensitivity, Recommended Exposure Index and ISO Speed",
4:
'Standard Output Sensitivity and Recommended Exposure Index',
5:
'Standard Output Sensitivity and ISO Speed',
6:
'Recommended Exposure Index and ISO Speed',
7:
'Standard Output Sensitivity, Recommended Exposure Index and ISO Speed',
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -278,7 +262,7 @@ def lens_specification(value):
Maps the value of the lens specification to a human readable format Maps the value of the lens specification to a human readable format
""" """
try: try:
return str(value[0] / value[1]) + 'mm - ' + str(value[2] / value[3]) + 'mm' return str(value[0] / value[1]) + "mm - " + str(value[2] / value[3]) + "mm"
except TypeError: except TypeError:
return None return None
@ -288,55 +272,55 @@ def compression_type(value):
Maps the value of the compression type to a human readable format Maps the value of the compression type to a human readable format
""" """
value_map = { value_map = {
1: 'Uncompressed', 1: "Uncompressed",
2: 'CCITT 1D', 2: "CCITT 1D",
3: 'T4/Group 3 Fax', 3: "T4/Group 3 Fax",
4: 'T6/Group 4 Fax', 4: "T6/Group 4 Fax",
5: 'LZW', 5: "LZW",
6: 'JPEG (old-style)', 6: "JPEG (old-style)",
7: 'JPEG', 7: "JPEG",
8: 'Adobe Deflate', 8: "Adobe Deflate",
9: 'JBIG B&W', 9: "JBIG B&W",
10: 'JBIG Color', 10: "JBIG Color",
99: 'JPEG', 99: "JPEG",
262: 'Kodak 262', 262: "Kodak 262",
32766: 'Next', 32766: "Next",
32767: 'Sony ARW Compressed', 32767: "Sony ARW Compressed",
32769: 'Packed RAW', 32769: "Packed RAW",
32770: 'Samsung SRW Compressed', 32770: "Samsung SRW Compressed",
32771: 'CCIRLEW', 32771: "CCIRLEW",
32772: 'Samsung SRW Compressed 2', 32772: "Samsung SRW Compressed 2",
32773: 'PackBits', 32773: "PackBits",
32809: 'Thunderscan', 32809: "Thunderscan",
32867: 'Kodak KDC Compressed', 32867: "Kodak KDC Compressed",
32895: 'IT8CTPAD', 32895: "IT8CTPAD",
32896: 'IT8LW', 32896: "IT8LW",
32897: 'IT8MP', 32897: "IT8MP",
32898: 'IT8BL', 32898: "IT8BL",
32908: 'PixarFilm', 32908: "PixarFilm",
32909: 'PixarLog', 32909: "PixarLog",
32946: 'Deflate', 32946: "Deflate",
32947: 'DCS', 32947: "DCS",
33003: 'Aperio JPEG 2000 YCbCr', 33003: "Aperio JPEG 2000 YCbCr",
33005: 'Aperio JPEG 2000 RGB', 33005: "Aperio JPEG 2000 RGB",
34661: 'JBIG', 34661: "JBIG",
34676: 'SGILog', 34676: "SGILog",
34677: 'SGILog24', 34677: "SGILog24",
34712: 'JPEG 2000', 34712: "JPEG 2000",
34713: 'Nikon NEF Compressed', 34713: "Nikon NEF Compressed",
34715: 'JBIG2 TIFF FX', 34715: "JBIG2 TIFF FX",
34718: '(MDI) Binary Level Codec', 34718: "(MDI) Binary Level Codec",
34719: '(MDI) Progressive Transform Codec', 34719: "(MDI) Progressive Transform Codec",
34720: '(MDI) Vector', 34720: "(MDI) Vector",
34887: 'ESRI Lerc', 34887: "ESRI Lerc",
34892: 'Lossy JPEG', 34892: "Lossy JPEG",
34925: 'LZMA2', 34925: "LZMA2",
34926: 'Zstd', 34926: "Zstd",
34927: 'WebP', 34927: "WebP",
34933: 'PNG', 34933: "PNG",
34934: 'JPEG XR', 34934: "JPEG XR",
65000: 'Kodak DCR Compressed', 65000: "Kodak DCR Compressed",
65535: 'Pentax PEF Compressed', 65535: "Pentax PEF Compressed",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -349,15 +333,15 @@ def orientation(value):
Maps the value of the orientation to a human readable format Maps the value of the orientation to a human readable format
""" """
value_map = { value_map = {
0: 'Undefined', 0: "Undefined",
1: 'Horizontal (normal)', 1: "Horizontal (normal)",
2: 'Mirror horizontal', 2: "Mirror horizontal",
3: 'Rotate 180', 3: "Rotate 180",
4: 'Mirror vertical', 4: "Mirror vertical",
5: 'Mirror horizontal and rotate 270 CW', 5: "Mirror horizontal and rotate 270 CW",
6: 'Rotate 90 CW', 6: "Rotate 90 CW",
7: 'Mirror horizontal and rotate 90 CW', 7: "Mirror horizontal and rotate 90 CW",
8: 'Rotate 270 CW', 8: "Rotate 270 CW",
} }
try: try:
return value_map[int(value)] return value_map[int(value)]
@ -370,16 +354,16 @@ def components_configuration(value):
Maps the value of the components configuration to a human readable format Maps the value of the components configuration to a human readable format
""" """
value_map = { value_map = {
0: '', 0: "",
1: 'Y', 1: "Y",
2: 'Cb', 2: "Cb",
3: 'Cr', 3: "Cr",
4: 'R', 4: "R",
5: 'G', 5: "G",
6: 'B', 6: "B",
} }
try: try:
return ''.join([value_map[int(x)] for x in value]) return "".join([value_map[int(x)] for x in value])
except KeyError: except KeyError:
return None return None
@ -388,18 +372,18 @@ def rating(value):
""" """
Maps the value of the rating to a human readable format Maps the value of the rating to a human readable format
""" """
return str(value) + ' stars' return str(value) + " stars"
def rating_percent(value): def rating_percent(value):
""" """
Maps the value of the rating to a human readable format Maps the value of the rating to a human readable format
""" """
return str(value) + '%' return str(value) + "%"
def pixel_dimension(value): def pixel_dimension(value):
""" """
Maps the value of the pixel dimension to a human readable format Maps the value of the pixel dimension to a human readable format
""" """
return str(value) + 'px' return str(value) + "px"

View file

@ -4,66 +4,66 @@ Mapping for metadata
""" """
PHOTOGRAHER_MAPPING = { PHOTOGRAHER_MAPPING = {
'Artist': ['Artist'], "Artist": ["Artist"],
'UserComment': ['Comment'], "UserComment": ["Comment"],
'ImageDescription': ['Description'], "ImageDescription": ["Description"],
'Copyright': ['Copyright'], "Copyright": ["Copyright"],
} }
CAMERA_MAPPING = { CAMERA_MAPPING = {
'Model': ['Model'], "Model": ["Model"],
'Make': ['Make'], "Make": ["Make"],
'BodySerialNumber': ['Camera Type'], "BodySerialNumber": ["Camera Type"],
'LensMake': ['Lens Make'], "LensMake": ["Lens Make"],
'LenseModel': ['Lens Model'], "LenseModel": ["Lens Model"],
'LensSpecification': ['Lens Specification', 'lens_specification'], "LensSpecification": ["Lens Specification", "lens_specification"],
'ComponentsConfiguration': ['Components Configuration', 'components_configuration'], "ComponentsConfiguration": ["Components Configuration", "components_configuration"],
'DateTime': ['Date Processed', 'date_format'], "DateTime": ["Date Processed", "date_format"],
'DateTimeDigitized': ['Time Digitized', 'date_format'], "DateTimeDigitized": ["Time Digitized", "date_format"],
'OffsetTime': ['Time Offset'], "OffsetTime": ["Time Offset"],
'OffsetTimeOriginal': ['Time Offset - Original'], "OffsetTimeOriginal": ["Time Offset - Original"],
'OffsetTimeDigitized': ['Time Offset - Digitized'], "OffsetTimeDigitized": ["Time Offset - Digitized"],
'DateTimeOriginal': ['Date Original', 'date_format'], "DateTimeOriginal": ["Date Original", "date_format"],
'FNumber': ['F-Stop', 'fnumber'], "FNumber": ["F-Stop", "fnumber"],
'FocalLength': ['Focal Length', 'focal_length'], "FocalLength": ["Focal Length", "focal_length"],
'FocalLengthIn35mmFilm': ['Focal Length (35mm format)', 'focal_length'], "FocalLengthIn35mmFilm": ["Focal Length (35mm format)", "focal_length"],
'MaxApertureValue': ['Max Aperture', 'fnumber'], "MaxApertureValue": ["Max Aperture", "fnumber"],
'ApertureValue': ['Aperture', 'fnumber'], "ApertureValue": ["Aperture", "fnumber"],
'ShutterSpeedValue': ['Shutter Speed', 'shutter'], "ShutterSpeedValue": ["Shutter Speed", "shutter"],
'ISOSpeedRatings': ['ISO Speed Ratings', 'iso'], "ISOSpeedRatings": ["ISO Speed Ratings", "iso"],
'ISOSpeed': ['ISO Speed', 'iso'], "ISOSpeed": ["ISO Speed", "iso"],
'SensitivityType': ['Sensitivity Type', 'sensitivity_type'], "SensitivityType": ["Sensitivity Type", "sensitivity_type"],
'ExposureBiasValue': ['Exposure Bias', 'exposure'], "ExposureBiasValue": ["Exposure Bias", "exposure"],
'ExposureTime': ['Exposure Time', 'shutter'], "ExposureTime": ["Exposure Time", "shutter"],
'ExposureMode': ['Exposure Mode', 'exposure_mode'], "ExposureMode": ["Exposure Mode", "exposure_mode"],
'ExposureProgram': ['Exposure Program', 'exposure_program'], "ExposureProgram": ["Exposure Program", "exposure_program"],
'WhiteBalance': ['White Balance', 'white_balance'], "WhiteBalance": ["White Balance", "white_balance"],
'Flash': ['Flash', 'flash'], "Flash": ["Flash", "flash"],
'MeteringMode': ['Metering Mode', 'metering_mode'], "MeteringMode": ["Metering Mode", "metering_mode"],
'LightSource': ['Light Source', 'light_source'], "LightSource": ["Light Source", "light_source"],
'SceneCaptureType': ['Scene Capture Type', 'scene_capture_type'], "SceneCaptureType": ["Scene Capture Type", "scene_capture_type"],
} }
SOFTWARE_MAPPING = { SOFTWARE_MAPPING = {
'Software': ['Software'], "Software": ["Software"],
'ColorSpace': ['Colour Space', 'color_space'], "ColorSpace": ["Colour Space", "color_space"],
'Compression': ['Compression', 'compression_type'], "Compression": ["Compression", "compression_type"],
} }
FILE_MAPPING = { FILE_MAPPING = {
'FileName': ['Name'], "FileName": ["Name"],
'FileSize': ['Size', 'human_size'], "FileSize": ["Size", "human_size"],
'FileFormat': ['Format'], "FileFormat": ["Format"],
'FileWidth': ['Width', 'pixel_dimension'], "FileWidth": ["Width", "pixel_dimension"],
'FileHeight': ['Height', 'pixel_dimension'], "FileHeight": ["Height", "pixel_dimension"],
'Orientation': ['Orientation', 'orientation'], "Orientation": ["Orientation", "orientation"],
'XResolution': ['X-resolution'], "XResolution": ["X-resolution"],
'YResolution': ['Y-resolution'], "YResolution": ["Y-resolution"],
'ResolutionUnit': ['Resolution Units', 'resolution_unit'], "ResolutionUnit": ["Resolution Units", "resolution_unit"],
'Rating': ['Rating', 'rating'], "Rating": ["Rating", "rating"],
'RatingPercent': ['Rating Percent', 'rating_percent'], "RatingPercent": ["Rating Percent", "rating_percent"],
} }
EXIF_MAPPING = [ EXIF_MAPPING = [
('Photographer', PHOTOGRAHER_MAPPING), ("Photographer", PHOTOGRAHER_MAPPING),
('Camera', CAMERA_MAPPING), ("Camera", CAMERA_MAPPING),
('Software', SOFTWARE_MAPPING), ("Software", SOFTWARE_MAPPING),
('File', FILE_MAPPING) ("File", FILE_MAPPING),
] ]

View file

@ -10,12 +10,12 @@ from gallery import db
from gallery.utils import contrast from gallery.utils import contrast
blueprint = Blueprint('group', __name__, url_prefix='/group') blueprint = Blueprint("group", __name__, url_prefix="/group")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/', methods=['GET']) @blueprint.route("/", methods=["GET"])
def groups(): def groups():
""" """
Group overview, shows all image groups Group overview, shows all image groups
@ -24,116 +24,134 @@ def groups():
# For each group, get the 3 most recent images # For each group, get the 3 most recent images
for group in groups: for group in groups:
group.author_username = (db_session.query(db.Users.username) group.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id) .filter(db.Users.id == group.author_id)
.first()[0]) .first()[0]
)
# Get the 3 most recent images # Get the 3 most recent images
images = (db_session.query(db.GroupJunction.post_id) images = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group.id) .filter(db.GroupJunction.group_id == group.id)
.order_by(db.GroupJunction.date_added.desc()) .order_by(db.GroupJunction.date_added.desc())
.limit(3)) .limit(3)
)
# For each image, get the image data and add it to the group item # For each image, get the image data and add it to the group item
group.images = [] group.images = []
for image in images: for image in images:
group.images.append(db_session.query(db.Posts.filename, db.Posts.alt, group.images.append(
db.Posts.colours, db.Posts.id) db_session.query(
db.Posts.filename, db.Posts.alt, db.Posts.colours, db.Posts.id
)
.filter(db.Posts.id == image[0]) .filter(db.Posts.id == image[0])
.first()) .first()
)
return render_template('list.html', groups=groups) return render_template("list.html", groups=groups)
@blueprint.route('/<int:group_id>') @blueprint.route("/<int:group_id>")
def group(group_id): def group(group_id):
""" """
Group view, shows all images in a group Group view, shows all images in a group
""" """
# Get the group, if it doesn't exist, 404 # Get the group, if it doesn't exist, 404
group = (db_session.query(db.Groups) group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
.filter(db.Groups.id == group_id)
.first())
if group is None: if group is None:
abort(404, 'Group not found! D:') abort(404, "Group not found! D:")
# Get the group's author username # Get the group's author username
group.author_username = (db_session.query(db.Users.username) group.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id) .filter(db.Users.id == group.author_id)
.first()[0]) .first()[0]
)
# Get all images in the group from the junction table # Get all images in the group from the junction table
junction = (db_session.query(db.GroupJunction.post_id) junction = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id) .filter(db.GroupJunction.group_id == group_id)
.order_by(db.GroupJunction.date_added.desc()) .order_by(db.GroupJunction.date_added.desc())
.all()) .all()
)
# Get the image data for each image in the group # Get the image data for each image in the group
images = [] images = []
for image in junction: for image in junction:
images.append(db_session.query(db.Posts) images.append(
.filter(db.Posts.id == image[0]) db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
.first()) )
# Check contrast for the first image in the group for the banner # Check contrast for the first image in the group for the banner
text_colour = 'rgb(var(--fg-black))' text_colour = "rgb(var(--fg-black))"
if images: if images:
text_colour = contrast.contrast(images[0].colours[0], text_colour = contrast.contrast(
'rgb(var(--fg-black))', images[0].colours[0], "rgb(var(--fg-black))", "rgb(var(--fg-white))"
'rgb(var(--fg-white))') )
return render_template('group.html', return render_template(
group=group, "group.html", group=group, images=images, text_colour=text_colour
images=images, )
text_colour=text_colour)
@blueprint.route('/<int:group_id>/<int:image_id>') @blueprint.route("/<int:group_id>/<int:image_id>")
def group_post(group_id, image_id): def group_post(group_id, image_id):
""" """
Image view, shows the image and its metadata from a specific group Image view, shows the image and its metadata from a specific group
""" """
# Get the image, if it doesn't exist, 404 # Get the image, if it doesn't exist, 404
image = (db_session.query(db.Posts) image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
.filter(db.Posts.id == image_id)
.first())
if image is None: if image is None:
abort(404, 'Image not found') abort(404, "Image not found")
# Get the image's author username # Get the image's author username
image.author_username = (db_session.query(db.Users.username) image.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id) .filter(db.Users.id == image.author_id)
.first()[0]) .first()[0]
)
# Get all groups the image is in # Get all groups the image is in
groups = (db_session.query(db.GroupJunction.group_id) groups = (
db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id) .filter(db.GroupJunction.post_id == image_id)
.all()) .all()
)
# Get the group data for each group the image is in # Get the group data for each group the image is in
image.groups = [] image.groups = []
for group in groups: for group in groups:
image.groups.append(db_session.query(db.Groups.id, db.Groups.name) image.groups.append(
db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0]) .filter(db.Groups.id == group[0])
.first()) .first()
)
# Get the next and previous images in the group # Get the next and previous images in the group
next_url = (db_session.query(db.GroupJunction.post_id) next_url = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id) .filter(db.GroupJunction.group_id == group_id)
.filter(db.GroupJunction.post_id > image_id) .filter(db.GroupJunction.post_id > image_id)
.order_by(db.GroupJunction.date_added.asc()) .order_by(db.GroupJunction.date_added.asc())
.first()) .first()
prev_url = (db_session.query(db.GroupJunction.post_id) )
prev_url = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id) .filter(db.GroupJunction.group_id == group_id)
.filter(db.GroupJunction.post_id < image_id) .filter(db.GroupJunction.post_id < image_id)
.order_by(db.GroupJunction.date_added.desc()) .order_by(db.GroupJunction.date_added.desc())
.first()) .first()
)
# If there is a next or previous image, get the URL for it # If there is a next or previous image, get the URL for it
if next_url: if next_url:
next_url = url_for('group.group_post', group_id=group_id, image_id=next_url[0]) next_url = url_for("group.group_post", group_id=group_id, image_id=next_url[0])
if prev_url: if prev_url:
prev_url = url_for('group.group_post', group_id=group_id, image_id=prev_url[0]) prev_url = url_for("group.group_post", group_id=group_id, image_id=prev_url[0])
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url) return render_template(
"image.html", image=image, next_url=next_url, prev_url=prev_url
)

View file

@ -9,12 +9,12 @@ from sqlalchemy.orm import sessionmaker
from gallery import db from gallery import db
blueprint = Blueprint('image', __name__, url_prefix='/image') blueprint = Blueprint("image", __name__, url_prefix="/image")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/<int:image_id>') @blueprint.route("/<int:image_id>")
def image(image_id): def image(image_id):
""" """
Image view, shows the image and its metadata Image view, shows the image and its metadata
@ -22,48 +22,55 @@ def image(image_id):
# Get the image, if it doesn't exist, 404 # Get the image, if it doesn't exist, 404
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first() image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if not image: if not image:
abort(404, 'Image not found :<') abort(404, "Image not found :<")
# Get the image's author username # Get the image's author username
image.author_username = (db_session.query(db.Users.username) image.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id) .filter(db.Users.id == image.author_id)
.first()[0]) .first()[0]
)
# Get the image's groups # Get the image's groups
groups = (db_session.query(db.GroupJunction.group_id) groups = (
db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id) .filter(db.GroupJunction.post_id == image_id)
.all()) .all()
)
# For each group, get the group data and add it to the image item # For each group, get the group data and add it to the image item
image.groups = [] image.groups = []
for group in groups: for group in groups:
image.groups.append(db_session.query(db.Groups.id, db.Groups.name) image.groups.append(
db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0]) .filter(db.Groups.id == group[0])
.first()) .first()
)
# Get the next and previous images # Get the next and previous images
# Check if there is a group ID set # Check if there is a group ID set
next_url = (db_session.query(db.Posts.id) next_url = (
db_session.query(db.Posts.id)
.filter(db.Posts.id > image_id) .filter(db.Posts.id > image_id)
.order_by(db.Posts.id.asc()) .order_by(db.Posts.id.asc())
.first()) .first()
prev_url = (db_session.query(db.Posts.id) )
prev_url = (
db_session.query(db.Posts.id)
.filter(db.Posts.id < image_id) .filter(db.Posts.id < image_id)
.order_by(db.Posts.id.desc()) .order_by(db.Posts.id.desc())
.first()) .first()
)
# If there is a next or previous image, get the url # If there is a next or previous image, get the url
if next_url: if next_url:
next_url = url_for('image.image', image_id=next_url[0]) next_url = url_for("image.image", image_id=next_url[0])
if prev_url: if prev_url:
prev_url = url_for('image.image', image_id=prev_url[0]) prev_url = url_for("image.image", image_id=prev_url[0])
# Yoink all the images in the database # Yoink all the images in the database
total_images = (db_session.query(db.Posts.id) total_images = db_session.query(db.Posts.id).order_by(db.Posts.id.desc()).all()
.order_by(db.Posts.id.desc()) limit = current_app.config["UPLOAD_CONF"]["max-load"]
.all())
limit = current_app.config['UPLOAD_CONF']['max-load']
# If the number of items is less than the limit, no point of calculating the page # If the number of items is less than the limit, no point of calculating the page
if len(total_images) <= limit: if len(total_images) <= limit:
@ -72,11 +79,16 @@ def image(image_id):
# How many pages should there be # How many pages should there be
for i in range(ceil(len(total_images) / limit)): for i in range(ceil(len(total_images) / limit)):
# Slice the list of IDs into chunks of the limit # Slice the list of IDs into chunks of the limit
for j in total_images[i * limit:(i + 1) * limit]: for j in total_images[i * limit : (i + 1) * limit]:
# Is our image in this chunk? # Is our image in this chunk?
if image_id in j: if image_id in j:
return_page = i + 1 return_page = i + 1
break break
return render_template('image.html', image=image, next_url=next_url, return render_template(
prev_url=prev_url, return_page=return_page) "image.html",
image=image,
next_url=next_url,
prev_url=prev_url,
return_page=return_page,
)

View file

@ -10,40 +10,50 @@ from sqlalchemy.orm import sessionmaker
from gallery import db from gallery import db
blueprint = Blueprint('gallery', __name__) blueprint = Blueprint("gallery", __name__)
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/') @blueprint.route("/")
def index(): def index():
""" """
Home page of the website, shows the feed of the latest images Home page of the website, shows the feed of the latest images
""" """
# meme # meme
if request.args.get('coffee') == 'please': if request.args.get("coffee") == "please":
abort(418) abort(418)
# pagination, defaults to page 1 if no page is specified # pagination, defaults to page 1 if no page is specified
page = request.args.get('page', default=1, type=int) page = request.args.get("page", default=1, type=int)
limit = current_app.config['UPLOAD_CONF']['max-load'] limit = current_app.config["UPLOAD_CONF"]["max-load"]
# get the total number of images in the database # get the total number of images in the database
# calculate the total number of pages, and make sure the page number is valid # calculate the total number of pages, and make sure the page number is valid
total_images = db_session.query(db.Posts.id).count() total_images = db_session.query(db.Posts.id).count()
pages = ceil(max(total_images, limit) / limit) pages = ceil(max(total_images, limit) / limit)
if page > pages: if page > pages:
abort(404, 'You have reached the far and beyond, ' + abort(
'but you will not find your answers here.') 404,
"You have reached the far and beyond, "
+ "but you will not find your answers here.",
)
# get the images for the current page # get the images for the current page
images = (db_session.query(db.Posts.filename, db.Posts.alt, db.Posts.colours, images = (
db.Posts.created_at, db.Posts.id) db_session.query(
db.Posts.filename,
db.Posts.alt,
db.Posts.colours,
db.Posts.created_at,
db.Posts.id,
)
.order_by(db.Posts.id.desc()) .order_by(db.Posts.id.desc())
.offset((page - 1) * limit) .offset((page - 1) * limit)
.limit(limit) .limit(limit)
.all()) .all()
)
return render_template('index.html', images=images, return render_template(
total_images=total_images, "index.html", images=images, total_images=total_images, pages=pages, page=page
pages=pages, page=page) )

View file

@ -9,31 +9,31 @@ from sqlalchemy.orm import sessionmaker
from gallery import db from gallery import db
blueprint = Blueprint('profile', __name__, url_prefix='/profile') blueprint = Blueprint("profile", __name__, url_prefix="/profile")
db_session = sessionmaker(bind=db.engine) db_session = sessionmaker(bind=db.engine)
db_session = db_session() db_session = db_session()
@blueprint.route('/profile') @blueprint.route("/profile")
def profile(): def profile():
""" """
Profile overview, shows all profiles on the onlylegs gallery Profile overview, shows all profiles on the onlylegs gallery
""" """
user_id = request.args.get('id', default=None, type=int) user_id = request.args.get("id", default=None, type=int)
# If there is no userID set, check if the user is logged in and display their profile # If there is no userID set, check if the user is logged in and display their profile
if not user_id: if not user_id:
if current_user.is_authenticated: if current_user.is_authenticated:
user_id = current_user.id user_id = current_user.id
else: else:
abort(404, 'You must be logged in to view your own profile!') abort(404, "You must be logged in to view your own profile!")
# Get the user's data # Get the user's data
user = db_session.query(db.Users).filter(db.Users.id == user_id).first() user = db_session.query(db.Users).filter(db.Users.id == user_id).first()
if not user: if not user:
abort(404, 'User not found :c') abort(404, "User not found :c")
images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all() images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all()
return render_template('profile.html', user=user, images=images) return render_template("profile.html", user=user, images=images)

View file

@ -4,40 +4,40 @@ OnlyLegs - Settings page
from flask import Blueprint, render_template from flask import Blueprint, render_template
from flask_login import login_required from flask_login import login_required
blueprint = Blueprint('settings', __name__, url_prefix='/settings') blueprint = Blueprint("settings", __name__, url_prefix="/settings")
@blueprint.route('/') @blueprint.route("/")
@login_required @login_required
def general(): def general():
""" """
General settings page General settings page
""" """
return render_template('settings/general.html') return render_template("settings/general.html")
@blueprint.route('/server') @blueprint.route("/server")
@login_required @login_required
def server(): def server():
""" """
Server settings page Server settings page
""" """
return render_template('settings/server.html') return render_template("settings/server.html")
@blueprint.route('/account') @blueprint.route("/account")
@login_required @login_required
def account(): def account():
""" """
Account settings page Account settings page
""" """
return render_template('settings/account.html') return render_template("settings/account.html")
@blueprint.route('/logs') @blueprint.route("/logs")
@login_required @login_required
def logs(): def logs():
""" """
Logs settings page Logs settings page
""" """
return render_template('settings/logs.html') return render_template("settings/logs.html")

11
run.py
View file

@ -5,7 +5,8 @@ from setup.args import PORT, ADDRESS, WORKERS, DEBUG
from setup.configuration import Configuration from setup.configuration import Configuration
print(""" print(
"""
:::::::: :::: ::: ::: ::: ::: ::: ::::::::: ::::::::: :::::::: :::::::: :::: ::: ::: ::: ::: ::: ::::::::: ::::::::: ::::::::
:+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+:
+:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+
@ -15,7 +16,8 @@ print("""
######## ### #### ########## ### ########## ######### ######### ######## ######## ### #### ########## ### ########## ######### ######### ########
Created by Fluffy Bean - Version 23.04.06 Created by Fluffy Bean - Version 23.04.06
""") """
)
# Run pre-startup checks and load configuration # Run pre-startup checks and load configuration
@ -24,6 +26,7 @@ Configuration()
if DEBUG: if DEBUG:
from gallery import create_app from gallery import create_app
create_app().run(host=ADDRESS, port=PORT, debug=True, threaded=True) create_app().run(host=ADDRESS, port=PORT, debug=True, threaded=True)
else: else:
from setup.runner import OnlyLegs # pylint: disable=C0412 from setup.runner import OnlyLegs # pylint: disable=C0412
@ -33,8 +36,8 @@ else:
sys.argv = [sys.argv[0]] sys.argv = [sys.argv[0]]
options = { options = {
'bind': f'{ADDRESS}:{PORT}', "bind": f"{ADDRESS}:{PORT}",
'workers': WORKERS, "workers": WORKERS,
} }
OnlyLegs(options).run() OnlyLegs(options).run()

View file

@ -13,11 +13,17 @@ Startup arguments for the OnlyLegs gallery
import argparse import argparse
parser = argparse.ArgumentParser(description='Run the OnlyLegs gallery') parser = argparse.ArgumentParser(description="Run the OnlyLegs gallery")
parser.add_argument('-p', '--port', type=int, default=5000, help='Port to run on') parser.add_argument("-p", "--port", type=int, default=5000, help="Port to run on")
parser.add_argument('-a', '--address', type=str, default='127.0.0.0', help='Address to run on') parser.add_argument(
parser.add_argument('-w', '--workers', type=int, default=4, help='Number of workers to run') "-a", "--address", type=str, default="127.0.0.0", help="Address to run on"
parser.add_argument('-d', '--debug', action='store_true', help='Run as Flask app in debug mode') )
parser.add_argument(
"-w", "--workers", type=int, default=4, help="Number of workers to run"
)
parser.add_argument(
"-d", "--debug", action="store_true", help="Run as Flask app in debug mode"
)
args = parser.parse_args() args = parser.parse_args()

View file

@ -9,13 +9,14 @@ import platformdirs
import yaml import yaml
USER_DIR = platformdirs.user_config_dir('onlylegs') USER_DIR = platformdirs.user_config_dir("onlylegs")
class Configuration: class Configuration:
""" """
Setup the application on first run Setup the application on first run
""" """
def __init__(self): def __init__(self):
""" """
Main setup function Main setup function
@ -27,11 +28,11 @@ class Configuration:
self.make_dir() self.make_dir()
# Check if the .env file exists # Check if the .env file exists
if not os.path.exists(os.path.join(USER_DIR, '.env')): if not os.path.exists(os.path.join(USER_DIR, ".env")):
self.make_env() self.make_env()
# Check if the conf.yml file exists # Check if the conf.yml file exists
if not os.path.exists(os.path.join(USER_DIR, 'conf.yml')): if not os.path.exists(os.path.join(USER_DIR, "conf.yml")):
self.make_yaml() self.make_yaml()
# Load the config files # Load the config files
@ -43,8 +44,8 @@ class Configuration:
Create the user directory Create the user directory
""" """
os.makedirs(USER_DIR) os.makedirs(USER_DIR)
os.makedirs(os.path.join(USER_DIR, 'instance')) os.makedirs(os.path.join(USER_DIR, "instance"))
os.makedirs(os.path.join(USER_DIR, 'uploads')) os.makedirs(os.path.join(USER_DIR, "uploads"))
print("Created user directory at:", USER_DIR) print("Created user directory at:", USER_DIR)
@ -54,21 +55,23 @@ class Configuration:
Create the .env file with default values Create the .env file with default values
""" """
env_conf = { env_conf = {
'FLASK_SECRET': os.urandom(32).hex(), "FLASK_SECRET": os.urandom(32).hex(),
} }
with open(os.path.join(USER_DIR, '.env'), encoding='utf-8', mode='w+') as file: with open(os.path.join(USER_DIR, ".env"), encoding="utf-8", mode="w+") as file:
for key, value in env_conf.items(): for key, value in env_conf.items():
file.write(f"{key}={value}\n") file.write(f"{key}={value}\n")
print(""" print(
"""
#################################################### ####################################################
# A NEW KEY WAS GENERATED FOR YOU! PLEASE NOTE # # A NEW KEY WAS GENERATED FOR YOU! PLEASE NOTE #
# DOWN THE FLASK_SECRET KEY LOCATED IN YOUR # # DOWN THE FLASK_SECRET KEY LOCATED IN YOUR #
# .config/onlylegs/.env FOLDER! LOOSING THIS KEY # # .config/onlylegs/.env FOLDER! LOOSING THIS KEY #
# WILL RESULT IN YOU BEING UNABLE TO LOG IN! # # WILL RESULT IN YOU BEING UNABLE TO LOG IN! #
#################################################### ####################################################
""") """
)
@staticmethod @staticmethod
def make_yaml(): def make_yaml():
@ -76,8 +79,8 @@ class Configuration:
Create the YAML config file with default values Create the YAML config file with default values
""" """
is_correct = False is_correct = False
email_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') email_regex = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
username_regex = re.compile(r'\b[A-Za-z0-9._%+-]+\b') username_regex = re.compile(r"\b[A-Za-z0-9._%+-]+\b")
print("\nNo config file found, please enter the following information:") print("\nNo config file found, please enter the following information:")
while not is_correct: while not is_correct:
@ -99,47 +102,52 @@ class Configuration:
continue continue
# Check if user is happy with the values # Check if user is happy with the values
if input("Is this correct? (y/n): ").lower() == 'y': if input("Is this correct? (y/n): ").lower() == "y":
is_correct = True is_correct = True
yaml_conf = { yaml_conf = {
'admin': { "admin": {
'name': name, "name": name,
'username': username, "username": username,
'email': email, "email": email,
}, },
'upload': { "upload": {
'allowed-extensions': { "allowed-extensions": {
'jpg': 'jpeg', "jpg": "jpeg",
'jpeg': 'jpeg', "jpeg": "jpeg",
'png': 'png', "png": "png",
'webp': 'webp', "webp": "webp",
}, },
'max-size': 69, "max-size": 69,
'max-load': 50, "max-load": 50,
'rename': 'GWA_{{username}}_{{time}}', "rename": "GWA_{{username}}_{{time}}",
},
"website": {
"name": "OnlyLegs",
"motto": "A gallery built for fast and simple image management!",
"language": "en",
}, },
'website': {
'name': 'OnlyLegs',
'motto': 'A gallery built for fast and simple image management!',
'language': 'en',
}
} }
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8', mode='w+') as file: with open(
os.path.join(USER_DIR, "conf.yml"), encoding="utf-8", mode="w+"
) as file:
yaml.dump(yaml_conf, file, default_flow_style=False) yaml.dump(yaml_conf, file, default_flow_style=False)
print("Generated config file, you can change these values in the settings of the app") print(
"Generated config file, you can change these values in the settings of the app"
)
@staticmethod @staticmethod
def logging_config(): def logging_config():
""" """
Set the logging config Set the logging config
""" """
logging.getLogger('werkzeug').disabled = True logging.getLogger("werkzeug").disabled = True
logging.basicConfig( logging.basicConfig(
filename=os.path.join(USER_DIR, 'only.log'), filename=os.path.join(USER_DIR, "only.log"),
level=logging.INFO, level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S', datefmt="%Y-%m-%d %H:%M:%S",
format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s', format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s",
encoding='utf-8') encoding="utf-8",
)

View file

@ -9,6 +9,7 @@ class OnlyLegs(Application):
""" """
Gunicorn application Gunicorn application
""" """
def __init__(self, options={}): # pylint: disable=W0102, W0231 def __init__(self, options={}): # pylint: disable=W0102, W0231
self.usage = None self.usage = None
self.callable = None self.callable = None
@ -27,7 +28,7 @@ class OnlyLegs(Application):
@staticmethod @staticmethod
def prog(): # pylint: disable=C0116, E0202 def prog(): # pylint: disable=C0116, E0202
return 'OnlyLegs' return "OnlyLegs"
def load(self): def load(self):
return util.import_app('gallery:create_app()') return util.import_app("gallery:create_app()")