Possibilité d'ajouter les partitions depuis la recherche

This commit is contained in:
augustin64 2022-08-17 23:52:58 +02:00
parent 8ae4a5ea44
commit 89cec8ef35
7 changed files with 180 additions and 40 deletions

View File

@ -3,6 +3,7 @@
Albums module Albums module
""" """
import os import os
import shutil
from uuid import uuid4 from uuid import uuid4
from flask import (Blueprint, abort, flash, redirect, render_template, request, from flask import (Blueprint, abort, flash, redirect, render_template, request,
@ -35,8 +36,19 @@ def search_page():
query = request.form["query"] query = request.form["query"]
search.flush_cache() search.flush_cache()
partitions_local = search.local_search(query, get_all_partitions()) partitions_local = search.local_search(query, get_all_partitions())
if "online-search" in request.form:
google_results = search.online_search(query) google_results = search.online_search(query)
return render_template("albums/search.html", partitions=partitions_local, google_results=google_results, query=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("/<uuid>") @bp.route("/<uuid>")
def album(uuid): def album(uuid):
@ -193,10 +205,25 @@ def add_partition(album_uuid):
error = None error = None
if "file" not in request.files: if "name" not in request.form:
error = "Aucun fichier n'a été fourni."
elif "name" not in request.form:
error = "Un titre est requis." 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: if error is not None:
flash(error) flash(error)
@ -224,8 +251,11 @@ def add_partition(album_uuid):
) )
db.commit() db.commit()
if partition_type == "file":
file = request.files["file"] file = request.files["file"]
file.save(f"partitioncloud/partitions/{partition_uuid}.pdf") 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( os.system(
f'/usr/bin/convert -thumbnail\ f'/usr/bin/convert -thumbnail\
@ -242,19 +272,64 @@ def add_partition(album_uuid):
""", """,
(album.uuid,) (album.uuid,)
).fetchone()["id"] ).fetchone()["id"]
db.execute(
"""
INSERT INTO contient_partition (partition_uuid, album_id)
VALUES (?, ?)
""",
(partition_uuid, album.id),
)
db.commit() db.commit()
album.add_partition(partition_uuid)
break break
except db.IntegrityError: except db.IntegrityError:
pass pass
flash(f"Partition {request.form['name']} ajoutée") flash(f"Partition {request.form['name']} ajoutée")
return redirect(f"/albums/{album.uuid}") 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")

View File

@ -42,16 +42,16 @@ def local_search(query, partitions):
def online_search(query): 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() db = get_db()
query = f"partition filetype:pdf {query}" query = f"partition filetype:pdf {query}"
partitions = [] partitions = []
results = googlesearch.search( results = googlesearch.search(
query, query,
num=5, num=3,
stop=5, stop=3,
pause=1 pause=0.2
) )
for element in results: for element in results:
while True: while True:

View File

@ -189,5 +189,22 @@ input[type=submit] {
#search-bar { #search-bar {
max-width: 50%; max-width: 50%;
margin-left: 25%; 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;
}

View File

@ -5,14 +5,18 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form method="post" enctype="multipart/form-data"> <form action="/albums/{{ album.uuid }}/add-partition" method="post" enctype="multipart/form-data">
Titre* Titre*
<input name="name" type="text" required /> <input name="name" type="text" required />
Auteur Auteur
<input name="author" type="text" /> <input name="author" type="text" />
Paroles Paroles
<textarea name="body" type="text"></textarea> <textarea name="body" type="text"></textarea>
{% if partition_uuid %}
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
{% else %}
<input name="file" type="file" required /> <input name="file" type="file" required />
{% endif %}
<input type="submit" value="Ajouter" /> <input type="submit" value="Ajouter" />
</form> </form>
{% endblock %} {% endblock %}

View File

@ -11,8 +11,11 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<form action="/albums/search" method="post" id="search"> <form action="/albums/search" method="post" id="search-form">
<input id="search-bar" type="search" name="query" placeholder="Rechercher" required> <input id="search-bar" type="search" name="query" placeholder="Rechercher" required>
<br/>
<input type="checkbox" id="online-search" name="online-search" value="online-search" checked="on">
<label for="online-search" id="online-search-label">Recherche en ligne</label>
</form> </form>
{% if albums|length != 0 %} {% if albums|length != 0 %}
{% for album in albums %} {% for album in albums %}

View File

@ -9,6 +9,7 @@
<h4>Résultats dans la bibliothèque locale</h4> <h4>Résultats dans la bibliothèque locale</h4>
<div id="partitions-grid"> <div id="partitions-grid">
{% for partition in partitions %} {% for partition in partitions %}
<div class="partition-container">
<a href="/partition/{{ partition['uuid'] }}"> <a href="/partition/{{ partition['uuid'] }}">
<div class="partition" id="partition-{{ partition['uuid'] }}"> <div class="partition" id="partition-{{ partition['uuid'] }}">
<img class="partition-thumbnail" src="/static/thumbnails/{{ partition['uuid'] }}.jpg"> <img class="partition-thumbnail" src="/static/thumbnails/{{ partition['uuid'] }}.jpg">
@ -18,6 +19,17 @@
</div> </div>
</div> </div>
</a> </a>
<form action="/albums/add-partition" method="post">
<select name="album-uuid">
{% for album in albums %}
<option value="{{ album['uuid'] }}">{{ album["name"] }}</option>
{% endfor %}
</select>
<input type="hidden" value="{{ partition['uuid'] }}" name="partition-uuid">
<input type="hidden" value="local_file" name="partition-type">
<input type="submit" value="Ajouter à l'album" class="add-to-album">
</form>
</div>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
@ -25,6 +37,7 @@
<h4>Résultats de la recherche en ligne</h4> <h4>Résultats de la recherche en ligne</h4>
<div id="partitions-grid"> <div id="partitions-grid">
{% for partition in google_results %} {% for partition in google_results %}
<div class="partition-container">
<a href="/partition/search/{{ partition['uuid'] }}"> <a href="/partition/search/{{ partition['uuid'] }}">
<div class="partition" id="partition-{{ partition['uuid'] }}"> <div class="partition" id="partition-{{ partition['uuid'] }}">
<img class="partition-thumbnail" src="/static/search-thumbnails/{{ partition['uuid'] }}.jpg"> <img class="partition-thumbnail" src="/static/search-thumbnails/{{ partition['uuid'] }}.jpg">
@ -33,6 +46,17 @@
</div> </div>
</div> </div>
</a> </a>
<form action="/albums/add-partition" method="post">
<select name="album-uuid">
{% for album in albums %}
<option value="{{ album['uuid'] }}">{{ album["name"] }}</option>
{% endfor %}
</select>
<input type="hidden" value="{{ partition['uuid'] }}" name="partition-uuid">
<input type="hidden" value="online_search" name="partition-type">
<input type="submit" value="Ajouter à l'album">
</form>
</div>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}

View File

@ -25,6 +25,8 @@ class User():
def is_participant(self, album_uuid): def is_participant(self, album_uuid):
db = get_db() db = get_db()
if self.access_level == 1:
return True
return len(db.execute( return len(db.execute(
""" """
SELECT album.id FROM album SELECT album.id FROM album
@ -193,6 +195,21 @@ class Album():
db.commit() 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(): def get_all_partitions():
db = get_db() db = get_db()
partitions = db.execute( partitions = db.execute(