From 58a84e11fedd31f6a080eab9dc230058d3c0632e Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 5 May 2023 09:35:43 +0300 Subject: [PATCH] Revert "fix hat ordering" This reverts commit c196041f64af2303fd0352cab55d5061cfcd93ab. --- files/classes/hats.py | 7 ++----- files/routes/hats.py | 6 ++++-- files/templates/hats.html | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/files/classes/hats.py b/files/classes/hats.py index 9d8cabd4b..8ec7421ac 100644 --- a/files/classes/hats.py +++ b/files/classes/hats.py @@ -8,8 +8,6 @@ from files.classes import Base from files.helpers.lazy import lazy from files.helpers.regex import censor_slurs -from flask import g - class HatDef(Base): __tablename__ = "hat_defs" @@ -31,10 +29,9 @@ class HatDef(Base): def __repr__(self): return f"<{self.__class__.__name__}(id={self.id})>" - @property @lazy - def number_sold(self): - return g.db.query(Hat).filter_by(hat_id=self.id).count() + def number_sold(self, db:scoped_session): + return db.query(Hat).filter_by(hat_id=self.id).count() @lazy def censored_description(self, v): diff --git a/files/routes/hats.py b/files/routes/hats.py index 379be507b..789c9f9ce 100644 --- a/files/routes/hats.py +++ b/files/routes/hats.py @@ -37,10 +37,12 @@ def hats(v:User): hats = hats.order_by(key).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all() elif sort == "owners": + hat_count = [x[0] for x in g.db.query(Hat.hat_id).group_by(Hat.hat_id).order_by(func.count(Hat.hat_id).desc()).all()] + if SITE == 'rdrama.net': - hats = sorted(hats.all(), key=lambda x: x[0].number_sold, reverse=True) + hats = sorted(hats.all(), key=lambda x: hat_count.index(x[0].id) if x[0].id in hat_count else 0) else: - hats = sorted(hats.all(), key=lambda x: x.number_sold, reverse=True) + hats = sorted(hats.all(), key=lambda x: hat_count.index(x.id) if x.id in hat_count else 0) firstrange = PAGE_SIZE * (page - 1) secondrange = firstrange + PAGE_SIZE diff --git a/files/templates/hats.html b/files/templates/hats.html index a1dc98503..c3220cd31 100644 --- a/files/templates/hats.html +++ b/files/templates/hats.html @@ -76,7 +76,7 @@ {% if SITE == 'rdrama.net' %} {% include "user_in_table.html" %} {% endif %} - {{hat.number_sold}} + {{hat.number_sold(g.db)}} {{hat.price}} {% if hat.id not in owned_hat_ids and hat.is_purchasable %}