Update paths

This commit is contained in:
augustin64 2024-04-05 17:24:02 +02:00
parent cd6c0b1c4d
commit 42cc2359a5
4 changed files with 102 additions and 88 deletions

View File

@ -15,10 +15,10 @@ Scan:
Transfert: Transfert:
- [x] Transférer les scans - [x] Transférer les scans
- URL de transfert: `http://<addresse du serveur>/submit-isbn?isbn=` - URL de transfert: `http://<addresse du serveur>/app/submit-isbn?isbn=`
- Type de requête: `GET` et ajouter simplement le contenu - 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://<addresse du serveur>/`) Vous recevrez un macaron vous indiquant si il faut finir de taper les données à la main sur l'interface web (`http://<addresse du serveur>/app/`)
> Attention, cela suppose que le serveur expose ses ports, pour cela, utilisez la commande suivante: > Attention, cela suppose que le serveur expose ses ports, pour cela, utilisez la commande suivante:
> `flask run --host=0.0.0.0 --port=5000` > `flask run --host=0.0.0.0 --port=5000`

View File

@ -1,94 +1,20 @@
from flask import Flask, render_template, redirect, request, g, flash from flask import Flask, redirect, g
import secrets import secrets
import json
from .book import Book from .app import bp as app_bp
from . import isbn_db, sse
app = Flask(__name__) app = Flask(__name__)
app.config["DATABASE"] = "data/books.sqlite" app.config["DATABASE"] = "data/books.sqlite"
app.config["SECRET_KEY"] = secrets.token_hex() app.config["SECRET_KEY"] = secrets.token_hex()
@app.route("/submit-isbn") app.register_blueprint(app_bp)
def submit_isbn():
if "isbn" not in request.args:
return "/submit-isbn?isbn=xxxxxx"
if request.args["isbn"] == "": @app.route("/<path>")
return "Pas d'ISBN spécifié" def handle_redirect(path):
if not path.startswith("/app"):
try: return redirect("/app"+path, 301)
book = Book(request.args["isbn"]) abort(404)
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.after_request @app.after_request
def after_request(response): def after_request(response):

88
isbn_sort/app.py Normal file
View File

@ -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"}
)

View File

@ -8,7 +8,7 @@
{% block content %} {% block content %}
<dialog id="edit-book-dialog"> <dialog id="edit-book-dialog">
<form id="edit-book-form" action="/update-book" method="post"> <form id="edit-book-form" action="/app/update-book" method="post">
<input type="hidden" id="edit-isbn" value="" name="isbn"> <input type="hidden" id="edit-isbn" value="" name="isbn">
<label for="edit-title">Titre:</label><br> <label for="edit-title">Titre:</label><br>
<input type="text" id="edit-title" name="title"><br> <input type="text" id="edit-title" name="title"><br>
@ -27,7 +27,7 @@
<dialog id="delete-book-dialog"> <dialog id="delete-book-dialog">
<p>Êtes-vous sûr de supprimer ce livre ?</p> <p>Êtes-vous sûr de supprimer ce livre ?</p>
<b id="delete-book-name">Nom du livre...</b> <b id="delete-book-name">Nom du livre...</b>
<form id="delete-book-form" action="/delete-book" method="post"> <form id="delete-book-form" action="/app/delete-book" method="post">
<input type="hidden" id="delete-isbn" value="" name="isbn"> <input type="hidden" id="delete-isbn" value="" name="isbn">
<input type="submit" value="Oui"> <input type="submit" value="Oui">
</form> </form>
@ -35,7 +35,7 @@
</dialog> </dialog>
<div id="add-book"> <div id="add-book">
Ajouter manuellement Ajouter manuellement
<form action="web-submit-isbn"> <form action="/app/web-submit-isbn">
<input type="text" name="isbn" placeholder="ISBN"> <input type="text" name="isbn" placeholder="ISBN">
<input type="submit" value="Ajouter"> <input type="submit" value="Ajouter">
</form> </form>
@ -68,5 +68,5 @@
</table> </table>
</div> </div>
<br/> <br/>
<a href="/export-csv" download="books.csv">Exporter en CSV</a> <a href="/app/export-csv" download="books.csv">Exporter en CSV</a>
{% endblock %} {% endblock %}