partitioncloud-server/partitioncloud/__init__.py

136 lines
4.0 KiB
Python
Raw Normal View History

2022-08-13 23:36:10 +02:00
#!/usr/bin/python3
"""
Main file
"""
import os
import sys
2024-01-13 12:44:28 +01:00
import datetime
2023-09-08 13:56:08 +02:00
import subprocess
import importlib.util
2022-08-13 23:36:10 +02:00
from flask import Flask, g, redirect, render_template, request, send_file, flash, session, abort
from werkzeug.security import generate_password_hash
2022-12-19 15:19:58 +01:00
from .modules.utils import User, Album, get_all_albums
2024-01-16 21:00:14 +01:00
from .modules import albums, auth, partition, admin, groupe, thumbnails
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__)
def load_config():
app.config.from_object('default_config')
app.instance_path = os.path.abspath(app.config["INSTANCE_PATH"])
if not os.path.exists(app.instance_path):
print("[ERROR] Instance path does not exist. Make sure to use an existing directory.")
sys.exit(1)
if os.path.exists(f"{app.instance_path}/config.py"):
# Load module from instance_path/config.py in user_config object
spec = importlib.util.spec_from_file_location(
".",
os.path.join(app.instance_path, "config.py")
)
user_config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(user_config)
app.config.from_object(user_config)
if os.path.abspath(app.config["INSTANCE_PATH"]) != app.instance_path:
2024-01-16 21:32:00 +01:00
print(("[ERROR] Using two different instance path.\n"
"Please modify INSTANCE_PATH only in default_config.py ",
"and remove it from $INSTANCE_PATH/config.py"))
sys.exit(1)
else:
print("[WARNING] Using default config")
app.config.from_mapping(
DATABASE=os.path.join(app.instance_path, f"{__name__}.sqlite"),
)
def get_version():
try:
result = subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE, check=True)
return result.stdout.decode('utf8')
except (FileNotFoundError, subprocess.CalledProcessError):
# In case git not found or any platform specific weird error
return "unknown"
load_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)
2023-10-11 17:15:49 +02:00
app.register_blueprint(groupe.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)
2024-01-16 21:00:14 +01:00
app.register_blueprint(thumbnails.bp)
2022-08-13 23:36:10 +02:00
__version__ = get_version()
2023-09-08 13:56:08 +02:00
2022-08-13 23:36:10 +02:00
@app.route("/")
def home():
"""Redirect to home"""
return redirect("/albums/")
@app.route("/add-user", methods=["GET", "POST"])
@admin_required
def add_user():
"""
Ajouter un utilisateur en tant qu'administrateur
"""
current_user = User(user_id=session.get("user_id"))
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"]
2023-12-15 13:38:32 +01:00
error = auth.create_user(username, password)
if error is None:
2023-12-15 13:38:32 +01:00
# Success, go to the login page.
user = User(name=username)
try:
2023-12-15 13:38:32 +01:00
if album_uuid != "":
user.join_album(album_uuid)
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")
flash(error)
return render_template("auth/register.html", albums=get_all_albums(), user=current_user)
2024-01-13 12:44:28 +01:00
@app.before_request
def before_request():
"""Set cookie max age to 31 days"""
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(days=int(app.config["MAX_AGE"]))
2023-09-08 13:56:08 +02:00
@app.context_processor
def inject_default_variables():
2023-12-15 11:36:34 +01:00
"""Inject the version number in the template variables"""
2023-09-08 13:56:08 +02:00
if __version__ == "unknown":
2023-12-15 11:36:34 +01:00
return {"version": ''}
return {"version": __version__}
2023-09-08 13:56:08 +02:00
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")