Skip to content

Commit

Permalink
Merge pull request #44 from boris/ft/add-country-page
Browse files Browse the repository at this point in the history
Ft/add country page
  • Loading branch information
Boris Quiroz authored Jun 24, 2023
2 parents c54e94f + f646c7a commit 6ac187c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 39 deletions.
38 changes: 34 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import Blueprint, render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func, desc

# This is for debug
import traceback
Expand Down Expand Up @@ -35,11 +36,11 @@ def add_book():
db.session.commit()
id_author = db.session.query(Author.id).filter(Author.name == author_name)

id_author = db.session.query(Author.id).filter(Author.name == author_name)
id_author = db.session.query(Author.id).filter(Author.name == author_name)

# Check editorial details and existence
editorial_name = request.form['editorial_name']
editorial_exists = Editorial.query.filter_by(name=editorial_name).first()
# Check editorial details and existence
editorial_name = request.form['editorial_name']
editorial_exists = Editorial.query.filter_by(name=editorial_name).first()

if not editorial_exists:
editorial = Editorial(name=editorial_name)
Expand Down Expand Up @@ -186,6 +187,16 @@ def profile():
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()

# Queries for data analysis
books_by_country_subquery = db.session.query(Book.id_author).filter(Book.id_user == current_user.id).subquery()
books_by_country_query = db.session.query(Author.country, func.count()).\
filter(Author.id.in_(books_by_country_subquery)).\
group_by(Author.country).\
order_by(func.count().desc()).\
limit(5)
books_by_country = [(row[0], row[1]) for row in books_by_country_query.all()]


return render_template('profile.html',
author = author,
quote = quote,
Expand All @@ -195,6 +206,7 @@ def profile():
books_read = books_read,
books_unread = books_unread,
books_shared = books_shared,
books_by_country = books_by_country,
)


Expand All @@ -215,6 +227,24 @@ def show_author(author_id):
)


@main.route('/show_country/<string:author_country>')
@login_required
def show_country(author_country):
author_country = author_country.lower()
authors = db.session.query(Author.id, Author.name)\
.filter(Author.country == author_country)\
.filter(Book.id_author == Author.id)\
.filter(Book.id_user == current_user.id)\
.join(Book)\
.order_by(Author.name.asc())

return render_template('show_country.html',
greeting = current_user.name,
author_country = author_country,
authors = authors,
)


@main.route('/show_editorial/<int:editorial_id>')
@login_required
def show_editorial(editorial_id):
Expand Down
40 changes: 18 additions & 22 deletions app/templates/my_books.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@
<h1 class="title">Mis Libros</h1>
<br>

<div class="table-container">
<table class="table is-fullwidth">
<tbody>
<tr class="has-background-success-light">
<th>Título</th>
<th>Autor</th>
<th>Puntuación</th>
<th>Género</th>
<th>Editorial</th>
</tr>
{% for book in books|sort(attribute='title') %}
<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><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></th>
<th><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial ) }}">{{ book.editorial_name }}</a></th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<table class="table is-fullwidth">
<tr class="has-background-success-light">
<th>Título</th>
<th>Autor</th>
<th>Puntuación</th>
<th>Género</th>
<th>Editorial</th>
</tr>
{% for book in books|sort(attribute='title') %}
<tr>
<td><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></td>
<td><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{ book.author_name }}</a></td>
<td>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</td>
<td><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></td>
<td><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial ) }}">{{ book.editorial_name }}</a></td>
</tr>
{% endfor %}
</table>

</div>
</div>
Expand Down
18 changes: 17 additions & 1 deletion app/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,23 @@ <h2>Estadísticas.</h2>

<br>

<h2>Análisis de Datos</h2>
<h2>Análisis de Datos.</h2>

<h3>Cantidad de libros por país de author</h3>
<table class="table is-halfwidth is-stripped">
<tbody>
<tr class="has-background-success-light">
<th>País</th>
<th>Cantidad</th>
</tr>
{% for country, count in books_by_country|sort(attribute='count_1') %}
<tr>
<td><a href="{{ url_for('main.show_country', author_country = country) }}">{{ country }}</a></td>
<td>{{ count }}</td>
</tr>
{% endfor %}
</tbody>
</table>

</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions app/templates/show_author.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ <h3>{{a.country}}</h3>
</tr>
{% for book in books %}
<tr>
<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><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></th>
<th><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial) }}">{{ book.editorial_name }}</a></th>
<td><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></td>
<td>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</td>
<td><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></td>
<td><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial) }}">{{ book.editorial_name }}</a></td>
</tr>
{% endfor %}
</table>
Expand Down
20 changes: 20 additions & 0 deletions app/templates/show_country.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'base.html' %}

{% block content %}
<div class="hero-body">
<div class="container has-text-justified is-fluid">
<h1 class="Title">{{ author_country|upper }}</h1>
<br>
<table class="table is-fullwidth">
<tr class="has-background-success-light">
<th>Autores</th>
</tr>
{% for author in authors|sort(attribute='name') %}
<tr>
<td><a href="{{ url_for('main.show_author', author_id = author.id) }}">{{ author.name }}</a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endblock %}
8 changes: 4 additions & 4 deletions app/templates/show_editorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ <h1 class="Title">{{e.name}}</h1>
</tr>
{% for book in books %}
<tr>
<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><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{ book.author_name }}</a></th>
<th><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></th>
<td><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></td>
<td>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</td>
<td><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{ book.author_name }}</a></td>
<td><a href="{{ url_for('main.show_genre', genre_id = book.id_genre) }}">{{ book.genre_name }}</a></td>
</tr>
{% endfor %}
</table>
Expand Down
8 changes: 4 additions & 4 deletions app/templates/show_genre.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ <h1 class="Title">{{ g.name }}</h1>
</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><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial) }}">{{ book.editorial_name }}</a></th>
<td><a href="{{ url_for('main.show_book', book_id = book.id) }}">{{ book.title }}</a></td>
<td><a href="{{ url_for('main.show_author', author_id = book.id_author) }}">{{book.author_name}}</a></td>
<td>{{ book.rating * '<i class="bi bi-star-fill"></i>' |safe }}</td>
<td><a href="{{ url_for('main.show_editorial', editorial_id = book.id_editorial) }}">{{ book.editorial_name }}</a></td>
</tr>
{% endfor %}
</table>
Expand Down

0 comments on commit 6ac187c

Please sign in to comment.