diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index a86de9c..b16bcba 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -7,6 +7,7 @@ import os from flask import Flask, g, redirect, render_template, request, send_file, flash from werkzeug.security import generate_password_hash +from .modules.utils import User, Album, get_all_albums from .modules import albums, auth, partition, admin from .modules.auth import admin_required from .modules.db import get_db @@ -43,6 +44,7 @@ def add_user(): if request.method == "POST": username = request.form["username"] password = request.form["password"] + album_uuid = request.form["album_uuid"] db = get_db() error = None @@ -64,11 +66,17 @@ def add_user(): error = f"Le nom d'utilisateur {username} est déjà pris." else: # Success, go to the login page. - flash(f"Utilisateur {username} crée") - return redirect("/albums") + user = User(name=username) + try: + 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") + return render_template("auth/register.html", albums=get_all_albums()) if __name__ == "__main__": diff --git a/partitioncloud/modules/admin.py b/partitioncloud/modules/admin.py index ae4888c..950c65c 100644 --- a/partitioncloud/modules/admin.py +++ b/partitioncloud/modules/admin.py @@ -21,10 +21,12 @@ def index(): SELECT id FROM user """ ) - users = [User(u["id"]) for u in users_id] + users = [User(user_id=u["id"]) for u in users_id] for u in users: u.albums = u.get_albums() + u.partitions = u.get_partitions() db.close() + return render_template( "admin/index.html", users=users diff --git a/partitioncloud/modules/albums.py b/partitioncloud/modules/albums.py index 486c92f..32e372e 100644 --- a/partitioncloud/modules/albums.py +++ b/partitioncloud/modules/albums.py @@ -20,7 +20,7 @@ bp = Blueprint("albums", __name__, url_prefix="/albums") @bp.route("/") @login_required def index(): - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) albums = user.get_albums() if user.access_level == 1: @@ -43,7 +43,7 @@ def search_page(): search.flush_cache() partitions_local = search.local_search(query, get_all_partitions()) - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) if nb_queries > 0: if user.access_level != 1: @@ -69,8 +69,8 @@ def album(uuid): """ try: album = Album(uuid=uuid) - album.users = [User(i["id"]) for i in album.get_users()] - user = User(session.get("user_id")) + album.users = [User(user_id=i["id"]) for i in album.get_users()] + user = User(user_id=session.get("user_id")) partitions = album.get_partitions() if user.id is None: # On ne propose pas aux gens non connectés de rejoindre l'album @@ -164,7 +164,7 @@ def create_album(): @bp.route("//join") @login_required def join_album(uuid): - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) try: user.join_album(uuid) except LookupError: @@ -178,7 +178,7 @@ def join_album(uuid): @bp.route("//quit") @login_required def quit_album(uuid): - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) album = Album(uuid=uuid) users = album.get_users() if user.id not in [u["id"] for u in users]: @@ -205,7 +205,7 @@ def delete_album(uuid): error = None users = album.get_users() - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) if len(users) > 1: error = "Vous n'êtes pas seul dans cet album." elif len(users) == 1 and users[0]["id"] != user.id: @@ -228,7 +228,7 @@ def delete_album(uuid): @login_required def add_partition(album_uuid): db = get_db() - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) album = Album(uuid=album_uuid) if (not user.is_participant(album.uuid)) and (user.access_level != 1): @@ -322,9 +322,8 @@ def add_partition(album_uuid): @bp.route("/add-partition", methods=["POST"]) @login_required def add_partition_from_search(): - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) error = None - db = get_db() if "album-uuid" not in request.form: error = "Il est nécessaire de sélectionner un album." @@ -341,6 +340,7 @@ def add_partition_from_search(): album = Album(request.form["album-uuid"]) if request.form["partition-type"] == "local_file": + db = get_db() data = db.execute( """ SELECT * FROM contient_partition diff --git a/partitioncloud/modules/auth.py b/partitioncloud/modules/auth.py index 8f1da28..8f52370 100644 --- a/partitioncloud/modules/auth.py +++ b/partitioncloud/modules/auth.py @@ -46,7 +46,7 @@ def admin_required(view): flash("Vous devez être connecté pour accéder à cette page.") return redirect(url_for("auth.login")) - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) if user.access_level != 1: flash("Droits insuffisants.") return redirect("/albums") diff --git a/partitioncloud/modules/partition.py b/partitioncloud/modules/partition.py index 2b88ef0..000abb9 100644 --- a/partitioncloud/modules/partition.py +++ b/partitioncloud/modules/partition.py @@ -40,7 +40,7 @@ def edit(uuid): except LookupError: abort(404) - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) if user.access_level != 1 and partition.user_id != user.id: flash("Vous n'êtes pas autorisé à modifier cette partition.") return redirect("/albums") @@ -71,6 +71,52 @@ def edit(uuid): return redirect("/albums") +@bp.route("//details", methods=["GET", "POST"]) +@admin_required +def details(uuid): + db = get_db() + try: + partition = Partition(uuid=uuid) + except LookupError: + abort(404) + + user = User(user_id=session.get("user_id")) + try: + partition_user = partition.get_user() + except LookupError: + partition_user = None + + if request.method == "GET": + return render_template( + "partition/details.html", + partition=partition, + user=partition_user, + albums=partition.get_albums() + ) + + error = None + + if "name" not in request.form or request.form["name"].strip() == "": + error = "Un titre est requis." + elif "author" not in request.form: + error = "Un nom d'auteur est requis (à minima nul)" + elif "body" not in request.form: + error = "Des paroles sont requises (à minima nulles)" + + if error is not None: + flash(error) + return redirect(f"/partition/{ uuid }/details") + + partition.update( + name=request.form["name"], + author=request.form["author"], + body=request.form["body"] + ) + + flash(f"Partition {request.form['name']} modifiée avec succès.") + return redirect("/albums") + + @bp.route("//delete", methods=["GET", "POST"]) @login_required def delete(uuid): @@ -79,7 +125,7 @@ def delete(uuid): except LookupError: abort(404) - user = User(session.get("user_id")) + user = User(user_id=session.get("user_id")) if user.access_level != 1 and partition.user_id != user.id: flash("Vous n'êtes pas autorisé à supprimer cette partition.") @@ -116,5 +162,5 @@ def partition_search(uuid): @bp.route("/") @admin_required def index(): - partitions = get_all_partitions().fetchall() + partitions = get_all_partitions() return render_template("admin/partitions.html", partitions=partitions) \ No newline at end of file diff --git a/partitioncloud/modules/search.py b/partitioncloud/modules/search.py index 470713d..3de45e3 100644 --- a/partitioncloud/modules/search.py +++ b/partitioncloud/modules/search.py @@ -124,4 +124,5 @@ def flush_cache(): DELETE FROM search_results WHERE creation_time <= datetime('now', '-15 minutes', 'localtime') """ - ) \ No newline at end of file + ) + db.commit() \ No newline at end of file diff --git a/partitioncloud/modules/utils.py b/partitioncloud/modules/utils.py index 215f2ce..2a82d64 100644 --- a/partitioncloud/modules/utils.py +++ b/partitioncloud/modules/utils.py @@ -3,23 +3,36 @@ import os from .db import get_db class User(): - def __init__(self, user_id): + def __init__(self, user_id=None, name=None): self.id = user_id + self.username = name self.albums = None + self.partitions = None db = get_db() - if self.id is None: + if self.id is None and self.username is None: self.username = "" self.access_level = -1 else: - data = db.execute( - """ - SELECT username, access_level FROM user - WHERE id = ? - """, - (self.id,) - ).fetchone() + if self.id is not None: + data = db.execute( + """ + SELECT * FROM user + WHERE id = ? + """, + (self.id,) + ).fetchone() + elif self.username is not None: + data = db.execute( + """ + SELECT * FROM user + WHERE username = ? + """, + (self.username,) + ).fetchone() + + self.id = data["id"] self.username = data["username"] self.access_level = data["access_level"] self.color = self.get_color() @@ -57,6 +70,24 @@ class User(): (self.id,), ).fetchall() + + def get_partitions(self): + db = get_db() + if self.access_level == 1: + return db.execute( + """ + SELECT * FROM partition + """ + ).fetchall() + return db.execute( + """ + SELECT * FROM partition + JOIN user ON user_id = user.id + WHERE user.id = ? + """, + (self.id,), + ).fetchall() + def join_album(self, album_uuid): db = get_db() @@ -294,6 +325,34 @@ class Partition(): ) db.commit() + def get_user(self): + db = get_db() + user = db.execute( + """ + SELECT * FROM user + JOIN partition ON user_id = user.id + WHERE partition.uuid = ? + """, + (self.uuid,), + ).fetchone() + + if user is None: + raise LookupError + + return User(user_id=user["id"]) + + def get_albums(self): + db = get_db() + return db.execute( + """ + SELECT * FROM album + JOIN contient_partition ON album.id = album_id + WHERE partition_uuid = ? + """, + (self.uuid,), + ).fetchall() + + def get_all_partitions(): db = get_db() @@ -302,4 +361,29 @@ def get_all_partitions(): SELECT * FROM partition """ ) - return partitions \ No newline at end of file + # Transform sql object to dictionary usable in any thread + return [ + { + "uuid": p["uuid"], + "name": p["name"], + "author": p["author"], + "body": p["body"], + "user_id": p["user_id"] + } for p in partitions + ] + +def get_all_albums(): + db = get_db() + albums = db.execute( + """ + SELECT * FROM album + """ + ) + # Transform sql object to dictionary usable in any thread + return [ + { + "id": a["id"], + "name": a["name"], + "uuid": a["uuid"] + } for a in albums + ] \ No newline at end of file diff --git a/partitioncloud/static/style.css b/partitioncloud/static/style.css index 2950b50..aaa9ab4 100644 --- a/partitioncloud/static/style.css +++ b/partitioncloud/static/style.css @@ -336,4 +336,25 @@ td { #paroles { font-family: inherit; font-size: 0.8rem; +} + +.button:hover { + background-color: #ddd; +} + +.button { + display: inline; + background-color: lightgray; + padding: 2px; + margin: 2px; + border-radius: 2px; + font-size: 0.9rem; +} + +#actions-rapides { + text-align: center; +} + +#actions-rapides a { + text-decoration: none; } \ No newline at end of file diff --git a/partitioncloud/templates/admin/index.html b/partitioncloud/templates/admin/index.html index 42ac920..9ba5316 100644 --- a/partitioncloud/templates/admin/index.html +++ b/partitioncloud/templates/admin/index.html @@ -8,11 +8,16 @@ {% endblock %} {% block content %} + + @@ -28,6 +33,7 @@ + {% endfor %} diff --git a/partitioncloud/templates/admin/partitions.html b/partitioncloud/templates/admin/partitions.html index 5014d6b..0645a08 100644 --- a/partitioncloud/templates/admin/partitions.html +++ b/partitioncloud/templates/admin/partitions.html @@ -8,15 +8,18 @@ {% if partitions|length != 0 %}
{% for partition in partitions %} - - {% else %} diff --git a/partitioncloud/templates/albums/album.html b/partitioncloud/templates/albums/album.html index b3aa713..10d9cf8 100644 --- a/partitioncloud/templates/albums/album.html +++ b/partitioncloud/templates/albums/album.html @@ -53,6 +53,7 @@ {% endfor %}
{% else %} -
Aucune partition disponible
+
+
Aucune partition disponible
{% endif %} {% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/auth/register.html b/partitioncloud/templates/auth/register.html index 401f119..14f2e20 100644 --- a/partitioncloud/templates/auth/register.html +++ b/partitioncloud/templates/auth/register.html @@ -5,11 +5,18 @@ {% endblock %} {% block content %} - + + + {% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/partition/details.html b/partitioncloud/templates/partition/details.html new file mode 100644 index 0000000..ffddf29 --- /dev/null +++ b/partitioncloud/templates/partition/details.html @@ -0,0 +1,42 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Détails - {{ partition.name }}{% endblock %}

+{% endblock %} + +{% block content %} +
+{% if user is not none %} +Auteur: +{% else %} +Auteur: inconnu +{% endif %} +
+ Albums: + +
+Fichier: voir le fichier ↗ + + + Titre* + + Auteur + + Paroles + + + + + + + +{% endblock %} \ No newline at end of file
Utilisateur AlbumsPartitions Privilèges
{{ user.albums | length }}{{ user.partitions | length }} {% if user.access_level != 1 %}❌{% else %}✅{% endif %}