Add top packages badge to profile

This commit is contained in:
rubenwardy 2021-07-25 18:17:59 +01:00
parent ac9b2207bf
commit 76675ad76b
2 changed files with 45 additions and 1 deletions

@ -72,9 +72,18 @@ def profile(username):
.join(User.maintained_packages) \
.filter(User.id == user.id, Package.state == PackageState.APPROVED).scalar() or 0
all_package_ranks = db.session.query(
Package.author_id,
func.rank().over(order_by=db.desc(Package.score)) \
.label('rank')).order_by(db.asc(text("rank"))).subquery()
user_package_ranks = db.session.query(all_package_ranks) \
.filter_by(author_id=user.id).first()
min_package_rank = user_package_ranks[1] if user_package_ranks else None
# Process GET or invalid POST
return render_template("users/profile.html", user=user, packages=packages,
total_downloads=total_downloads, review_idx=review_idx, review_percent=review_percent)
total_downloads=total_downloads, min_package_rank=min_package_rank,
review_idx=review_idx, review_percent=review_percent)
@bp.route("/users/<username>/check/", methods=["POST"])

@ -218,6 +218,41 @@
</div>
</div>
{% endif %}
{% if min_package_rank <= 30 %}
{% if min_package_rank <= 5 %}
{% set badge_color = "gold" %}
{% elif min_package_rank <= 10 %}
{% set badge_color = "#888" %}
{% elif min_package_rank <= 20 %}
{% set badge_color = "#cd7f32" %}
{% else %}
{% set badge_color = "white" %}
{% endif %}
<div class="col-md-4">
<div class="card">
<div class="card-body media align-items-center">
<i class="fas fa-trophy ml-2 mr-4 text-size" style="font-size: 45px; color: {{ badge_color }};"></i>
<div class="media-body">
<h5 class="mt-0">
{% if min_package_rank <= 5 %}
{{ _("Top 5 package") }}
{% elif min_package_rank <= 10 %}
{{ _("Top 10 package") }}
{% elif min_package_rank <= 20 %}
{{ _("Top 20 package") }}
{% else %}
{{ _("Top 30 package") }}
{% endif %}
</h5>
<p class="my-0">
{{ _("%(display_name)s has a package placed at #%(place)d.",
display_name=user.display_name, place=min_package_rank) }}
</p>
</div>
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}