diff --git a/files/classes/user.py b/files/classes/user.py index e31506639..21f01d096 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -208,6 +208,13 @@ class User(Base): def num_of_owned_hats(self): return len(self.owned_hats) + @property + @lazy + def hats_owned_proportion_display(self): + total_num_of_hats = g.db.query(HatDef).filter(HatDef.submitter_id == None).count() + proportion = f'{float(self.num_of_owned_hats) / total_num_of_hats:.1%}' + return (proportion, total_num_of_hats) + @property @lazy def num_of_designed_hats(self): diff --git a/files/routes/users.py b/files/routes/users.py index b3cef624d..2fa897d6c 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -994,9 +994,6 @@ def u_username(username, v=None): listing = get_posts(ids, v=v) - # this is probably totalllly inefficient but maybe someone can fix it later - total_num_of_hats = g.db.query(HatDef).filter(HatDef.submitter_id == None).count() - if u.unban_utc: if request.headers.get("Authorization") or request.path.endswith(".json"): return {"data": [x.json for x in listing]} @@ -1010,8 +1007,7 @@ def u_username(username, v=None): sort=sort, t=t, next_exists=next_exists, - is_following=is_following, - total_num_of_hats=total_num_of_hats) + is_following=is_following) if request.headers.get("Authorization") or request.path.endswith(".json"): return {"data": [x.json for x in listing]} @@ -1024,8 +1020,7 @@ def u_username(username, v=None): sort=sort, t=t, next_exists=next_exists, - is_following=is_following, - total_num_of_hats=total_num_of_hats) + is_following=is_following) @app.get("/@/comments") diff --git a/files/templates/userpage.html b/files/templates/userpage.html index 6875fe6cf..0cf8c89e1 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -30,6 +30,8 @@ {% endblock %} {% import 'userpage_admintools.html' as userpage_admintools with context %} +{% set hats_total = u.hats_owned_proportion_display[1] %} +{% set hats_owned_percent = u.hats_owned_proportion_display[0] %} {% block desktopUserBanner %} @@ -253,7 +255,7 @@

Coins spent: {{u.coins_spent}}

True score: {{u.truecoins}}

Winnings: {{u.winnings}}

-

{{u.num_of_owned_hats}} / {{total_num_of_hats}} hats owned ({{ '{:.0%}'.format(u.num_of_owned_hats / total_num_of_hats) }})

+

{{u.num_of_owned_hats}} / {{hats_total}} hats owned ({{hats_owned_percent}})

{% if u.is_private %}

User has private mode enabled

{% endif %} @@ -497,7 +499,7 @@

Coins spent: {{u.coins_spent}}

True score: {{u.truecoins}}

Winnings: {{u.winnings}}

-

{{u.num_of_owned_hats}} / {{total_num_of_hats}} hats owned ({{ '{:.0%}'.format(u.num_of_owned_hats / total_num_of_hats) }})

+

{{u.num_of_owned_hats}} / {{hats_total}} hats owned ({{hats_owned_percent}})

{% if u.is_private %}

User has private mode enabled

{% endif %}