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))
def test_achievement_count(self): users = [random.randrange(0, 1000) for i in range(random.randrange(0, 1000))] users = set(users) achievement_id = random.randrange(0, 10000) key = self.ACHIEVEMENTS_METRICS_FORMAT.format(achievement_id) for user_id in users: self.r.setbit(key, user_id, 1) a = AchievementMetrics(achievement_id) self.assertEquals(len(users), a.total_users())
def test_achievement_set(self): user_id = random.randrange(0, 10000) achievement_id = random.randrange(0, 10000) key = self.ACHIEVEMENTS_METRICS_FORMAT.format(achievement_id) result = int(self.r.getbit(key, user_id)) self.assertEquals(0, result) a = AchievementMetrics(achievement_id) a.add_user_achievement(user_id) result = int(self.r.getbit(key, user_id)) self.assertEquals(1, result)
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))
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))