You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suchwow/suchwow/routes/leaderboard.py

55 lines
1.3 KiB
Python

from datetime import datetime, timedelta
import peewee
from flask import render_template, Blueprint, request
from suchwow._models import Post, TipReceived, User
bp = Blueprint("leaderboard", "leaderboard")
@bp.route("/leaderboards/top_posters")
def top_posters():
tips_received = peewee.fn.SUM(TipReceived.amount)
top_posters = User.select(
User, tips_received
).join(
Post, peewee.JOIN.LEFT_OUTER, on=Post.user
).join(
TipReceived, peewee.JOIN.LEFT_OUTER
).group_by(User.username).order_by(
tips_received.desc()
).limit(30)
return render_template("leaderboard.html", posters=top_posters)
@bp.route("/leaderboards/top_posts")
def top_posts():
tips_received = peewee.fn.SUM(TipReceived.amount)
days = request.args.get('days', 1)
try:
days = int(days)
except:
days = 1
if days not in [1, 3, 7, 30, 9999]:
days = 7
new_date = datetime.utcnow() - timedelta(hours=(days * 24))
posts = Post.select(Post, tips_received).join(
TipReceived
).where(
TipReceived.timestamp >= new_date
).group_by(
Post.id
).order_by(
tips_received.desc()
).limit(30)
return render_template(
"index.html",
posts=posts,
days=days,
title=f'Top Posts Last {days} Days'
)