forked from rDrama/rDrama
1
0
Fork 0

paginate /shop/hats

master
Aevann 2023-05-05 06:13:17 +03:00
parent 67874f453a
commit b0f3c26283
2 changed files with 65 additions and 16 deletions

View File

@ -14,19 +14,60 @@ from files.__main__ import app, limiter
def hats(v:User):
owned_hat_ids = [x.hat_id for x in v.owned_hats]
if v.equipped_hat_ids:
equipped = g.db.query(HatDef, User).join(HatDef.author).filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids), HatDef.id.in_(v.equipped_hat_ids)).order_by(HatDef.price, HatDef.name).all()
not_equipped = g.db.query(HatDef, User).join(HatDef.author).filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids), HatDef.id.notin_(v.equipped_hat_ids)).order_by(HatDef.price, HatDef.name).all()
owned = equipped + not_equipped
else:
owned = g.db.query(HatDef, User).join(HatDef.author).filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids)).order_by(HatDef.price, HatDef.name).all()
sort = request.values.get("sort")
not_owned = g.db.query(HatDef, User).join(HatDef.author).filter(HatDef.submitter_id == None, HatDef.id.notin_(owned_hat_ids)).order_by(HatDef.price == 0, HatDef.price, HatDef.name).all()
hats = owned + not_owned
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
if SITE == 'rdrama.net':
hats = g.db.query(HatDef, User).join(HatDef.author)
else:
hats = g.db.query(HatDef)
if sort and sort != "owners":
if sort == "name":
key = HatDef.name
elif sort == "description":
key = HatDef.description
elif sort == "author":
key = User.username
elif sort == "price":
key = HatDef.price
elif sort == "added_on":
key = HatDef.created_utc.desc()
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, reverse=True)
else:
hats = sorted(hats.all(), key=lambda x: hat_count.index(x.id) if x.id in hat_count else 0, reverse=True)
firstrange = PAGE_SIZE * (page - 1)
secondrange = firstrange + PAGE_SIZE
hats = hats[firstrange:secondrange]
else:
if v.equipped_hat_ids:
equipped = hats.filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids), HatDef.id.in_(v.equipped_hat_ids)).order_by(HatDef.price, HatDef.name).all()
not_equipped = hats.filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids), HatDef.id.notin_(v.equipped_hat_ids)).order_by(HatDef.price, HatDef.name).all()
owned = equipped + not_equipped
else:
owned = hats.filter(HatDef.submitter_id == None, HatDef.id.in_(owned_hat_ids)).order_by(HatDef.price, HatDef.name).all()
not_owned = hats.filter(HatDef.submitter_id == None, HatDef.id.notin_(owned_hat_ids)).order_by(HatDef.price == 0, HatDef.price, HatDef.name).all()
hats = owned + not_owned
firstrange = PAGE_SIZE * (page - 1)
secondrange = firstrange + PAGE_SIZE
hats = hats[firstrange:secondrange]
sales = g.db.query(func.sum(User.coins_spent_on_hats)).scalar()
num_of_hats = g.db.query(HatDef).filter(HatDef.submitter_id == None).count()
return render_template("hats.html", owned_hat_ids=owned_hat_ids, hats=hats, v=v, sales=sales, num_of_hats=num_of_hats)
print(hats, flush=True)
return render_template("hats.html", owned_hat_ids=owned_hat_ids, hats=hats, v=v, sales=sales, num_of_hats=num_of_hats, next_exists=num_of_hats, page=page, sort=sort)
@app.post("/buy_hat/<int:hat_id>")
@limiter.limit('1/second', scope=rpath)

View File

@ -44,20 +44,24 @@
<thead class="bg-primary text-white">
<tr>
<th class="disable-sort-click">Hat</th>
<th>Name</th>
<th>Description</th>
<th class="disable-sort-click"><a href="?sort=name" {% if sort=="name" %}disabled{% endif %}>Name</a></th>
<th class="disable-sort-click"><a href="?sort=description" {% if sort=="description" %}disabled{% endif %}>Description</a></th>
{% if SITE == 'rdrama.net' %}
<th>Author</a></th>
<th class="disable-sort-click"><a href="?sort=author" {% if sort=="author" %}disabled{% endif %}>Author</a></th>
{% endif %}
<th>Owners</th>
<th>Price</th>
<th class="disable-sort-click"><a href="?sort=owners" {% if sort=="owners" %}disabled{% endif %}>Owners</a></th>
<th class="disable-sort-click"><a href="?sort=price" {% if sort=="price" %}disabled{% endif %}>Price</a></th>
<th class="disable-sort-click">Actions</th>
<th>Added on</th>
<th class="disable-sort-click"><a href="?sort=added_on" {% if sort=="added_on" %}disabled{% endif %}>Added on</a></th>
</tr>
</thead>
<tbody>
{% for hat, user in hats %}
{% for hat in hats %}
{% if SITE == 'rdrama.net' %}
{% set hat = hat[0] %}
{% set user = hat[1] %}
{% endif %}
<tr>
<td>
<div class="profile-pic-75-wrapper mt-4">
@ -93,3 +97,7 @@
</table>
{% endblock %}
{% block pagenav %}
{% include "pagination.html" %}
{% endblock %}