fix hat ordering in a more performant way

pull/146/head
Aevann 2023-05-05 09:37:58 +03:00
parent 58a84e11fe
commit b0eea9b223
1 changed files with 3 additions and 3 deletions

View File

@ -37,12 +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()]
hat_count = {x[0]:x[1] for x in g.db.query(Hat.hat_id, func.count(Hat.hat_id)).group_by(Hat.hat_id).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: hat_count[x[0].id] if x[0].id in hat_count else 0, 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: hat_count[x.id] if x.id in hat_count else 0, reverse=True)
firstrange = PAGE_SIZE * (page - 1)
secondrange = firstrange + PAGE_SIZE