isbn-sort/app.py

39 lines
935 B
Python
Raw Normal View History

2024-04-03 16:54:54 +02:00
from flask import Flask, render_template, redirect, url_for, request, abort, g
import json
from book import Book
import isbn_db
app = Flask(__name__)
app.config["DATABASE"] = "data/books.sqlite"
@app.route("/submit-isbn")
def submit_isbn():
if "isbn" not in request.args:
return "/submit-isbn?isbn=xxxxxx"
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.after_request
def after_request(response):
"""Automatically close db after each request"""
if ('db' in g) and (g.db is not None):
g.db.close()
return response