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 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))