Пример #1
0
    def _setup_badge_settings(self):
        """
        Set up badges for all jobs and helpers (called from post_save handler).

        It adds the badge settings if badge creation is enabled and it is not there already.
        It also adds badge defaults to all jobs and badges to all helpers and coordinators if necessary.
        """
        # badge settings for event
        if not self.badge_settings:
            settings = BadgeSettings()
            settings.event = self
            settings.save()

        # badge defaults for jobs
        for job in self.job_set.all():
            if not job.badge_defaults:
                defaults = BadgeDefaults()
                defaults.save()

                job.badge_defaults = defaults
                job.save()

        # badge for helpers
        for helper in self.helper_set.all():
            if not hasattr(helper, 'badge'):
                badge = Badge()
                badge.event = self
                badge.helper = helper
                badge.save()
Пример #2
0
    def test_badge_associated_to_user(self):
        badge_type = BadgeType.objects.get(identifier="models_1")
        badge = Badge(proprietary=self.user, badge_type=badge_type)
        badge.save()

        self.assertEqual(1, self.user.badges.count())
        self.assertEqual(badge, self.user.badges.all()[0])
Пример #3
0
def user_creates_badge(username, title):
    user = get_user(username)
    badge = Badge(
        title = title, 
        description = "Example description", 
        creator = user
    )
    badge.save()
Пример #4
0
def award_badge(badge_identifier, user, model=None):
    """Create badge object related to user and model."""


    badge_type = BadgeType.objects.get(identifier=badge_identifier)
    badge = Badge(badge_type=badge_type, proprietary=user, model=model)
    try:
        badge.save()
    except Badge.AlreadyExists:
        pass
Пример #5
0
def helper_saved(sender, instance, using, **kwargs):
    """ Add badge and gifts to helper if necessary.

    This is a signal handler, that is called, when a helper is saved. It
    adds the badge if badge creation is enabled and it is not there already.
    """
    if instance.event:
        if instance.event.badges and not hasattr(instance, 'badge'):
            badge = Badge()
            badge.helper = instance
            badge.save()

        if instance.event.gifts and not hasattr(instance, 'gifts'):
            gifts = HelpersGifts()
            gifts.helper = instance
            gifts.save()
Пример #6
0
def event_saved(sender, instance, using, **kwargs):
    """ Add badge settings, badges and gifts if necessary.

    This is a signal handler, that is called, when a event is saved. It
    adds the badge settings if badge creation is enabled and it is not
    there already. It also adds badge defaults to all jobs and badges to all
    helpers and coordinators if necessary.
    """
    if instance.badges:
        # badge settings for event
        if not instance.badge_settings:
            settings = BadgeSettings()
            settings.event = instance
            settings.save()

        # badge defaults for jobs
        for job in instance.job_set.all():
            if not job.badge_defaults:
                defaults = BadgeDefaults()
                defaults.save()

                job.badge_defaults = defaults
                job.save()

        # badge for helpers
        for helper in instance.helper_set.all():
            if not hasattr(helper, 'badge'):
                badge = Badge()
                badge.helper = helper
                badge.save()

    if instance.gifts:
        for helper in instance.helper_set.all():
            if not hasattr(helper, 'gifts'):
                gifts = HelpersGifts()
                gifts.helper = helper
                gifts.save()

    if instance.inventory:
        if not instance.inventory_settings:
            InventorySettings.objects.create(event=instance)
Пример #7
0
class BadgeTests(TestCase):
    def setUp(self):
        self.c = Client()
        
        # Create the test badge.
        self.b = Badge( name = "The Super Badge",
                        shortname = "superbadge",
                        description = "This badge is quite super.",
                        graphic = "superbadge" )
        self.b.save()
        
        # Create some test users.
        self.u1 = User.objects.create_user("ishara",
                                           "*****@*****.**",
                                           "ishararulz")
        self.u2 = User.objects.create_user("alexis",
                                           "*****@*****.**",
                                           "alexisrulz")
        self.s1 = Student( user=self.u1 )
        self.s2 = Student( user=self.u2 )
        self.s1.save()
        self.s2.save()
        
        # Award the badge to s1
        self.b.awarded_to.add( self.s2 )
        self.b.save()
        
        # Give the test users memorable names.
        self.ishara = self.s1
        self.alexis = self.s2
    
    # Test the model's "held_by" method.
    def test_held_by(self):
        self.assertFalse( self.b.held_by( self.ishara ) )
        self.assertTrue( self.b.held_by( self.alexis ) )
    
    # When an anonymous user visits the badge list (/badges/) they see the
    # superbadge.
    def test_view_badge_list(self):
        response = self.c.get('/badges/')
        self.assertEqual(response.status_code, 200)
        response = self.c.get('/badges/superbadge/')
        self.assertEqual(response.status_code, 200)
    
    # When you go to a badge that doesn't exist, you get redirected back to
    # '/badges'.
    def test_badge_redirect(self):
        response = self.c.get('/badges/fakebadge/', follow=True)
        self.assertEqual(response.redirect_chain, [(u'http://testserver/badges/', 302)])

    # When Alexis visits the badge list, she sees a check mark next to
    # the superbadge.
    def test_view_badge_list_check(self):
        self.c.login(username="******", password="******")
        response = self.c.get('/badges/')
        self.assertEqual(response.status_code, 200)
        self.assertTrue( '<i class="icon-ok"></i>' in response.content )

    # When Ishara visits the badge list, he doesn't see a check mark next to
    # the superbadge.
    def test_view_badge_list_nocheck(self):
        self.c.login(username="******", password="******")
        response = self.c.get('/badges/')
        self.assertEqual(response.status_code, 200)
        self.assertFalse( '<i class="icon-ok"></i>' in response.content )
    
    # When Alexis visits the badge information, she can add the badge to her
    # backpack.
    def test_badge_backpack_button(self):
        self.c.login(username="******", password="******")
        response = self.c.get('/badges/superbadge/')
        self.assertEqual(response.status_code, 200)
        self.assertTrue( 'http://beta.openbadges.org/issuer.js' in response.content )

    # When Ishara visits the badge information, he can not add the badge to his
    # backpack.
    def test_view_badge_list_nocheck(self):
        self.c.login(username="******", password="******")
        response = self.c.get('/badges/superbadge/')
        self.assertEqual(response.status_code, 200)
        self.assertFalse( 'http://beta.openbadges.org/issuer.js' in response.content )    
    
    # Check to ensure that you get a 404 for the assertion for Ishara and a
    # JSON string for Alexis.
    def test_badge_assertion(self):
        response = self.c.get('/badges/superbadge/ishara/')
        self.assertEqual(response.status_code, 404)
        response = self.c.get('/badges/superbadge/ramen/')
        self.assertEqual(response.status_code, 404)
        response = self.c.get('/badges/superbadge/alexis/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/json')