diff --git a/files/classes/hats.py b/files/classes/hats.py index 8ec7421ac..9d8cabd4b 100644 --- a/files/classes/hats.py +++ b/files/classes/hats.py @@ -8,6 +8,8 @@ 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" @@ -29,9 +31,10 @@ class HatDef(Base): def __repr__(self): return f"<{self.__class__.__name__}(id={self.id})>" + @property @lazy - def number_sold(self, db:scoped_session): - return db.query(Hat).filter_by(hat_id=self.id).count() + def number_sold(self): + return g.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 789c9f9ce..379be507b 100644 --- a/files/routes/hats.py +++ b/files/routes/hats.py @@ -37,12 +37,10 @@ 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: hat_count.index(x[0].id) if x[0].id in hat_count else 0) + hats = sorted(hats.all(), key=lambda x: x[0].number_sold, reverse=True) else: - hats = sorted(hats.all(), key=lambda x: hat_count.index(x.id) if x.id in hat_count else 0) + hats = sorted(hats.all(), key=lambda x: x.number_sold, reverse=True) firstrange = PAGE_SIZE * (page - 1) secondrange = firstrange + PAGE_SIZE diff --git a/files/templates/hats.html b/files/templates/hats.html index c3220cd31..a1dc98503 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(g.db)}} + {{hat.number_sold}} {{hat.price}} {% if hat.id not in owned_hat_ids and hat.is_purchasable %}