mirror of
https://github.com/partitioncloud/partitioncloud-server.git
synced 2025-01-23 09:16:25 +01:00
Possibilité d'ajouter les partitions depuis la recherche
This commit is contained in:
parent
8ae4a5ea44
commit
89cec8ef35
@ -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("/<uuid>")
|
||||
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}")
|
||||
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")
|
@ -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:
|
||||
|
@ -189,5 +189,22 @@ input[type=submit] {
|
||||
|
||||
#search-bar {
|
||||
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;
|
||||
}
|
@ -5,14 +5,18 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<form action="/albums/{{ album.uuid }}/add-partition" method="post" enctype="multipart/form-data">
|
||||
Titre*
|
||||
<input name="name" type="text" required />
|
||||
Auteur
|
||||
<input name="author" type="text" />
|
||||
Paroles
|
||||
<textarea name="body" type="text"></textarea>
|
||||
<input name="file" type="file" required />
|
||||
{% if partition_uuid %}
|
||||
<input name="partition-uuid" value="{{ partition_uuid }}" type="hidden">
|
||||
{% else %}
|
||||
<input name="file" type="file" required />
|
||||
{% endif %}
|
||||
<input type="submit" value="Ajouter" />
|
||||
</form>
|
||||
{% endblock %}
|
@ -11,8 +11,11 @@
|
||||
{% endblock %}
|
||||
|
||||
{% 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>
|
||||
<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>
|
||||
{% if albums|length != 0 %}
|
||||
{% for album in albums %}
|
||||
|
@ -9,15 +9,27 @@
|
||||
<h4>Résultats dans la bibliothèque locale</h4>
|
||||
<div id="partitions-grid">
|
||||
{% for partition in partitions %}
|
||||
<a href="/partition/{{ partition['uuid'] }}">
|
||||
<div class="partition" id="partition-{{ partition['uuid'] }}">
|
||||
<img class="partition-thumbnail" src="/static/thumbnails/{{ partition['uuid'] }}.jpg">
|
||||
<div class="partition-description">
|
||||
<div class="partition-name">{{ partition["name"] }}</div>
|
||||
<div class="partition-author">{{ partition["author"] }}</div>
|
||||
<div class="partition-container">
|
||||
<a href="/partition/{{ partition['uuid'] }}">
|
||||
<div class="partition" id="partition-{{ partition['uuid'] }}">
|
||||
<img class="partition-thumbnail" src="/static/thumbnails/{{ partition['uuid'] }}.jpg">
|
||||
<div class="partition-description">
|
||||
<div class="partition-name">{{ partition["name"] }}</div>
|
||||
<div class="partition-author">{{ partition["author"] }}</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 %}
|
||||
</div>
|
||||
{% endif %}
|
||||
@ -25,14 +37,26 @@
|
||||
<h4>Résultats de la recherche en ligne</h4>
|
||||
<div id="partitions-grid">
|
||||
{% for partition in google_results %}
|
||||
<a href="/partition/search/{{ partition['uuid'] }}">
|
||||
<div class="partition" id="partition-{{ partition['uuid'] }}">
|
||||
<img class="partition-thumbnail" src="/static/search-thumbnails/{{ partition['uuid'] }}.jpg">
|
||||
<div class="partition-description">
|
||||
<div class="partition-name">{{ partition["name"] }}</div>
|
||||
<div class="partition-container">
|
||||
<a href="/partition/search/{{ partition['uuid'] }}">
|
||||
<div class="partition" id="partition-{{ partition['uuid'] }}">
|
||||
<img class="partition-thumbnail" src="/static/search-thumbnails/{{ partition['uuid'] }}.jpg">
|
||||
<div class="partition-description">
|
||||
<div class="partition-name">{{ partition["name"] }}</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 %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user