diff --git a/isbn_sort/app.py b/isbn_sort/app.py index 523302e..93c75ed 100644 --- a/isbn_sort/app.py +++ b/isbn_sort/app.py @@ -42,6 +42,11 @@ def submit_isbn(): except IndexError: pass + if "no-search" in request.args: + isbn_db.add_book(book) + announce_book(book) + return "À rajouter manuellement" + try: book.load(loader="google_books", config=current_app.config) print("Got ", book, "[Google]") @@ -67,7 +72,11 @@ def web_submit_isbn(): @bp.route("/") def index(): - return render_template("index.html", books=isbn_db.get_all_books()) + return render_template( + "index.html", + books=isbn_db.get_all_books(), + categories=isbn_db.get_categories() + ) @bp.route("/delete-book", methods=["POST"]) @@ -88,7 +97,7 @@ def delete_book(): @bp.route("/update-book", methods=["POST"]) def update_book(): - attributes = ["isbn", "count", "title", "author", "publisher", "publish_date"] + attributes = ["isbn", "count", "title", "author", "publisher", "publish_date", "category"] if True in [i not in request.form for i in attributes]: return "missing an attribute" @@ -104,7 +113,8 @@ def update_book(): publisher=form_data["publisher"], publish_date=form_data["publish_date"], author=form_data["author"], - count=int(form_data["count"]) + count=int(form_data["count"]), + category=form_data["category"] ) isbn_db.update_book(book) announce_book(book, msg_type="update_book") diff --git a/isbn_sort/book.py b/isbn_sort/book.py index b273399..35a1417 100644 --- a/isbn_sort/book.py +++ b/isbn_sort/book.py @@ -18,6 +18,7 @@ class Book: self.publisher = None self.publish_date = None self.author = None + self.category = None self.count = -1 def _openlibrary_load(self, _): @@ -74,12 +75,13 @@ class Book: self.author = item["volumeInfo"]["authors"][0] - def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1): + def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1, category=None): self.title = title self.publisher = publisher self.publish_date = publish_date self.author = author self.count = count + self.category = category def load(self, config, loader="openlibrary"): if loader == "openlibrary": @@ -101,5 +103,6 @@ class Book: "publisher": self.publisher, "publish_date": self.publish_date, "author": self.author, - "count": self.count + "category": self.category, + "count": self.count, } \ No newline at end of file diff --git a/isbn_sort/isbn_db.py b/isbn_sort/isbn_db.py index 95c67bc..516a816 100644 --- a/isbn_sort/isbn_db.py +++ b/isbn_sort/isbn_db.py @@ -36,7 +36,8 @@ def get_book(isbn): publisher=data["publisher"], publish_date=data["publish_date"], author=data["author"], - count=data["count"] + count=data["count"], + category=data["category"] if data["category"] != "" else None ) return book @@ -77,10 +78,10 @@ def add_book(book): db = get_db() db.execute( """ - INSERT INTO book (isbn, count, title, author, publisher, publish_date) - VALUES (?, ?, ?, ?, ?, ?) + INSERT INTO book (isbn, count, title, author, publisher, publish_date, category) + VALUES (?, ?, ?, ?, ?, ?, ?) """, - (book.isbn, book.count, book.title, book.author, book.publisher, book.publish_date) + (book.isbn, book.count, book.title, book.author, book.publisher, book.publish_date, book.category) ) db.commit() return "added" @@ -89,10 +90,10 @@ def update_book(book): db = get_db() db.execute( """ - UPDATE book SET count=?, title=?, author=?, publisher=?, publish_date=? + UPDATE book SET count=?, title=?, author=?, publisher=?, publish_date=?, category=? WHERE isbn=? """, - (book.count, book.title, book.author, book.publisher, book.publish_date, book.isbn) + (book.count, book.title, book.author, book.publisher, book.publish_date, book.category, book.isbn) ) db.commit() return "updated" @@ -108,6 +109,8 @@ def get_all_books(): count += 1 if book.publisher is None: count += 1 + if book.category is None: + count += 1 return -count db = get_db() @@ -126,8 +129,20 @@ def get_all_books(): publisher=data_row["publisher"], publish_date=data_row["publish_date"], author=data_row["author"], - count=data_row["count"] + count=data_row["count"], + category=data_row["category"] if data_row["category"] != "" else None ) books.append(book) - return sorted(books, key=count_none) \ No newline at end of file + return sorted(books, key=count_none) + +def get_categories(): + db = get_db() + + data = db.execute( + """ + SELECT DISTINCT category FROM book + """ + ).fetchall() + + return [row["category"] for row in data] diff --git a/isbn_sort/schema.sql b/isbn_sort/schema.sql index c292837..985e746 100644 --- a/isbn_sort/schema.sql +++ b/isbn_sort/schema.sql @@ -6,5 +6,6 @@ CREATE TABLE book ( title TEXT, author TEXT, publisher TEXT, - publish_date TEXT + publish_date TEXT, + category TEXT DEFAULT '', ); \ No newline at end of file diff --git a/isbn_sort/static/dynamicUpdate.js b/isbn_sort/static/dynamicUpdate.js index f01100d..96ea2d3 100644 --- a/isbn_sort/static/dynamicUpdate.js +++ b/isbn_sort/static/dynamicUpdate.js @@ -39,6 +39,7 @@ function viewAddBook(book) { '
{{ book.author }}
{{ book.publish_date }}
{{ book.publisher }}
{{ book.category }}
{{ book.count }}