Commits

Anonymous committed d582ea6 Merge

merged

  • Participants
  • Parent commits dfddd07, ee71db4

Comments (0)

Files changed (4)

File badges/models.py

 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.db import models
+from django.conf import settings
 
 from badges.signals import badge_awarded
 from badges.managers import BadgeManager
 
-LEVEL_CHOICES = (
+if hasattr(settings, 'BADGE_LEVEL_CHOICES'):
+    LEVEL_CHOICES = settings.BADGE_LEVEL_CHOICES
+else:
+    LEVEL_CHOICES = (
         ("1", "Bronze"),
         ("2", "Silver"),
         ("3", "Gold"),
         ("4", "Diamond"),
-        )
+    )
 
 class Badge(models.Model):
     id = models.CharField(max_length=255, primary_key=True)
         
         return BadgeToUser.objects.filter(badge=self, user=user).count()
 
+    def number_awarded(self, user_or_qs=None):
+        """
+        Gives the number awarded total. Pass in an argument to
+        get the number per user, or per queryset.
+        """
+        kwargs = {'badge':self}
+        if user_or_qs is None:
+            pass
+        elif isinstance(user_or_qs, User):
+            kwargs.update(dict(user=user_or_qs))
+        else:
+            kwargs.update(dict(user__in=user_or_qs))
+        return BadgeToUser.objects.filter(**kwargs).count()
+
+
 class BadgeToUser(models.Model):
     badge = models.ForeignKey(Badge)
     user = models.ForeignKey(User)

File badges/templates/badges/badges_display.html

 
 {% load badges_tags %}
 <span class="badge">
-{% for level in "gsb"|make_list %}
-	{% if badges|level_count:level %}
-		<span class="level_{{ level }} metal">●</span>{{ badges|level_count:level }}
+{% for level in u|badge_count %}
+	{% if level.count %}
+		<span title="{{level.count}} {{level.badge__level|level_title}} badge{{level.count|pluralize}}"><span class="level_{{level.badge__level}} metal">●</span>{{level.count}}</span>
 	{% endif %}
 {% endfor %}
 </span>

File badges/templatetags/badges_tags.py

 from django.template import Library
+from badges.utils import badge_count
+from badges.models import LEVEL_CHOICES
+level_choices = dict(LEVEL_CHOICES)
 
 register = Library()
 
 @register.filter
 def is_in(value,arg):
-   return value in arg
+    return value in arg
 
 @register.filter
 def level_count(badges, level):
-   return badges.filter(level=level).count()
+    return badges.filter(level=level).count()
 
+@register.filter
+def level_title(level):
+    return level_choices[level]
 
+@register.filter('badge_count')
+def _badge_count(user_or_qs):
+    return badge_count(user_or_qs)
 
-
-
-
-
-
-
+@register.filter
+def number_awarded(badge, user_or_qs=None):
+    return badge.number_awarded(user_or_qs)

File badges/utils.py

 from badges.models import Badge as BadgeModel
 from badges.models import BadgeToUser
 
+from django.contrib.auth.models import User
 
 registered_badges = {}
 
         registered_badges[badge.id] = badge()
     return badge
 
+def badge_count(user_or_qs=None):
+    """
+    Given a user or queryset of users, this returns the badge
+    count at each badge level that the user(s) have earned.
+
+    Example:
+
+     >>> badge_count(User.objects.filter(username='admin'))
+     [{'count': 20, 'badge__level': u'1'}, {'count': 1, 'badge__level': u'2'}, {'count': 1, 'badge__level': u'3'}]
+
+    Uses a single database query.
+    """
+    kwargs = {}
+    if user_or_qs is None:
+        pass
+    elif isinstance(user_or_qs, User):
+        kwargs.update(dict(user=user_or_qs))
+    else:
+        kwargs.update(dict(user__in=user_or_qs))
+
+    return BadgeToUser.objects.filter(
+        **kwargs
+    ).values(
+        'badge__level',
+    ).annotate(
+        count=models.Count('badge__level'),
+    ).order_by('badge__level')
 
 class MetaBadgeMeta(type):