diff --git a/suchwow/_models.py b/suchwow/_models.py index 52625d8..68ded7a 100644 --- a/suchwow/_models.py +++ b/suchwow/_models.py @@ -39,6 +39,10 @@ class User(Model): def get_wow_sent(self): tips = TipSent.select().where(TipSent.from_user == self) return sum(tip.amount for tip in tips) + + def get_post_count(self): + posts = Post.select().where(Post.user == self) + return posts.count() class Meta: database = db diff --git a/suchwow/routes/leaderboard.py b/suchwow/routes/leaderboard.py index f210aa0..eea0e86 100644 --- a/suchwow/routes/leaderboard.py +++ b/suchwow/routes/leaderboard.py @@ -1,3 +1,5 @@ +from datetime import datetime, timedelta + import peewee from flask import render_template, Blueprint, request @@ -6,26 +8,25 @@ from suchwow._models import Post, TipReceived, User bp = Blueprint("leaderboard", "leaderboard") + @bp.route("/leaderboards/top_posters") def top_posters(): - top_posters = TipReceived.select( - TipReceived.post, peewee.fn.SUM(TipReceived.amount) - ).join(Post).order_by( - peewee.fn.SUM(TipReceived.amount).desc() - ).group_by(TipReceived.post) - # revenue = fn.SUM(Booking.slots * Case(None, ( - # (Booking.member == 0, Facility.guestcost), - # ), Facility.membercost)) - # - # query = (Facility - # .select(Facility.name, revenue.alias('revenue')) - # .join(Booking) - # .group_by(Facility.name) - # .order_by(SQL('revenue'))) + 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) @@ -34,6 +35,20 @@ def top_posts(): if days not in [1, 3, 7, 30, 9999]: days = 7 - - posts = get_top_posts(days) - return render_template("post/top.html", posts=posts, days=days) + + 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' + ) diff --git a/suchwow/templates/leaderboard.html b/suchwow/templates/leaderboard.html index e3c7916..0c50f92 100644 --- a/suchwow/templates/leaderboard.html +++ b/suchwow/templates/leaderboard.html @@ -13,11 +13,11 @@ Post Count Amount - {% for poster, data in posters.items() | sort(attribute='1.amount', reverse=True) %} + {% for poster in posters %} - {{ poster }} - {{ data["posts"] | length }} - {{ data["amount"] }} WOW + {{ poster.username }} + {{ poster.get_post_count() }} + {{ poster.get_wow_received() | from_atomic }} WOW {% endfor %} diff --git a/suchwow/templates/post/top.html b/suchwow/templates/post/top.html deleted file mode 100644 index ff44783..0000000 --- a/suchwow/templates/post/top.html +++ /dev/null @@ -1,62 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} - -
- -

Top Memes Last {{ days }} Days

- -
- {% if posts %} - {% for row in posts | sort(attribute='received_wow', reverse=True) | batch(4) %} -
- {% if loop.index < 15 %} - {% for post in row %} -
-
-
- - Placeholder image - -
-
- - -
- {{ post.text | truncate(60) }} -

{{ post.received_wow }} WOW received

- -
-
-
-
- {% endfor %} - {% endif %} -
- {% endfor %} - {% else %} -

No posts yet!

- {% endif %} -
- - {% if total_pages %} - - {% endif %} - - -
- -{% endblock %}