Skip to content

Commit

Permalink
Add books_by_country (top 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Quiroz committed Jun 24, 2023
1 parent 6a42654 commit f646c7a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 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 @@ -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 Down

0 comments on commit f646c7a

Please sign in to comment.