Skip to content

Commit

Permalink
Finishing CRUD Books, Updating Status.
Browse files Browse the repository at this point in the history
  • Loading branch information
kilerhg committed Sep 15, 2021
1 parent 333d0d2 commit 439f4e8
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 12 deletions.
47 changes: 45 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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,
)


Expand All @@ -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/<book_id>")
@login_required
Expand Down
13 changes: 13 additions & 0 deletions dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions funcoes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
95 changes: 87 additions & 8 deletions templates/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">

<title>Home - Brand</title>
<!-- <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"> -->
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href=" {{url_for('static', filename='bootstrap/css/bootstrap.min.css')}} ">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic">
Expand All @@ -17,6 +19,15 @@

<style>
a { text-decoration: none; }
#texto {
color: white;
position: absolute;
margin-top: -250px;
z-index:1;
}
#black {
color: black;
}
</style>

</head>
Expand Down Expand Up @@ -53,33 +64,46 @@
<div class="col-sm-12">
<div class="title-box text-center">
<h3 class="title-a">
Olá {{user_session['given_name']}}, Bem vindo á sua
Hi {{user_session['given_name']}}, Welcome To Your Self
</h3>
<h3 class="title-a">
Biblioteca de Livros
Library
</h3>
<p class="subtitle-a">
Clique na capa do livro para ver mais, ou alternar status
Click on the book to change status
</p>
<div class="line-mf"></div>
</div>

</div>
</div>
</br>
</br>
</br>
</br>
</br>

<div class="row">



{% for item in list_books%}
<div class="col-md-2">
{% for pos, item in dict_books.items() %}

<div class="col-md-2" onclick="document.getElementById('id{{pos}}').style.display='block'" >
<div class="work-box">
<a href=" /biblioteca/{{ item['book_id'] }}" target="_blank">
<a target="_blank" >
<div class="work-img">
<img src=" {{ item['imagem'] }} " alt="" class="img-fluid" style="width: 210px; height: 250px; margin: 0px;">
</div>
{% if item['id_status_book'] == 0 %}
<div id="texto">Not Started</div>
{% elif item['id_status_book'] == 1 %}
<div id="texto">Reading</div>
{% elif item['id_status_book'] == 2 %}
<div id="texto">Readed</div>
{% else %}
<div id="texto">Undefined</div>
{% endif %}
<div class="work-content">
<div class="row">
<div class="col-sm-12">
Expand All @@ -91,13 +115,68 @@ <h2 class="w-title"> {{ item['titulo'] }} </h2>
</div>
</div>
</div>
</div>
{% if item['id_status_book'] == 1 %}
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: {{ item['percent_book'] }}%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
{% endif %}
</a>

</div>
</div>
</div>

<div id="id{{pos}}" class="w3-modal">
<div class="w3-modal-content w3-animate-right w3-card-4">
<header class="w3-container w3-teal">
<span onclick="document.getElementById('id{{pos}}').style.display='none'"
class="w3-button w3-display-topright">&times;</span>
<p></p>
<h3>{{item['titulo']}}</h3>
</header>
<p></p>
<p>Options</p>
<div class="w3-container">

<form action="{{url_for('update_book_library')}}", method="POST">
<input type="text" id="book_id" name="book_id" value="{{ item['book_id'] }}" hidden/>
<div class="row">
<div class="col-md-6">
<label for="status" ><a class="black"> Status</a></label>
<select name="status" id="status">
<option value="">Select</option>
<option value="0">To Read</option>
<option value="1">Reading</option>
<option value="2">Readed</option>
</select>
</div>
<div class="col-md-6" class="black">
<label for="percent" class="black"><a class="black"> Porcentagem</a></label>
<input type="number" id="percent" name="percent" min="0" max="100">
</div>
</div>

<div class="row">
<div class="col-md-12">
<input type="submit" value="Enviar">
</div>
</div>
<p></p>
</form>
</div>
<footer class="w3-container w3-teal">
<p>To Do Book</p>
</footer>
</div>
</div>


{%endfor%}







</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
<div class="col-sm-12">
<div class="title-box text-center">
<h3 class="title-a">
Livros
Books
</h3>
<p class="subtitle-a">
Clique na capa do livro para ver mais
Click on book to add to your library
</p>
<div class="line-mf"></div>
</div>
Expand Down

0 comments on commit 439f4e8

Please sign in to comment.