From a29a914bf01ac3c757000521067b1091ce7ce9a3 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 10 Jan 2024 09:44:59 +1300 Subject: [PATCH] display 1200 votes as 1.2k --- app/templates/post/_post_voting_buttons.html | 8 ++++---- app/utils.py | 10 ++++++++++ pyfedi.py | 3 ++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/templates/post/_post_voting_buttons.html b/app/templates/post/_post_voting_buttons.html index 516c2fa4..0ccd990b 100644 --- a/app/templates/post/_post_voting_buttons.html +++ b/app/templates/post/_post_voting_buttons.html @@ -3,7 +3,7 @@
{% endif %} @@ -11,17 +11,17 @@ {% endif %} {% else %} {% endif %} diff --git a/app/utils.py b/app/utils.py index 0297bfc4..7bbf55f0 100644 --- a/app/utils.py +++ b/app/utils.py @@ -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) diff --git a/pyfedi.py b/pyfedi.py index 0da039e0..19520d5e 100644 --- a/pyfedi.py +++ b/pyfedi.py @@ -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