def badge_list(request):
    if request.user.is_authenticated():
        user_badges = set(
            (slug, level) for slug, level in
            BadgeAward.objects.filter(
                user=request.user
            ).values_list("slug", "level"))
    else:
        user_badges = []
    badges_awarded = BadgeAward.objects.values("slug", "level").annotate(
        num=Count("pk")
    )
    badges_dict = defaultdict(list)
    for badge in badges_awarded:
        badges_dict[badge["slug"]].append({
            "level": badge["level"],
            "name": badges._registry[badge["slug"]].levels[badge["level"]].name,
            "description": badges._registry[badge["slug"]].levels[badge["level"]].description,
            "count": badge["num"],
            "user_has": user_badges in (badge["slug"], badge["level"])
        })

    for badge_group in badges_dict.values():
        badge_group.sort(key=lambda o: o["level"])

    return render_to_response("brabeion/badges.html", {
        "badges": sorted(badges_dict.items()),
    }, context_instance=RequestContext(request))
dataset/ETHPy150Open eldarion/brabeion/brabeion/views.py/badge_list/swapped operands of `in`