diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index 0d9b0ec..736be2b 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -5,7 +5,7 @@ Main file import os from flask import Flask, render_template, request, send_file, g, redirect -from . import auth, albums +from . import auth, albums, partition app = Flask(__name__) @@ -18,6 +18,7 @@ app.config.from_mapping( app.register_blueprint(auth.bp) app.register_blueprint(albums.bp) +app.register_blueprint(partition.bp) @app.route("/") diff --git a/partitioncloud/albums.py b/partitioncloud/albums.py index b38215b..5f9f53a 100644 --- a/partitioncloud/albums.py +++ b/partitioncloud/albums.py @@ -10,7 +10,8 @@ from flask import (Blueprint, abort, flash, redirect, render_template, request, from .auth import login_required 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") @@ -24,6 +25,17 @@ def index(): 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("/") def album(uuid): """ diff --git a/partitioncloud/partition.py b/partitioncloud/partition.py new file mode 100644 index 0000000..08c17c4 --- /dev/null +++ b/partitioncloud/partition.py @@ -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("/") +@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")) \ No newline at end of file diff --git a/partitioncloud/search.py b/partitioncloud/search.py new file mode 100644 index 0000000..e310eeb --- /dev/null +++ b/partitioncloud/search.py @@ -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))] diff --git a/partitioncloud/static/style.css b/partitioncloud/static/style.css index 5cfd324..dc1bc32 100644 --- a/partitioncloud/static/style.css +++ b/partitioncloud/static/style.css @@ -180,4 +180,9 @@ input[type=submit] { #cancel-deletion { width: 133.333px; -} \ No newline at end of file +} + +#search-bar { + max-width: 50%; + margin-left: 25%; + } \ No newline at end of file diff --git a/partitioncloud/templates/albums/index.html b/partitioncloud/templates/albums/index.html index f500dc8..f530036 100644 --- a/partitioncloud/templates/albums/index.html +++ b/partitioncloud/templates/albums/index.html @@ -11,6 +11,9 @@ {% 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 new file mode 100644 index 0000000..57852f0 --- /dev/null +++ b/partitioncloud/templates/albums/search.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Résultats de la recherche "{{ query }}"{% endblock %}

+{% endblock %} + +{% block content %} +{% if partitions|length != 0 %} +
+{% else %} +
Aucune partition ne correspond aux termes de votre requête
+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/partitioncloud/templates/base.html b/partitioncloud/templates/base.html index cb9f773..af05df1 100644 --- a/partitioncloud/templates/base.html +++ b/partitioncloud/templates/base.html @@ -28,6 +28,5 @@ {% for message in get_flashed_messages() %}
{{ message }}
{% endfor %} -
{% block content %}{% endblock %} \ No newline at end of file diff --git a/partitioncloud/utils.py b/partitioncloud/utils.py index 1f61d08..201cec6 100644 --- a/partitioncloud/utils.py +++ b/partitioncloud/utils.py @@ -190,4 +190,14 @@ class Album(): ) """ ) - db.commit() \ No newline at end of file + db.commit() + + +def get_all_partitions(): + db = get_db() + partitions = db.execute( + """ + SELECT * FROM partition + """ + ) + return partitions \ No newline at end of file