From 439f4e873eb9facb616c3250ce743da0be78fdd6 Mon Sep 17 00:00:00 2001 From: Lucas Nunes <64300982+kilerhg@users.noreply.github.com> Date: Tue, 14 Sep 2021 23:57:21 -0300 Subject: [PATCH] Finishing CRUD Books, Updating Status. --- app.py | 47 ++++++++++++++++++++- dao.py | 13 ++++++ funcoes.py | 1 + templates/library.html | 95 ++++++++++++++++++++++++++++++++++++++---- templates/search.html | 4 +- 5 files changed, 148 insertions(+), 12 deletions(-) diff --git a/app.py b/app.py index 0e7c0da..46081d1 100644 --- a/app.py +++ b/app.py @@ -82,7 +82,6 @@ def search(): if request.method == "POST": livro = request.form["book_name"] lista_livros = funcoes.search_book(busca=livro) - print(lista_livros) return render_template('search.html', books=lista_livros) @@ -92,11 +91,18 @@ def library(): db, cursor = dao.connect_db() id_user = dict(session)['profile']['id'] list_books_id = dao.get_id_books_by_user_id(cursor=cursor, id_user=id_user) + dict_other_infos_book = dao.get_books_by_user_id(cursor=cursor, id_user=id_user) list_books = funcoes.get_books_by_id(list_books_id=list_books_id) + new_list_books = [] + for x in list_books: + x['id_status_book'] = dict_other_infos_book[x['book_id']]['id_status_book'] + x['percent_book'] = dict_other_infos_book[x['book_id']]['percent_book'] + new_list_books.append(x) + dict_books = {pos:book for pos, book in enumerate(new_list_books)} return render_template( 'library.html', user_session=dict(session)['profile'], - list_books=list_books, + dict_books=dict_books, ) @@ -113,6 +119,43 @@ def add_book_library(book_id): dao.insert_book_into_library_by_id(db=db, cursor=cursor, id_user=id_user, google_book_id=book_id) return redirect('/biblioteca') +@app.route("/biblioteca/update", methods=['POST']) +@login_required +def update_book_library(): + db, cursor = dao.connect_db() + + id_user = dict(session)['profile']['id'] + + status = request.form["status"] + book_id = request.form["book_id"] + percent = request.form['percent'] + + if percent == '': + percent = 0 + + + if int(status) in [0, 1, 2]: + status = int(status) + else: + status = 0 + + if int(percent) in list(range(101)) and int(status) not in [0, 2]: + percent = int(percent) + elif int(status) == 2: + percent = 100 + else: + percent = 0 + + + + + db, cursor = dao.connect_db() + id_register = dao.get_id_register(cursor=cursor, id_user=id_user, google_book_id=book_id) + dao.update_status_book_by_id(cursor, db, new_status_book=status, id_register=id_register) + dao.update_percent_book_by_id(cursor, db, new_percent=percent, id_register=id_register) + + return redirect('/biblioteca') + @app.route("/biblioteca/remove/") @login_required diff --git a/dao.py b/dao.py index c5d742f..db5407c 100644 --- a/dao.py +++ b/dao.py @@ -101,4 +101,17 @@ def get_id_books_by_user_id(cursor, id_user): valores = [book[0] for book in valores] else: valores = [] + return valores + +def get_books_by_user_id(cursor, id_user): + cursor.execute(f''' + select book_unique_key, id_status_book, percent_book from library + where id_user = '{id_user}' + ;''' + ) + valores = cursor.fetchall() + if valores: + valores = {book[0]:{'id_status_book':book[1], 'percent_book':book[2]} for book in valores} + else: + valores = {} return valores \ No newline at end of file diff --git a/funcoes.py b/funcoes.py index 1dab8ba..164cb09 100644 --- a/funcoes.py +++ b/funcoes.py @@ -110,5 +110,6 @@ def get_books_by_id(list_books_id : list): link_book = base_url+book dados = consumir_api(link_book) dados_limpos = limpa_requisicao_livro(livro=dados['volumeInfo'], book_id=dados['id']) + # dados_limpos[''] list_book_library.append(dados_limpos) return list_book_library \ No newline at end of file diff --git a/templates/library.html b/templates/library.html index f499af2..fc669a3 100644 --- a/templates/library.html +++ b/templates/library.html @@ -4,8 +4,10 @@ + Home - Brand + @@ -17,6 +19,15 @@ @@ -53,16 +64,17 @@

- Olá {{user_session['given_name']}}, Bem vindo á sua + Hi {{user_session['given_name']}}, Welcome To Your Self

- Biblioteca de Livros + Library

- Clique na capa do livro para ver mais, ou alternar status + Click on the book to change status

+

@@ -70,16 +82,28 @@




+ + +
+
+
+ × +

+

{{item['titulo']}}

+
+

+

Options

+
+ +
+ +
+
+ + +
+ +
+ +
+
+ +
+
+

+
+
+
+

To Do Book

+
+
+
+ {%endfor%} + + + + diff --git a/templates/search.html b/templates/search.html index c825b1c..6e2a4b9 100644 --- a/templates/search.html +++ b/templates/search.html @@ -53,10 +53,10 @@

- Livros + Books

- Clique na capa do livro para ver mais + Click on book to add to your library