display 1200 votes as 1.2k

This commit is contained in:
rimu 2024-01-10 09:44:59 +13:00
parent bdcb17e3c1
commit a29a914bf0
3 changed files with 16 additions and 5 deletions

View file

@ -3,7 +3,7 @@
<div class="upvote_button digits_{{ digits(post.up_votes) }} {{ upvoted_class }}"
hx-post="/post/{{ post.id }}/upvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons">
<span class="fe fe-arrow-up"></span>
{{ post.up_votes }}
{{ shorten_number(post.up_votes) }}
<img class="htmx-indicator" src="/static/images/spinner.svg" alt="" style="opacity: 0;">
</div>
{% endif %}
@ -11,17 +11,17 @@
<div class="downvote_button digits_{{ digits(post.down_votes) }} {{ downvoted_class }}"
hx-post="/post/{{ post.id }}/downvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons">
<span class="fe fe-arrow-down"></span>
{{ post.down_votes }}
{{ shorten_number(post.down_votes) }}
<img class="htmx-indicator" src="/static/images/spinner.svg" alt="" style="opacity: 0;">
</div>
{% endif %}
{% else %}
<div class="upvote_button digits_{{ digits(post.up_votes) }} {{ upvoted_class }}">
<span class="fe fe-arrow-up"></span>
{{ post.up_votes }}
{{ shorten_number(post.up_votes) }}
</div>
<div class="downvote_button digits_{{ digits(post.down_votes) }} {{ downvoted_class }}">
<span class="fe fe-arrow-down"></span>
{{ post.down_votes }}
{{ shorten_number(post.down_votes) }}
</div>
{% endif %}

View file

@ -483,6 +483,16 @@ def awaken_dormant_instance(instance):
db.session.commit()
def shorten_number(number):
if number < 1000:
return str(number)
elif number < 1000000:
return f'{number / 1000:.1f}k'
else:
return f'{number / 1000000:.1f}M'
# All the following post/comment ranking math is explained at https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9
epoch = datetime(1970, 1, 1)

View file

@ -8,7 +8,7 @@ from flask import session, g, json
from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE
from app.models import Site
from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access, community_membership, \
can_create, can_upvote, can_downvote
can_create, can_upvote, can_downvote, shorten_number
app = create_app()
cli.register(app)
@ -31,6 +31,7 @@ with app.app_context():
app.jinja_env.globals['len'] = len
app.jinja_env.globals['digits'] = digits
app.jinja_env.globals['str'] = str
app.jinja_env.globals['shorten_number'] = shorten_number
app.jinja_env.globals['community_membership'] = community_membership
app.jinja_env.globals['json_loads'] = json.loads
app.jinja_env.globals['user_access'] = user_access