From 41a954a5f483a0665af94beae2322ec2a186f022 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Feb 02 2018 05:07:16 +0000 Subject: implemend badges widget as per mockup we don't habe the "rarest" badge info from the API but everything else is there and works. fixes #435 --- diff --git a/hubs/widgets/badges/__init__.py b/hubs/widgets/badges/__init__.py index c7fb323..a039018 100644 --- a/hubs/widgets/badges/__init__.py +++ b/hubs/widgets/badges/__init__.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals -import operator import requests +import arrow +from collections import defaultdict, OrderedDict -from hubs.utils import validators from hubs.widgets.base import Widget from hubs.widgets.view import RootWidgetView from hubs.widgets.caching import CachedFunction @@ -13,13 +13,13 @@ class Badges(Widget): name = "badges" position = "right" - parameters = [dict( - name="username", - label="Username", - default=None, - validator=validators.Username, - help="A FAS username.", - )] + hub_types = ["stream", "user"] + + def get_template_environment(self): + env = super(Badges, self).get_template_environment() + # Add a filter + env.filters['humanize'] = lambda d: arrow.get(d).humanize() + return env class BaseView(RootWidgetView): @@ -32,6 +32,9 @@ class BaseView(RootWidgetView): class GetBadges(CachedFunction): + def __init__(self, instance): + self.instance = instance + self.invalidate() def execute(self): username = self.instance.config["username"] @@ -39,11 +42,36 @@ class GetBadges(CachedFunction): url = url.format(username=username) response = requests.get(url) try: - assertions = response.json()['assertions'] + response = response.json() + assertions = response['assertions'] except (KeyError, ValueError): - assertions = [] - key = operator.itemgetter('issued') - return dict(assertions=sorted(assertions, key=key, reverse=True)) + return {} + tag_totals = defaultdict(int) + latest_badge = None + for badge in assertions: + if (latest_badge is None or + badge["issued"] > latest_badge["issued"]): + latest_badge = badge + + tags = filter(None, badge["tags"].split(',')) + for tag in tags: + tag_totals[tag] += 1 + + tag_totals = OrderedDict(sorted(tag_totals.items(), + key=lambda t: t[1], + reverse=True)) + + tag_totals = {t: tag_totals[t] for t in list(tag_totals)[:5]} + + return dict( + username=username, + total_users=response["user_count"], + rank=response["rank"], + tag_totals=tag_totals, + latest_badge=latest_badge, + total_badges=len(assertions), + badge_percentage=round(response["percent_earned"], 1) + ) def should_invalidate(self, message): if message['topic'].endswith('hubs.widget.update'): diff --git a/hubs/widgets/badges/templates/root.html b/hubs/widgets/badges/templates/root.html index 8f5056f..94d5e34 100644 --- a/hubs/widgets/badges/templates/root.html +++ b/hubs/widgets/badges/templates/root.html @@ -1,7 +1,74 @@ -
-{% for badge in assertions %} - - - -{% endfor %} +
+
+
Badges Earned
+ +
{{badge_percentage}}% of total
+
+
+
Badges Rank
+ +
of {{total_users}}
+
+
+
+
+
+ +
+
+
+
Most Recent Badge
+
{{latest_badge["name"]}}
+
awarded {{latest_badge["issued"]|humanize}}
+
+
+
+
+
+ +
+
+
Top Badge Tags
+
    + {% for tag in tag_totals%} +
  • + {{tag}}
    {{tag_totals[tag]}}
    +
  • + {% endfor %} +
+
+
+ +