2022-08-13 23:36:10 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
Main file
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
2023-08-28 14:14:57 +02:00
|
|
|
from flask import Flask, g, redirect, render_template, request, send_file, flash, session, abort
|
2022-08-16 20:14:56 +02:00
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
|
2022-12-19 15:19:58 +01:00
|
|
|
from .modules.utils import User, Album, get_all_albums
|
2022-08-31 13:54:13 +02:00
|
|
|
from .modules import albums, auth, partition, admin
|
2023-08-28 14:14:57 +02:00
|
|
|
from .modules.auth import admin_required, login_required
|
2022-08-31 13:54:13 +02:00
|
|
|
from .modules.db import get_db
|
2022-08-13 23:36:10 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
app.config.from_mapping(
|
|
|
|
DATABASE=os.path.join(app.instance_path, f"{__name__}.sqlite"),
|
|
|
|
)
|
2022-08-30 18:18:38 +02:00
|
|
|
app.config.from_object('default_config')
|
|
|
|
if os.path.exists("instance/config.py"):
|
|
|
|
app.config.from_object('instance.config')
|
|
|
|
else:
|
|
|
|
print("[WARNING] Using default config")
|
2022-08-13 23:36:10 +02:00
|
|
|
|
|
|
|
app.register_blueprint(auth.bp)
|
2022-08-31 13:54:13 +02:00
|
|
|
app.register_blueprint(admin.bp)
|
2022-08-13 23:36:10 +02:00
|
|
|
app.register_blueprint(albums.bp)
|
2022-08-16 18:13:00 +02:00
|
|
|
app.register_blueprint(partition.bp)
|
2022-08-13 23:36:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
"""Redirect to home"""
|
|
|
|
return redirect("/albums/")
|
|
|
|
|
|
|
|
|
2022-08-16 20:14:56 +02:00
|
|
|
@app.route("/add-user", methods=["GET", "POST"])
|
|
|
|
@admin_required
|
|
|
|
def add_user():
|
|
|
|
"""
|
|
|
|
Ajouter un utilisateur en tant qu'administrateur
|
|
|
|
"""
|
2023-06-10 16:49:07 +02:00
|
|
|
current_user = User(user_id=session.get("user_id"))
|
|
|
|
|
2022-08-16 20:14:56 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
username = request.form["username"]
|
|
|
|
password = request.form["password"]
|
2022-12-19 15:19:58 +01:00
|
|
|
album_uuid = request.form["album_uuid"]
|
2022-08-16 20:14:56 +02:00
|
|
|
db = get_db()
|
|
|
|
error = None
|
|
|
|
|
|
|
|
if not username:
|
|
|
|
error = "Un nom d'utilisateur est requis."
|
|
|
|
elif not password:
|
|
|
|
error = "Un mot de passe est requis."
|
|
|
|
|
|
|
|
if error is None:
|
|
|
|
try:
|
|
|
|
db.execute(
|
|
|
|
"INSERT INTO user (username, password) VALUES (?, ?)",
|
|
|
|
(username, generate_password_hash(password)),
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
except db.IntegrityError:
|
|
|
|
# The username was already taken, which caused the
|
|
|
|
# commit to fail. Show a validation error.
|
|
|
|
error = f"Le nom d'utilisateur {username} est déjà pris."
|
|
|
|
else:
|
|
|
|
# Success, go to the login page.
|
2022-12-19 15:19:58 +01:00
|
|
|
user = User(name=username)
|
|
|
|
try:
|
2023-03-04 19:48:37 +01:00
|
|
|
if album_uuid != "":
|
|
|
|
user.join_album(album_uuid)
|
2022-12-19 15:19:58 +01:00
|
|
|
flash(f"Utilisateur {username} créé")
|
|
|
|
return redirect("/albums")
|
|
|
|
except LookupError:
|
|
|
|
flash(f"Cet album n'existe pas. L'utilisateur {username} a été créé")
|
|
|
|
return redirect("/albums")
|
2022-08-16 20:14:56 +02:00
|
|
|
|
|
|
|
flash(error)
|
2023-06-10 16:49:07 +02:00
|
|
|
return render_template("auth/register.html", albums=get_all_albums(), user=current_user)
|
2022-08-16 20:14:56 +02:00
|
|
|
|
|
|
|
|
2023-08-28 14:14:57 +02:00
|
|
|
@app.route("/static/search-thumbnails/<uuid>.jpg")
|
|
|
|
@login_required
|
|
|
|
def search_thumbnail(uuid):
|
|
|
|
db = get_db()
|
|
|
|
partition = db.execute(
|
|
|
|
"""
|
|
|
|
SELECT uuid, url FROM search_results
|
|
|
|
WHERE uuid = ?
|
|
|
|
""",
|
|
|
|
(uuid,)
|
|
|
|
).fetchone()
|
|
|
|
|
|
|
|
if partition is None:
|
|
|
|
abort(404)
|
|
|
|
if not os.path.exists(os.path.join(app.static_folder, "search-thumbnails", f"{uuid}.jpg")):
|
|
|
|
os.system(
|
|
|
|
f'/usr/bin/convert -thumbnail\
|
|
|
|
"178^>" -background white -alpha \
|
|
|
|
remove -crop 178x178+0+0 \
|
|
|
|
partitioncloud/search-partitions/{uuid}.pdf[0] \
|
|
|
|
partitioncloud/static/search-thumbnails/{uuid}.jpg'
|
|
|
|
)
|
|
|
|
|
|
|
|
return send_file(os.path.join(app.static_folder, "search-thumbnails", f"{uuid}.jpg"))
|
|
|
|
|
|
|
|
|
2023-06-24 16:05:50 +02:00
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
2023-06-25 09:24:44 +02:00
|
|
|
"""Automatically close db after each request"""
|
2023-06-24 16:05:50 +02:00
|
|
|
if ('db' in g) and (g.db is not None):
|
|
|
|
g.db.close()
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2022-08-13 23:36:10 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0")
|