diff --git a/partitioncloud/albums.py b/partitioncloud/albums.py index 93ad3a9..8265d15 100644 --- a/partitioncloud/albums.py +++ b/partitioncloud/albums.py @@ -3,6 +3,7 @@ Albums module """ import os +import shutil from uuid import uuid4 from flask import (Blueprint, abort, flash, redirect, render_template, request, @@ -35,8 +36,19 @@ def search_page(): query = request.form["query"] search.flush_cache() partitions_local = search.local_search(query, get_all_partitions()) - google_results = search.online_search(query) - return render_template("albums/search.html", partitions=partitions_local, google_results=google_results, query=query) + if "online-search" in request.form: + google_results = search.online_search(query) + else: + google_results = [] + user = User(session.get("user_id")) + + return render_template( + "albums/search.html", + partitions=partitions_local, + google_results=google_results, + query=query, + albums=user.get_albums() + ) @bp.route("/") def album(uuid): @@ -193,10 +205,25 @@ def add_partition(album_uuid): error = None - if "file" not in request.files: - error = "Aucun fichier n'a été fourni." - elif "name" not in request.form: + if "name" not in request.form: error = "Un titre est requis." + elif "file" not in request.files: + if "partition-uuid" not in request.form: + error = "Aucun fichier n'a été fourni." + else: + partition_type = "uuid" + search_uuid = request.form["partition-uuid"] + data = db.execute( + """ + SELECT * FROM search_results + WHERE uuid = ? + """, + (search_uuid,) + ).fetchone() + if data is None: + error = "Les résultats de la recherche ont expiré." + else: + partition_type = "file" if error is not None: flash(error) @@ -224,8 +251,11 @@ def add_partition(album_uuid): ) db.commit() - file = request.files["file"] - file.save(f"partitioncloud/partitions/{partition_uuid}.pdf") + if partition_type == "file": + file = request.files["file"] + file.save(f"partitioncloud/partitions/{partition_uuid}.pdf") + else: + shutil.copyfile(f"partitioncloud/search-partitions/{search_uuid}.pdf", f"partitioncloud/partitions/{partition_uuid}.pdf") os.system( f'/usr/bin/convert -thumbnail\ @@ -242,19 +272,64 @@ def add_partition(album_uuid): """, (album.uuid,) ).fetchone()["id"] - - db.execute( - """ - INSERT INTO contient_partition (partition_uuid, album_id) - VALUES (?, ?) - """, - (partition_uuid, album.id), - ) db.commit() + album.add_partition(partition_uuid) + break except db.IntegrityError: pass flash(f"Partition {request.form['name']} ajoutée") - return redirect(f"/albums/{album.uuid}") \ No newline at end of file + return redirect(f"/albums/{album.uuid}") + + +@bp.route("/add-partition", methods=["POST"]) +@login_required +def add_partition_from_search(): + user = User(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." + elif "partition-uuid" not in request.form: + error = "Il est nécessaire de sélectionner une partition." + elif "partition-type" not in request.form: + error = "Il est nécessaire de spécifier un type de partition." + elif not user.is_participant(request.form["album-uuid"]): + error = "Vous ne participez pas à cet album." + + if error is not None: + flash(error) + return redirect("/albums") + + album = Album(request.form["album-uuid"]) + if request.form["partition-type"] == "local_file": + data = db.execute( + """ + SELECT * FROM contient_partition + WHERE album_id = ? + AND partition_uuid = ? + """, + (album.id, request.form["partition-uuid"]) + ).fetchone() + + if data is None: + album.add_partition(request.form["partition-uuid"]) + flash("Partition ajoutée.") + else: + flash("Partition déjà dans l'album.") + + return redirect(f"/albums/{album.uuid}") + + elif request.form["partition-type"] == "online_search": + return render_template( + "albums/add-partition.html", + album=album, + partition_uuid=request.form["partition-uuid"] + ) + + else: + flash("Type de partition inconnu.") + return redirect("/albums") \ No newline at end of file diff --git a/partitioncloud/search.py b/partitioncloud/search.py index f472a7a..de05d33 100644 --- a/partitioncloud/search.py +++ b/partitioncloud/search.py @@ -42,16 +42,16 @@ def local_search(query, partitions): def online_search(query): """ - Renvoie les 5 résultats les plus pertinents depuis google + Renvoie les 3 résultats les plus pertinents depuis google """ db = get_db() query = f"partition filetype:pdf {query}" partitions = [] results = googlesearch.search( query, - num=5, - stop=5, - pause=1 + num=3, + stop=3, + pause=0.2 ) for element in results: while True: diff --git a/partitioncloud/static/style.css b/partitioncloud/static/style.css index 74e5e1a..0baf9fc 100644 --- a/partitioncloud/static/style.css +++ b/partitioncloud/static/style.css @@ -189,5 +189,22 @@ input[type=submit] { #search-bar { max-width: 50%; - margin-left: 25%; - } \ No newline at end of file + margin-bottom: .25rem; +} + +#search-form { + display: block; + text-align: center; +} + +.add-to-album { + width: 100%; + width: -moz-available; + width: -webkit-fill-available; + width: stretch; +} + +#online-search-label { + font-size: .7rem; + font-weight: lighter; +} \ No newline at end of file diff --git a/partitioncloud/templates/albums/add-partition.html b/partitioncloud/templates/albums/add-partition.html index 6b555a3..9024236 100644 --- a/partitioncloud/templates/albums/add-partition.html +++ b/partitioncloud/templates/albums/add-partition.html @@ -5,14 +5,18 @@ {% endblock %} {% block content %} -
+ Titre* Auteur Paroles - + {% if partition_uuid %} + + {% else %} + + {% endif %}
{% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/albums/index.html b/partitioncloud/templates/albums/index.html index f530036..860b71c 100644 --- a/partitioncloud/templates/albums/index.html +++ b/partitioncloud/templates/albums/index.html @@ -11,8 +11,11 @@ {% endblock %} {% block content %} - {% if albums|length != 0 %} {% for album in albums %} diff --git a/partitioncloud/templates/albums/search.html b/partitioncloud/templates/albums/search.html index 4ebebf8..ee4efa8 100644 --- a/partitioncloud/templates/albums/search.html +++ b/partitioncloud/templates/albums/search.html @@ -9,15 +9,27 @@

Résultats dans la bibliothèque locale

{% for partition in partitions %} - -
- - {% endfor %}
{% endif %} @@ -25,14 +37,26 @@

Résultats de la recherche en ligne

{% for partition in google_results %} - -
- -
-
{{ partition["name"] }}
+
- + +
+ + + + +
+
{% endfor %}
{% endif %} diff --git a/partitioncloud/utils.py b/partitioncloud/utils.py index 201cec6..15db7cf 100644 --- a/partitioncloud/utils.py +++ b/partitioncloud/utils.py @@ -25,6 +25,8 @@ class User(): def is_participant(self, album_uuid): db = get_db() + if self.access_level == 1: + return True return len(db.execute( """ SELECT album.id FROM album @@ -193,6 +195,21 @@ class Album(): db.commit() + def add_partition(self, partition_uuid): + """ + Ajoute une partition à l'album à partir de son uuid + """ + db = get_db() + db.execute( + """ + INSERT INTO contient_partition (partition_uuid, album_id) + VALUES (?, ?) + """, + (partition_uuid, self.id), + ) + db.commit() + + def get_all_partitions(): db = get_db() partitions = db.execute(