fixing leaderboards

This commit is contained in:
lza_menace 2022-09-04 22:13:50 -07:00
parent 0d02bf63f7
commit 08050e56c6
4 changed files with 40 additions and 83 deletions

View file

@ -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

View file

@ -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'
)

View file

@ -13,11 +13,11 @@
<th>Post Count</th>
<th>Amount</th>
</tr>
{% for poster, data in posters.items() | sort(attribute='1.amount', reverse=True) %}
{% for poster in posters %}
<tr>
<td><a href="/?submitter={{ poster }}">{{ poster }}</a></td>
<td>{{ data["posts"] | length }}</td>
<td>{{ data["amount"] }} WOW</td>
<td><a href="/?submitter={{ poster.username }}">{{ poster.username }}</a></td>
<td>{{ poster.get_post_count() }}</td>
<td>{{ poster.get_wow_received() | from_atomic }} WOW</td>
</tr>
{% endfor %}
</table>

View file

@ -1,62 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<div class="container" style="text-align:center;">
<h1 class="title">Top Memes Last {{ days }} Days</h1>
<section class="section">
{% if posts %}
{% for row in posts | sort(attribute='received_wow', reverse=True) | batch(4) %}
<div class="columns">
{% if loop.index < 15 %}
{% for post in row %}
<div class="column">
<div class="card">
<div class="card-image">
<a href="{{ url_for('post.read', id=post.id) }}">
<img src="{{ url_for('post.uploaded_file', filename=post.thumbnail_name) }}" alt="Placeholder image">
</a>
</div>
<div class="card-content">
<div class="media">
<div class="media-content">
<p class="title is-4">
<a href="{{ url_for('post.read', id=post.id) }}">{{ post.title }}</a>
</p>
<p class="subtitle is-6"><a href="/?submitter={{ post.submitter }}">{{ post.submitter }}</a></p>
</div>
</div>
<div class="content">
{{ post.text | truncate(60) }}
<p><strong>{{ post.received_wow }} WOW received</strong></p>
<time datetime="2016-1-1">{{ post.timestamp.year }}-{{ post.timestamp.month }}-{{ post.timestamp.day }} {{ post.timestamp.hour }}:{{ post.timestamp.minute }} UTC</time>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
</div>
{% endfor %}
{% else %}
<p>No posts yet!</p>
{% endif %}
</section>
{% if total_pages %}
<nav class="pagination is-centered pb-4" role="navigation" aria-label="pagination">
<ul class="pagination-list">
{% for p in range(1, total_pages + 1) %}
<a href="{% if request.args.submitter %}/?submitter={{ request.args.submitter }}&{% else %}/?{% endif %}page={{ p }}" class="pagination-link {% if p == page %}current-page-btn{% endif %}">{{ p }}</a>
{% endfor %}
</ul>
</nav>
{% endif %}
</div>
{% endblock %}