mirror of
https://github.com/partitioncloud/partitioncloud-server.git
synced 2025-01-23 17:26:26 +01:00
Ajout de la recherche en local
This commit is contained in:
parent
dfa3945a77
commit
b1052563f7
@ -5,7 +5,7 @@ Main file
|
|||||||
import os
|
import os
|
||||||
from flask import Flask, render_template, request, send_file, g, redirect
|
from flask import Flask, render_template, request, send_file, g, redirect
|
||||||
|
|
||||||
from . import auth, albums
|
from . import auth, albums, partition
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@ -18,6 +18,7 @@ app.config.from_mapping(
|
|||||||
|
|
||||||
app.register_blueprint(auth.bp)
|
app.register_blueprint(auth.bp)
|
||||||
app.register_blueprint(albums.bp)
|
app.register_blueprint(albums.bp)
|
||||||
|
app.register_blueprint(partition.bp)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
@ -10,7 +10,8 @@ from flask import (Blueprint, abort, flash, redirect, render_template, request,
|
|||||||
|
|
||||||
from .auth import login_required
|
from .auth import login_required
|
||||||
from .db import get_db
|
from .db import get_db
|
||||||
from .utils import User, Album
|
from .utils import User, Album, get_all_partitions
|
||||||
|
from .search import search as srch
|
||||||
|
|
||||||
bp = Blueprint("albums", __name__, url_prefix="/albums")
|
bp = Blueprint("albums", __name__, url_prefix="/albums")
|
||||||
|
|
||||||
@ -24,6 +25,17 @@ def index():
|
|||||||
return render_template("albums/index.html", albums=albums)
|
return render_template("albums/index.html", albums=albums)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/search", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def search():
|
||||||
|
if "query" not in request.form or request.form["query"] == "":
|
||||||
|
flash("Aucun terme de recherche spécifié.")
|
||||||
|
return redirect("/albums")
|
||||||
|
|
||||||
|
query = request.form["query"]
|
||||||
|
partitions = srch(query, get_all_partitions())
|
||||||
|
return render_template("albums/search.html", partitions=partitions, query=query)
|
||||||
|
|
||||||
@bp.route("/<uuid>")
|
@bp.route("/<uuid>")
|
||||||
def album(uuid):
|
def album(uuid):
|
||||||
"""
|
"""
|
||||||
|
28
partitioncloud/partition.py
Normal file
28
partitioncloud/partition.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
Partition module
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
from flask import Blueprint, abort, send_file
|
||||||
|
|
||||||
|
from .db import get_db
|
||||||
|
from .auth import login_required
|
||||||
|
|
||||||
|
|
||||||
|
bp = Blueprint("partition", __name__, url_prefix="/partition")
|
||||||
|
|
||||||
|
@bp.route("/<uuid>")
|
||||||
|
@login_required
|
||||||
|
def partition(uuid):
|
||||||
|
db = get_db()
|
||||||
|
partition = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT * FROM partition
|
||||||
|
WHERE uuid = ?
|
||||||
|
""",
|
||||||
|
(uuid,)
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
if partition is None:
|
||||||
|
abort(404)
|
||||||
|
return send_file(os.path.join("partitions", f"{uuid}.pdf"))
|
32
partitioncloud/search.py
Normal file
32
partitioncloud/search.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
Module implémentant la recherche de partitions par mots-clés
|
||||||
|
"""
|
||||||
|
|
||||||
|
def search(query, partitions):
|
||||||
|
"""
|
||||||
|
Renvoie les 5 résultats les plus pertinents parmi une liste donnée
|
||||||
|
"""
|
||||||
|
def score_attribution(partition):
|
||||||
|
score = 0
|
||||||
|
for word in query.split(" "):
|
||||||
|
if word != "":
|
||||||
|
low_word = word.lower()
|
||||||
|
if low_word in partition["name"].lower():
|
||||||
|
score += 3
|
||||||
|
elif low_word in partition["body"].lower():
|
||||||
|
score += 1
|
||||||
|
elif low_word in partition["author"].lower():
|
||||||
|
score += 2
|
||||||
|
else:
|
||||||
|
score -= .5
|
||||||
|
return score
|
||||||
|
|
||||||
|
partitions = sorted(partitions, key=score_attribution, reverse=True)
|
||||||
|
sorted_partitions = []
|
||||||
|
for partition in partitions:
|
||||||
|
if score_attribution(partition) > 0:
|
||||||
|
sorted_partitions.append(partition)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return sorted_partitions[:min(5,len(sorted_partitions))]
|
@ -180,4 +180,9 @@ input[type=submit] {
|
|||||||
|
|
||||||
#cancel-deletion {
|
#cancel-deletion {
|
||||||
width: 133.333px;
|
width: 133.333px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#search-bar {
|
||||||
|
max-width: 50%;
|
||||||
|
margin-left: 25%;
|
||||||
|
}
|
@ -11,6 +11,9 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
<form action="/albums/search" method="post" id="search">
|
||||||
|
<input id="search-bar" type="search" name="query" placeholder="Rechercher" required>
|
||||||
|
</form>
|
||||||
{% if albums|length != 0 %}
|
{% if albums|length != 0 %}
|
||||||
{% for album in albums %}
|
{% for album in albums %}
|
||||||
<a href="{{ album['uuid'] }}">
|
<a href="{{ album['uuid'] }}">
|
||||||
|
25
partitioncloud/templates/albums/search.html
Normal file
25
partitioncloud/templates/albums/search.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block header %}
|
||||||
|
<h1>{% block title %}Résultats de la recherche "{{ query }}"{% endblock %}</h1>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% if partitions|length != 0 %}
|
||||||
|
<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>
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div>Aucune partition ne correspond aux termes de votre requête</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
@ -28,6 +28,5 @@
|
|||||||
{% for message in get_flashed_messages() %}
|
{% for message in get_flashed_messages() %}
|
||||||
<div class="flash">{{ message }}</div>
|
<div class="flash">{{ message }}</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<br />
|
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</section>
|
</section>
|
@ -190,4 +190,14 @@ class Album():
|
|||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_partitions():
|
||||||
|
db = get_db()
|
||||||
|
partitions = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT * FROM partition
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return partitions
|
Loading…
Reference in New Issue
Block a user