From 29bfc91683d63c3045a8de42eddced61df74ef15 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 25 Jul 2021 16:09:21 +0100 Subject: [PATCH] Add top reviewer badge to profile page --- app/blueprints/users/profile.py | 15 +++++++- app/templates/users/profile.html | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/app/blueprints/users/profile.py b/app/blueprints/users/profile.py index fcfcb150..c8a1a114 100644 --- a/app/blueprints/users/profile.py +++ b/app/blueprints/users/profile.py @@ -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//check/", methods=["POST"]) diff --git a/app/templates/users/profile.html b/app/templates/users/profile.html index ff1a3ca8..16fe9aa4 100644 --- a/app/templates/users/profile.html +++ b/app/templates/users/profile.html @@ -117,6 +117,69 @@ Is this you? Claim your account now! +{% else %} +
+ {% if review_percent is not none and review_percent < 25 and review_idx >= 0 %} +
+
+ {% 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 %} +
+ +
+ {% if review_idx == 0 %} +
+ {{ _("Most reviews", perc=review_percent) }} +
+

+ {{ _("%(display_name)s has written the most reviews on ContentDB.", + display_name=user.display_name) }} +

+ {% elif review_idx <= 2 %} +
+ {% if review_idx == 1 %} + {{ _("2nd most reviews", perc=review_percent) }} + {% else %} + {{ _("3rd most reviews", perc=review_percent) }} + {% endif %} +
+

+ {{ _("This puts %(display_name)s in the top %(perc)s%%", + display_name=user.display_name, perc=review_percent) }} +

+ {% else %} +
+ {{ _("Top %(perc)s%% reviewer", perc=review_percent) }} +
+

+ {% if review_idx == 1 %} + {{ _("Only 1 user has written more reviews.") }} + {% else %} + {{ _("Only %(place)d users have written more reviews.", place=review_idx) }} + {% endif %} +

+ {% endif %} +
+
+
+
+ {% elif current_user == user %} +
+ + {{ _("Consider writing more reviews to get a badge.") }} + {% if review_idx %} + {{ _("You are in place %(place)s.", place=review_idx + 1) }} + {% endif %} + +
+ {% endif %} {% endif %} {% if current_user == user or user.checkPerm(current_user, "CHANGE_AUTHOR") %}