From f646c7a7de8c15be58483f2a0cf512473c98c712 Mon Sep 17 00:00:00 2001 From: Boris Quiroz Date: Sat, 24 Jun 2023 00:26:57 -0400 Subject: [PATCH] Add books_by_country (top 5) --- app/main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/main.py b/app/main.py index 2326594..6fc7a34 100644 --- a/app/main.py +++ b/app/main.py @@ -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 @@ -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, @@ -195,6 +206,7 @@ def profile(): books_read = books_read, books_unread = books_unread, books_shared = books_shared, + books_by_country = books_by_country, )