fix hat ordering

pull/146/head
Aevann 2023-05-05 09:30:39 +03:00
parent 636f78761c
commit c196041f64
3 changed files with 8 additions and 7 deletions

View File

@ -8,6 +8,8 @@ from files.classes import Base
from files.helpers.lazy import lazy from files.helpers.lazy import lazy
from files.helpers.regex import censor_slurs from files.helpers.regex import censor_slurs
from flask import g
class HatDef(Base): class HatDef(Base):
__tablename__ = "hat_defs" __tablename__ = "hat_defs"
@ -29,9 +31,10 @@ class HatDef(Base):
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__}(id={self.id})>" return f"<{self.__class__.__name__}(id={self.id})>"
@property
@lazy @lazy
def number_sold(self, db:scoped_session): def number_sold(self):
return db.query(Hat).filter_by(hat_id=self.id).count() return g.db.query(Hat).filter_by(hat_id=self.id).count()
@lazy @lazy
def censored_description(self, v): def censored_description(self, v):

View File

@ -37,12 +37,10 @@ def hats(v:User):
hats = hats.order_by(key).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all() hats = hats.order_by(key).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
elif sort == "owners": 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': 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: 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) firstrange = PAGE_SIZE * (page - 1)
secondrange = firstrange + PAGE_SIZE secondrange = firstrange + PAGE_SIZE

View File

@ -76,7 +76,7 @@
{% if SITE == 'rdrama.net' %} {% if SITE == 'rdrama.net' %}
<td>{% include "user_in_table.html" %}</td> <td>{% include "user_in_table.html" %}</td>
{% endif %} {% endif %}
<td><a href="/hat_owners/{{hat.id}}">{{hat.number_sold(g.db)}}</a></td> <td><a href="/hat_owners/{{hat.id}}">{{hat.number_sold}}</a></td>
<td>{{hat.price}}</td> <td>{{hat.price}}</td>
<td class="shop-table-actions" style="width:unset"> <td class="shop-table-actions" style="width:unset">
{% if hat.id not in owned_hat_ids and hat.is_purchasable %} {% if hat.id not in owned_hat_ids and hat.is_purchasable %}