def test_level_one(self):
        # Requires 10 upvotes on an answer
        badge = feedback_badges.LevelOneAnswerVoteCountBadge()

        video = self.make_video()
        asker = self.make_user_data('*****@*****.**')
        question = self.make_question("What's up, Black Widow?", video, asker)
        answerer = self.make_user_data('*****@*****.**')
        answer = self.make_answer("Just saving the world!", question, answerer)

        votes = {
            0: False,
            1: False,
            8: False,
            9: True,  # implicit 1 vote, so this is really "10 votes"
            10: True,
            1000: True,
        }

        for sv in votes:
            question.sum_votes = sv
            question.put()
            answer.sum_votes = sv
            answer.put()

            # Questions don't earn answer badges ever
            self.assertFalse(badge.is_satisfied_by(feedback=question))
            self.assertEqual(votes[sv], badge.is_satisfied_by(feedback=answer))

        self.assertFalse(badge.is_already_owned_by(answerer, feedback=answer))
        badge.award_to(answerer, feedback=answer)
        self.assertTrue(badge.is_already_owned_by(answerer, feedback=answer))
Пример #2
0
def all_badges():
    list_badges = [
        exercise_completion_count_badges.GettingStartedBadge(),
        exercise_completion_count_badges.MakingProgressBadge(),
        exercise_completion_count_badges.HardAtWorkBadge(),
        exercise_completion_count_badges.WorkHorseBadge(),
        exercise_completion_count_badges.MagellanBadge(),
        exercise_completion_count_badges.CopernicusBadge(),
        exercise_completion_count_badges.KeplerBadge(),
        exercise_completion_count_badges.AtlasBadge(),
        points_badges.TenThousandaireBadge(),
        points_badges.HundredThousandaireBadge(),
        points_badges.FiveHundredThousandaireBadge(),
        points_badges.MillionaireBadge(),
        points_badges.TenMillionaireBadge(),
        streak_badges.NiceStreakBadge(),
        streak_badges.GreatStreakBadge(),
        streak_badges.AwesomeStreakBadge(),
        streak_badges.RidiculousStreakBadge(),
        streak_badges.LudicrousStreakBadge(),
        topic_time_badges.NiceTopicTimeBadge(),
        topic_time_badges.GreatTopicTimeBadge(),
        topic_time_badges.AwesomeTopicTimeBadge(),
        topic_time_badges.RidiculousTopicTimeBadge(),
        topic_time_badges.LudicrousTopicTimeBadge(),
        timed_problem_badges.NiceTimedProblemBadge(),
        timed_problem_badges.GreatTimedProblemBadge(),
        timed_problem_badges.AwesomeTimedProblemBadge(),
        timed_problem_badges.RidiculousTimedProblemBadge(),
        timed_problem_badges.LudicrousTimedProblemBadge(),
        recovery_problem_badges.RecoveryBadge(),
        recovery_problem_badges.ResurrectionBadge(),
        unfinished_exercise_badges.SoCloseBadge(),
        unfinished_exercise_badges.KeepFightingBadge(),
        unfinished_exercise_badges.UndeterrableBadge(),
        power_time_badges.PowerFifteenMinutesBadge(),
        power_time_badges.PowerHourBadge(),
        power_time_badges.DoublePowerHourBadge(),
        profile_badges.ProfileCustomizationBadge(),
        exercise_completion_badges.LevelOneArithmeticianBadge(),
        exercise_completion_badges.LevelTwoArithmeticianBadge(),
        exercise_completion_badges.LevelThreeArithmeticianBadge(),
        exercise_completion_badges.TopLevelArithmeticianBadge(),
        exercise_completion_badges.LevelOneTrigonometricianBadge(),
        exercise_completion_badges.LevelTwoTrigonometricianBadge(),
        exercise_completion_badges.LevelThreeTrigonometricianBadge(),
        exercise_completion_badges.TopLevelTrigonometricianBadge(),
        exercise_completion_badges.LevelOnePrealgebraistBadge(),
        exercise_completion_badges.LevelTwoPrealgebraistBadge(),
        exercise_completion_badges.LevelThreePrealgebraistBadge(),
        exercise_completion_badges.TopLevelPrealgebraistBadge(),
        exercise_completion_badges.LevelOneAlgebraistBadge(),
        exercise_completion_badges.LevelTwoAlgebraistBadge(),
        exercise_completion_badges.LevelThreeAlgebraistBadge(),
        exercise_completion_badges.LevelFourAlgebraistBadge(),
        exercise_completion_badges.LevelFiveAlgebraistBadge(),
        exercise_completion_badges.TopLevelAlgebraistBadge(),
        tenure_badges.YearOneBadge(),
        tenure_badges.YearTwoBadge(),
        tenure_badges.YearThreeBadge(),
        video_time_badges.ActOneSceneOneBadge(),
        consecutive_activity_badges.FiveDayConsecutiveActivityBadge(),
        consecutive_activity_badges.FifteenDayConsecutiveActivityBadge(),
        consecutive_activity_badges.ThirtyDayConsecutiveActivityBadge(),
        consecutive_activity_badges.HundredDayConsecutiveActivityBadge(),
        feedback_badges.LevelOneAnswerVoteCountBadge(),
        feedback_badges.LevelTwoAnswerVoteCountBadge(),
        feedback_badges.LevelThreeAnswerVoteCountBadge(),
        feedback_badges.LevelOneQuestionVoteCountBadge(),
        feedback_badges.LevelTwoQuestionVoteCountBadge(),
        feedback_badges.LevelThreeQuestionVoteCountBadge(),
        discussion_badges.FirstFlagBadge(),
        discussion_badges.FirstUpVoteBadge(),
        discussion_badges.FirstDownVoteBadge(),
        discussion_badges.ModeratorBadge(),
    ]

    # Add custom badges and topic exercise badges, which both correspond
    # to datastore entities, to the collection of all badges.
    list_badges.extend(custom_badges.CustomBadge.all())
    list_badges.extend(topic_exercise_badges.TopicExerciseBadge.all())

    return list_badges
Пример #3
0
def get_user_discussion_badges(user_data):
    """Returns the discussion badges earned by the user,
    sorted from more prestigious to less prestigious.
    """
    # TODO(ankit): Find a less static way to fetch
    # the discussion badges.
    discussion_badges_list = [
        feedback_badges.LevelOneAnswerVoteCountBadge(),
        feedback_badges.LevelTwoAnswerVoteCountBadge(),
        feedback_badges.LevelThreeAnswerVoteCountBadge(),
        feedback_badges.LevelOneQuestionVoteCountBadge(),
        feedback_badges.LevelTwoQuestionVoteCountBadge(),
        feedback_badges.LevelThreeQuestionVoteCountBadge(),
        discussion_badges.FirstFlagBadge(),
        discussion_badges.FirstUpVoteBadge(),
        discussion_badges.FirstDownVoteBadge(),
        discussion_badges.ModeratorBadge(),
        discussion_quality_count_badges.LevelOneAnswerQualityCountBadge(),
        discussion_quality_count_badges.LevelTwoAnswerQualityCountBadge(),
        discussion_quality_count_badges.LevelOneQuestionQualityCountBadge(),
        discussion_quality_count_badges.LevelTwoQuestionQualityCountBadge(),
    ]

    discussion_badges_map = {}

    for badge in discussion_badges_list:
        discussion_badges_map[badge.name] = badge

    badges = models_badges.UserBadge.get_for(user_data)

    # Only discussion badges.
    user_badges_dict = {}

    for badge in badges:
        badge_name = badge.badge_name

        if badge_name in discussion_badges_map:
            discussion_badge = discussion_badges_map[badge_name]

            if badge_name in user_badges_dict:
                user_badges_dict[badge_name]['count'] += 1
                user_badges_dict[badge_name]['multiple'] = True

                try:
                    if badge.target_context:
                        user_badges_dict[badge_name]['feedback_keys'].append(
                            badge.target_context.key())
                except db.ReferencePropertyResolveError:
                    pass
            else:
                user_badge = {}
                user_badge['icon_src'] = discussion_badge.icon_src
                user_badge['absolute_url'] = discussion_badge.absolute_url
                user_badge['description'] = (
                    discussion_badge.safe_extended_description)
                user_badge['name'] = discussion_badge.description
                user_badge['category'] = discussion_badge.badge_category
                user_badge['count'] = 1
                user_badge['multiple'] = False

                # Do a silent fail in case of a
                # db.ReferencePropertyResolveError error.
                # Occurs when the badge has a dangling reference to a
                # feedback item that was deleted.
                try:
                    if badge.target_context:
                        user_badge['feedback_keys'] = [
                            badge.target_context.key()
                        ]
                except db.ReferencePropertyResolveError:
                    # TODO(ankit): Maybe set target_context to None here?
                    pass

                user_badges_dict[badge_name] = user_badge

    user_discussion_badges = user_badges_dict.values()

    # Sort the badges from more prestigious -> less prestigious.
    key_fxn = lambda entity: entity['category']
    user_discussion_badges = sorted(user_discussion_badges,
                                    key=key_fxn,
                                    reverse=True)

    return user_discussion_badges