Skip to content

Commit

Permalink
Merge pull request #28 from boris/ft/add-genre-page
Browse files Browse the repository at this point in the history
Feature: Add genre page
  • Loading branch information
Boris Quiroz authored Oct 9, 2022
2 parents c56e3de + e4dbb18 commit 7a071dd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 73 deletions.
165 changes: 93 additions & 72 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,6 @@
def index():
return render_template('index.html')

@main.route('/profile')
def profile():
if current_user.is_authenticated:
books_total = db.session.query(Book.id).filter(Book.id_user == current_user.id).count()
books_read = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.read == True)).count()
books_unread = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.read == False)).count()
books_shared = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.shared == True)).count()

return render_template('profile.html',
name = current_user.name,
greeting = current_user.name,
books_total = books_total,
books_read = books_read,
books_unread = books_unread,
books_shared = books_shared,
)
else:
return redirect(url_for('auth.login'))

@main.route('/my_books')
def books():
if current_user.is_authenticated:
filtered_books = db.session.query(Book.id, Book.title, Book.rating, Book.id_author, Author.name.label('author_name'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.join(Author)\
.join(Editorial)\
.join(Genre)\
.filter((Book.id_author == Author.id) & (Book.id_editorial == Editorial.id) & (Book.id_user == current_user.id))\
.order_by(Author.name.asc())

return render_template('my_books.html',
greeting = current_user.name,
books = filtered_books,
)
else:
# Fix this route!!
return redirect(url_for('main.login'))


@main.route('/show_author/<int:author_id>')
def show_author(author_id):
if current_user.is_authenticated:
current_author = db.session.query(Author.name, Author.country).filter(Author.id == author_id)
author_books = db.session.query(Book.id, Book.title, Book.year, Book.pages, Book.rating, Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id_author == author_id)\
.join(Author)\
.join(Editorial)\
.join(Genre)

return render_template('show_author.html',
greeting = current_user.name,
author = current_author,
books = author_books,
)


@main.route('/show_book/<int:book_id>')
def show_book(book_id):
if current_user.is_authenticated:
current_book = db.session.query(Book.title, Book.year, Book.pages, Book.read, Book.shared, Book.rating, Author.name.label('author_name'), Author.country.label('author_country'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id == book_id)\
.join(Author)\
.join(Editorial)\
.join(Genre)

return render_template('show_book.html',
greeting = current_user.name,
book = current_book,
)

else:
return redirect(url_for('main.login'))


@main.route('/add_book')
def add_book():
Expand Down Expand Up @@ -173,6 +101,99 @@ def add_book_post():
return redirect(url_for('auth.login'))


@main.route('/my_books')
def books():
if current_user.is_authenticated:
filtered_books = db.session.query(Book.id, Book.title, Book.rating, Book.id_author, Book.id_genre, Author.name.label('author_name'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.join(Author)\
.join(Editorial)\
.join(Genre)\
.filter((Book.id_author == Author.id) & (Book.id_editorial == Editorial.id) & (Book.id_user == current_user.id))\
.order_by(Author.name.asc())

return render_template('my_books.html',
greeting = current_user.name,
books = filtered_books,
)
else:
# Fix this route!!
return redirect(url_for('main.login'))


@main.route('/profile')
def profile():
if current_user.is_authenticated:
books_total = db.session.query(Book.id).filter(Book.id_user == current_user.id).count()
books_read = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.read == True)).count()
books_unread = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.read == False)).count()
books_shared = db.session.query(Book.id).filter((Book.id_user == current_user.id) & (Book.shared == True)).count()

return render_template('profile.html',
name = current_user.name,
greeting = current_user.name,
books_total = books_total,
books_read = books_read,
books_unread = books_unread,
books_shared = books_shared,
)
else:
return redirect(url_for('auth.login'))


@main.route('/show_author/<int:author_id>')
def show_author(author_id):
if current_user.is_authenticated:
current_author = db.session.query(Author.name, Author.country).filter(Author.id == author_id)
author_books = db.session.query(Book.id, Book.title, Book.year, Book.pages, Book.rating, Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id_author == author_id)\
.join(Author)\
.join(Editorial)\
.join(Genre)

return render_template('show_author.html',
greeting = current_user.name,
author = current_author,
books = author_books,
)


@main.route('/show_book/<int:book_id>')
def show_book(book_id):
if current_user.is_authenticated:
current_book = db.session.query(Book.title, Book.year, Book.pages, Book.read, Book.shared, Book.rating, Author.name.label('author_name'), Author.country.label('author_country'), Editorial.name.label('editorial_name'), Genre.name.label('genre_name'))\
.filter(Book.id == book_id)\
.join(Author)\
.join(Editorial)\
.join(Genre)

return render_template('show_book.html',
greeting = current_user.name,
book = current_book,
)

else:
return redirect(url_for('main.login'))


@main.route('/show_genre/<int:genre_id>')
def show_genre(genre_id):
if current_user.is_authenticated:
current_genre = db.session.query(Genre.name).filter(Genre.id == genre_id)
genre_books = db.session.query(Book.id, Book.title, Book.id_author, Book.rating, Editorial.name.label('editorial_name'), Author.name.label('author_name'))\
.filter(Book.id_genre == genre_id)\
.join(Author)\
.join(Editorial)\
.join(Genre)

return render_template('show_genre.html',
greeting = current_user.name,
genre = current_genre,
books = genre_books,
)

else:
return redirect(url_for('main.login'))

@main.route('/test_utf8/')
def test_utf8():
editorial_list = db.session.query(Editorial.name).filter(Editorial.id == 10).first()
Expand Down
2 changes: 1 addition & 1 deletion app/templates/my_books.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1 class="title">Mis Libros</h1>
<th><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{ book.author_name }}</a></th>
<th><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></th>
<th>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</th>
<th>{{ book.genre_name }}</th>
<th><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></th>
<th>{{ book.editorial_name }}</th>
</tr>
{% endfor %}
Expand Down
28 changes: 28 additions & 0 deletions app/templates/show_genre.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends 'base.html' %}

{% block content %}
<div class="hero-body">
<div class="container has-text-justified is-fluid">
{% for g in genre %}
<h1 class="Title">{{ g.name }}</h1>
{% endfor %}
<br>
<table class="table is-fullwidth">
<tr class="has-background-success-light">
<th>Título</th>
<th>Autor</th>
<th>Puntuación</th>
<th>Editorial</th>
</tr>
{% for book in books %}
<tr>
<th><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></th>
<th><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{book.author_name}}</a></th>
<th>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</th>
<th>{{ book.editorial_name }}</th>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endblock %}

0 comments on commit 7a071dd

Please sign in to comment.