paginate /marseys

pull/146/head
Aevann 2023-05-05 06:30:04 +03:00
parent b0f3c26283
commit b53b0e6ac7
3 changed files with 29 additions and 28 deletions

View File

@ -183,8 +183,6 @@ def approve_emoji(v, name):
cache.delete("emojis")
if emoji.kind == "Marsey":
cache.delete("marseys")
purge_files_in_cache(f"https://{SITE}/e/{emoji.name}/webp")
@ -493,8 +491,6 @@ def update_emoji(v):
g.db.add(ma)
cache.delete("emojis")
if existing.kind == "Marsey":
cache.delete("marseys")
return render_template("admin/update_assets.html", v=v, msg=f"'{name}' updated successfully!", name=name, tags=tags, kind=kind, type="Emoji")

View File

@ -1,7 +1,7 @@
import os
from shutil import copyfile
from sqlalchemy import func
from sqlalchemy import func, nullslast
from files.helpers.media import *
import files.helpers.stats as statshelper
@ -41,35 +41,39 @@ def reddit_post(subreddit, v, path):
return redirect(f'https://{reddit}/{post_id}')
@cache.cached(key_prefix="marseys")
def get_marseys(db:scoped_session):
if not FEATURES['MARSEYS']: return []
marseys = []
for marsey, author in db.query(Emoji, User).join(User, Emoji.author_id == User.id).filter(Emoji.kind == "Marsey", Emoji.submitter_id == None).order_by(Emoji.count.desc()):
marsey.author = author.username if FEATURES['ASSET_SUBMISSIONS'] else None
marseys.append(marsey)
return marseys
@app.get("/marseys")
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def marseys(v:User):
if SITE_NAME != 'rDrama':
if SITE_NAME != 'rDrama' or not FEATURES['MARSEYS']:
abort(404)
marseys = get_marseys(g.db)
authors = get_accounts_dict([m.author_id for m in marseys], v=v, graceful=True)
marseys = g.db.query(Emoji, User).join(User, Emoji.author_id == User.id).filter(Emoji.kind == "Marsey", Emoji.submitter_id==None)
next_exists = marseys.count()
sort = request.values.get("sort")
if sort == "author":
marseys = marseys.order_by(User.username, Emoji.count.desc())
elif sort == "added_on":
marseys = marseys.order_by(nullslast(Emoji.created_utc.desc()), User.username)
else: # implied sort == "usage"
marseys = marseys.order_by(Emoji.count.desc(), User.username)
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
marseys = marseys.offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all()
original = os.listdir("/asset_submissions/emojis/original")
for marsey in marseys:
marsey.user = authors.get(marsey.author_id)
for marsey, user in marseys:
for x in IMAGE_FORMATS:
if f'{marsey.name}.{x}' in original:
marsey.og = f'{marsey.name}.{x}'
break
return render_template("marseys.html", v=v, marseys=marseys)
return render_template("marseys.html", v=v, marseys=marseys, page=page, next_exists=next_exists, sort=sort)
@cache.cached(key_prefix="emojis")

View File

@ -5,28 +5,25 @@
<div class="overflow-x-auto mt-3"><table class="table table-striped mb-5">
<thead class="bg-primary text-white">
<tr>
<th>#</th>
<th>Name</th>
<th>Marsey</th>
<th>Usage</th>
<th class="disable-sort-click"><a href="/marseys" {% if sort=="usage" %}disabled{% endif %}>Usage</a></th>
{% if FEATURES['ASSET_SUBMISSIONS'] %}
<th>Author</th>
<th class="disable-sort-click"><a href="?sort=author" {% if sort=="author" %}disabled{% endif %}>Author</a></th>
{% endif %}
<th>Added on</th>
<th class="disable-sort-click"><a href="?sort=added_on" {% if sort=="added_on" %}disabled{% endif %}>Added on</a></th>
{% if FEATURES['ASSET_SUBMISSIONS'] %}
<th>Original File</th>
{% endif %}
</tr>
</thead>
<tbody id="marseys-table">
{% for marsey in marseys %}
{% for marsey, user in marseys %}
<tr>
<td>{{loop.index}}</td>
<td>{{marsey.name}}</td>
<td><img class="marsey" loading="lazy" data-bs-toggle="tooltip" alt=":#{{marsey.name}}:" title=":{{marsey.name}}:" src="/e/{{marsey.name}}.webp"></td>
<td>{{marsey.count}}</td>
{% if FEATURES['ASSET_SUBMISSIONS'] %}
{% set user = marsey.user %}
<td>{% include "user_in_table.html" %}</td>
{% endif %}
<td data-sort-key="{{marsey.created_utc or 0}}" {% if marsey.created_utc != None %}data-time="{{marsey.created_utc}}"{% endif %}></td>
@ -43,3 +40,7 @@
</table>
{% endblock %}
{% block pagenav %}
{% include "pagination.html" %}
{% endblock %}