Esempio n. 1
0
File: tests.py Progetto: slok/dwarf
    def test_achievement_get(self):
        user_id = random.randrange(0, 10000)
        achievement_id = random.randrange(0, 10000)
        key = self.ACHIEVEMENTS_METRICS_FORMAT.format(achievement_id)

        a = AchievementMetrics(achievement_id)
        self.assertEquals(False, a.user_has_achievement(user_id))

        self.r.setbit(key, user_id, 1)
        self.assertEquals(True, a.user_has_achievement(user_id))
Esempio n. 2
0
File: tests.py Progetto: slok/dwarf
    def test_padawan_achievement(self):
        user = User()
        user.username = "******"
        user.save()

        achiv = Achievement()
        achiv.name = "padawan test"
        achiv.id = achievementsids.PADAWAN
        achiv.points = 0
        achiv.level = 0
        achiv.save()

        receivers.padawan_achievement(user)

        # Check
        self.assertTrue(UserAchievement.objects.filter(
                                                    user=user,
                                                    achievement=achiv
        ).exists())

        metrics = AchievementMetrics(achievementsids.PADAWAN)
        self.assertTrue(metrics.user_has_achievement(user.id))
Esempio n. 3
0
File: views.py Progetto: slok/dwarf
def list_achievements(request):

    # Global achievements
    achievements_tmp = Achievement.objects.all()
    users = User.objects.all().count()
    achievements = []

    # Login required page, not needed to check
    user = request.user

    for i in achievements_tmp:
        metrics = AchievementMetrics(i.id)
        total = metrics.total_users()
        try:
            percent = total * 100 / users
        except ZeroDivisionError:
            percent = 0

        own = metrics.user_has_achievement(user.id) == 1
        achievements.append((i, percent, own))

    # User achievements
    total_achievements_len = len(achievements_tmp)
    user_achievements_len = UserAchievement.objects.filter(user=user).count()
    try:
        user_percent = 100 * user_achievements_len / total_achievements_len
    except ZeroDivisionError:
        user_percent = 0

    context = {
        'achievements': achievements,
        'user_metrics': (total_achievements_len,
                         user_achievements_len,
                         user_percent)
    }

    return render_to_response('achievements/achievements.html',
                              context,
                              context_instance=RequestContext(request))