Add top reviewer badge to profile page

This commit is contained in:
rubenwardy 2021-07-25 16:09:21 +01:00
parent cb5fa4d6e7
commit 29bfc91683
2 changed files with 77 additions and 1 deletions

@ -54,8 +54,21 @@ def profile(username):
packages = packages.filter_by(state=PackageState.APPROVED)
packages = packages.order_by(db.asc(Package.title))
users_by_reviews = db.session.query(User.username, func.count(PackageReview.id).label("count")) \
.select_from(User).join(PackageReview) \
.group_by(User.username).order_by(text("count DESC")).all()
users_by_reviews = [ username for username, _ in users_by_reviews ]
review_idx = None
review_percent = None
try:
review_idx = users_by_reviews.index(user.username)
review_percent = round(100 * review_idx / len(users_by_reviews), 1)
except ValueError:
pass
# Process GET or invalid POST
return render_template("users/profile.html", user=user, packages=packages)
return render_template("users/profile.html", user=user, packages=packages,
review_idx=review_idx, review_percent=review_percent)
@bp.route("/users/<username>/check/", methods=["POST"])

@ -117,6 +117,69 @@
Is this you? Claim your account now!
</div>
{% else %}
<div class="row mb-5">
{% if review_percent is not none and review_percent < 25 and review_idx >= 0 %}
<div class="col-md-4">
<div class="card">
{% if review_idx == 0 %}
{% set badge_color = "gold" %}
{% elif review_idx == 1 %}
{% set badge_color = "#888" %}
{% elif review_idx == 2 %}
{% set badge_color = "#cd7f32" %}
{% else %}
{% set badge_color = "white" %}
{% endif %}
<div class="card-body media align-items-center">
<i class="fas fa-thumbs-up ml-2 mr-4 text-size" style="font-size: 45px; color: {{ badge_color }};"></i>
<div class="media-body">
{% if review_idx == 0 %}
<h5 class="mt-0">
{{ _("Most reviews", perc=review_percent) }}
</h5>
<p class="my-0">
{{ _("%(display_name)s has written the most reviews on ContentDB.",
display_name=user.display_name) }}
</p>
{% elif review_idx <= 2 %}
<h5 class="mt-0">
{% if review_idx == 1 %}
{{ _("2nd most reviews", perc=review_percent) }}
{% else %}
{{ _("3rd most reviews", perc=review_percent) }}
{% endif %}
</h5>
<p class="my-0">
{{ _("This puts %(display_name)s in the top %(perc)s%%",
display_name=user.display_name, perc=review_percent) }}
</p>
{% else %}
<h5 class="mt-0">
{{ _("Top %(perc)s%% reviewer", perc=review_percent) }}
</h5>
<p class="my-0">
{% if review_idx == 1 %}
{{ _("Only 1 user has written more reviews.") }}
{% else %}
{{ _("Only %(place)d users have written more reviews.", place=review_idx) }}
{% endif %}
</p>
{% endif %}
</div>
</div>
</div>
</div>
{% elif current_user == user %}
<div class="col-md-4">
<span class="border border-dark rounded p-3 text-muted">
{{ _("Consider writing more reviews to get a badge.") }}
{% if review_idx %}
{{ _("You are in place %(place)s.", place=review_idx + 1) }}
{% endif %}
</span>
</div>
{% endif %}
{% endif %}
{% if current_user == user or user.checkPerm(current_user, "CHANGE_AUTHOR") %}