diff --git a/README.md b/README.md index e51c9c2..d399b8f 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ Scan: Transfert: - [x] Transférer les scans -- URL de transfert: `http:///submit-isbn?isbn=` +- URL de transfert: `http:///app/submit-isbn?isbn=` - Type de requête: `GET` et ajouter simplement le contenu -Vous recevrez un macaron vous indiquant si il faut finir de taper les données à la main sur l'interface web (`http:///`) +Vous recevrez un macaron vous indiquant si il faut finir de taper les données à la main sur l'interface web (`http:///app/`) > Attention, cela suppose que le serveur expose ses ports, pour cela, utilisez la commande suivante: > `flask run --host=0.0.0.0 --port=5000` \ No newline at end of file diff --git a/isbn_sort/__init__.py b/isbn_sort/__init__.py index 80f2457..91937d9 100644 --- a/isbn_sort/__init__.py +++ b/isbn_sort/__init__.py @@ -1,94 +1,20 @@ -from flask import Flask, render_template, redirect, request, g, flash +from flask import Flask, redirect, g import secrets -import json -from .book import Book -from . import isbn_db, sse +from .app import bp as app_bp app = Flask(__name__) app.config["DATABASE"] = "data/books.sqlite" app.config["SECRET_KEY"] = secrets.token_hex() -@app.route("/submit-isbn") -def submit_isbn(): - if "isbn" not in request.args: - return "/submit-isbn?isbn=xxxxxx" +app.register_blueprint(app_bp) - if request.args["isbn"] == "": - return "Pas d'ISBN spécifié" - - try: - book = Book(request.args["isbn"]) - except ValueError: - return "Pas le bon format de QR code" - - try: - book.load() - except KeyError: - isbn_db.add_book(book) - return "Pas trouvé, à rajouter manuellement" - - print("Got ", book) - if isbn_db.add_book(book) == "duplicate": - return f"{book.title} ajouté (plusieurs occurrences)" - return f"{book.title} ajouté" - -@app.route("/web-submit-isbn") -def web_submit_isbn(): - flash(submit_isbn()) - return redirect("/") - -@app.route("/") -def index(): - return render_template("index.html", books=isbn_db.get_all_books()) - - -@app.route("/delete-book", methods=["POST"]) -def delete_book(): - if "isbn" not in request.form: - return "missing isbn" - - isbn_db.delete_book(request.form["isbn"]) - return redirect("/") - -@app.route("/update-book", methods=["POST"]) -def update_book(): - attributes = ["isbn", "count", "title", "author", "publisher", "publish_date"] - if True in [i not in request.form for i in attributes]: - return "missing an attribute" - - form_data = request.form.copy() - - for attribute in attributes: - if form_data[attribute] == "None": - form_data[attribute] = None - - book = Book(form_data["isbn"]) - book._manual_load( - form_data["title"], - publisher=form_data["publisher"], - publish_date=form_data["publish_date"], - author=form_data["author"], - count=int(form_data["count"]) - ) - isbn_db.update_book(book) - return redirect("/") - -@app.route("/export-csv") -def export_csv(): - books = isbn_db.get_all_books() - - csv = "ISBN;Titre;Auteur;Éditeur;Date;Nombre\n" - for book in books: - csv += f"{book.isbn};{book.title};{book.author};{book.publisher};{book.publish_date};{book.count}\n" - - # return as file with a good filename - return app.response_class( - csv, - mimetype="text/csv", - headers={"Content-Disposition": "attachment;filename=books.csv"} - ) +@app.route("/") +def handle_redirect(path): + if not path.startswith("/app"): + return redirect("/app"+path, 301) + abort(404) @app.after_request def after_request(response): diff --git a/isbn_sort/app.py b/isbn_sort/app.py new file mode 100644 index 0000000..60228ed --- /dev/null +++ b/isbn_sort/app.py @@ -0,0 +1,88 @@ +from flask import render_template, redirect, request, flash, Blueprint, current_app, url_for + +import json + +from .book import Book +from . import isbn_db + +bp = Blueprint("app", __name__, url_prefix="/app") + +@bp.route("/submit-isbn") +def submit_isbn(): + if "isbn" not in request.args: + return "/submit-isbn?isbn=xxxxxx" + + if request.args["isbn"] == "": + return "Pas d'ISBN spécifié" + + try: + book = Book(request.args["isbn"]) + except ValueError: + return "Pas le bon format de QR code" + + try: + book.load() + except KeyError: + isbn_db.add_book(book) + return "Pas trouvé, à rajouter manuellement" + + print("Got ", book) + if isbn_db.add_book(book) == "duplicate": + return f"{book.title} ajouté (plusieurs occurrences)" + return f"{book.title} ajouté" + +@bp.route("/web-submit-isbn") +def web_submit_isbn(): + flash(submit_isbn()) + return redirect(url_for("app.index")) + +@bp.route("/") +def index(): + return render_template("index.html", books=isbn_db.get_all_books()) + + +@bp.route("/delete-book", methods=["POST"]) +def delete_book(): + if "isbn" not in request.form: + return "missing isbn" + + isbn_db.delete_book(request.form["isbn"]) + return redirect(url_for("app.index")) + +@bp.route("/update-book", methods=["POST"]) +def update_book(): + attributes = ["isbn", "count", "title", "author", "publisher", "publish_date"] + if True in [i not in request.form for i in attributes]: + return "missing an attribute" + + form_data = request.form.copy() + + for attribute in attributes: + if form_data[attribute] == "None": + form_data[attribute] = None + + book = Book(form_data["isbn"]) + book._manual_load( + form_data["title"], + publisher=form_data["publisher"], + publish_date=form_data["publish_date"], + author=form_data["author"], + count=int(form_data["count"]) + ) + isbn_db.update_book(book) + return redirect(url_for("app.index")) + +@bp.route("/export-csv") +def export_csv(): + books = isbn_db.get_all_books() + + csv = "ISBN;Titre;Auteur;Éditeur;Date;Nombre\n" + for book in books: + csv += f"{book.isbn};{book.title};{book.author};{book.publisher};{book.publish_date};{book.count}\n" + + # return as file with a good filename + return current_app.response_class( + csv, + mimetype="text/csv", + headers={"Content-Disposition": "attachment;filename=books.csv"} + ) \ No newline at end of file diff --git a/isbn_sort/templates/index.html b/isbn_sort/templates/index.html index 25ab85b..7b19c28 100644 --- a/isbn_sort/templates/index.html +++ b/isbn_sort/templates/index.html @@ -8,7 +8,7 @@ {% block content %} -
+

@@ -27,7 +27,7 @@

Êtes-vous sûr de supprimer ce livre ?

Nom du livre... - + @@ -35,7 +35,7 @@
Ajouter manuellement -
+
@@ -68,5 +68,5 @@

-Exporter en CSV +Exporter en CSV {% endblock %}