示例#1
0
文件: badges.py 项目: Faldrian/kuma
def on_revision_save(sender, **kwargs):
    if not waffle.switch_is_active('badger'):
        return

    o = kwargs['instance']
    created = kwargs['created']

    # Reward the first revision of a new document
    first_document = get_badge('first-document')
    if not first_document.is_awarded_to(o.creator):
        if o.document.revisions.count() == 1:
            first_document.award_to(o.creator)

    # Reward the first edit of any document
    first_edit = get_badge('first-edit')
    if not first_edit.is_awarded_to(o.creator):
        first_edit.award_to(o.creator)

    # Reward the fifth revision
    fifth_edit = get_badge('fifth-edit')
    if not fifth_edit.is_awarded_to(o.creator):
        p = fifth_edit.progress_for(o.creator).increment_by(1)
        if p.counter >= 5:
            fifth_edit.award_to(o.creator)
            p.delete()
示例#2
0
def on_revision_save(sender, **kwargs):
    if not waffle.switch_is_active('badger'):
        return

    o = kwargs['instance']
    created = kwargs['created']

    # Reward the first revision of a new document
    first_document = get_badge('first-document')
    if not first_document.is_awarded_to(o.creator):
        if o.document.revisions.count() == 1:
            first_document.award_to(o.creator)

    # Reward the first edit of any document
    first_edit = get_badge('first-edit')
    if not first_edit.is_awarded_to(o.creator):
        first_edit.award_to(o.creator)

    # Reward the fifth revision
    fifth_edit = get_badge('fifth-edit')
    if not fifth_edit.is_awarded_to(o.creator):
        p = fifth_edit.progress_for(o.creator).increment_by(1)
        if p.counter >= 5:
            fifth_edit.award_to(o.creator)
            p.delete()
示例#3
0
 def test_badges_from_fixture(self):
     """Badges can be created via fixture"""
     b = get_badge("test-1")
     eq_("Test #1", b.title)
     b = get_badge("button-clicker")
     eq_("Button Clicker", b.title)
     b = get_badge("first-post")
     eq_("First post!", b.title)
示例#4
0
 def test_badges_from_fixture(self):
     """Badges can be created via fixture"""
     b = get_badge("test-1")
     eq_("Test #1", b.title)
     b = get_badge("button-clicker")
     eq_("Button Clicker", b.title)
     b = get_badge("first-post")
     eq_("First post!", b.title)
示例#5
0
 def test_badges_from_code(self):
     """Badges can be created in code"""
     b = get_badge("test-2")
     eq_("Test #2", b.title)
     b = get_badge("awesomeness")
     eq_("Awesomeness (you have it)", b.title)
     b = get_badge("250-words")
     eq_("250 Words", b.title)
     b = get_badge("master-badger")
     eq_("Master Badger", b.title)
示例#6
0
 def test_badges_from_code(self):
     """Badges can be created in code"""
     b = get_badge("test-2")
     eq_("Test #2", b.title)
     b = get_badge("awesomeness")
     eq_("Awesomeness (you have it)", b.title)
     b = get_badge("250-words")
     eq_("250 Words", b.title)
     b = get_badge("master-badger")
     eq_("Master Badger", b.title)
示例#7
0
def update_points_end_badge(sender, old_score, new_score, id_instance, type_instance,**kwargs):

	wrapsito = ''
	badge = ''
	instance = ''

	if type_instance == 'Activity':
		instance = ActivitieParent.objects.get(id = id_instance)
		wrapsito = Activitie_wrap.objects.get(activitie = instance)
		badge = get_badge(wrapsito.module.slug)
	else:
		instance = Quiz.objects.get(id = id_instance)
		wrapsito = Quiz_wrap.objects.get(quiz = id_instance)
		badge = get_badge(wrapsito.module.slug)

	badge.points_end -= old_score
	badge.points_end += new_score

	badge.save()

	# recalculo los progresos 
	# se obtienen los progresos de los usuarios en esa medalla
	progress = Progress.objects.filter(badge=badge)
	for item in progress:
		if type_instance == 'Activity':

			activitie_child = ActivitieChild.objects.filter(parent = instance, author = item.user, status = 3)
			#si ya lo realizo se le restan los puntos asignados si no se recalcula el progreso 
			if len(activitie_child)>0:
				item.counter -= old_score
				item.counter += new_score
				item.update_percent2()
				item.save()
				if item.percent >= 100:
					gamification_badge_award.send(sender = set_points, badge = badge, user = item.user)
					action.send(user, verb=u'ganó una medalla', action_object=badge, target=badge)
			else:
				item.update_percent2()

		elif type_instance == 'Quiz':
			# se verifica si el usuario realizo y aprobo el quiz
			sitting = Sitting.objects.filter(quiz = instance, user = item.user, complete = True, check_if_passed = True)
			#si ya lo realizo se le restan los puntos asignados si no se recalcula el progreso 
			if len(sitting) > 0:
				item.counter -= old_score
				item.counter += new_score
				item.update_percent2()
				item.save()
				if item.percent >= 100:
					gamification_badge_award.send(sender = set_points, badge = badge, user = item.user)
					action.send(user, verb=u'ganó una medalla', action_object=badge, target=badge)
			else:
				item.update_percent2()
示例#8
0
def set_points_quiz(sender, sitting, badge, **kwargs):
	
	# si aprobo el quiz 
	if sitting.check_if_passed == True:
		print 'puntos quices'
		
		# se trae la medalla y el progreso del usario en esa medalla
		b = get_badge(badge)
		p = b.progress_for(sitting.user)

		# se consulta cuantos puntos tiene ese quiz 
		points = Scores.objects.get(id_event=sitting.quiz.id, event='Quiz')

		# se llama a la funcion para asigna los puntos en el progreso 
		set_points(p, points.score, b, sitting.user)
		# se registra la accion de que hizo una actividad
		action.send(sitting.user, verb=u'Aprobó', action_object=sitting.quiz, target=sitting.quiz)	

		questions = sitting.incorrect_questions.split(',')
		if len(sitting.incorrect_questions) > 0:
			for question in questions:
				if question != '':
					tipo = str(ContentType.objects.get_for_model (Question.objects.get_subclass(id = question)))
					if tipo == 'Essay style question':
						finish_quiz.send(sender=set_points_quiz, sitting=sitting)
示例#9
0
    def test_badge_awarded_on_content(self):
        """A badge should be awarded upon 250 words worth of guestbook posts
        created"""
        user = self._get_user()

        b = get_badge('250-words')

        # Post 5 words in progress...
        GuestbookEntry.objects.create(creator=user,
            message="A few words to start")
        ok_(not b.is_awarded_to(user))
        eq_(5, b.progress_for(user).counter)

        # Post 5 more words in progress...
        GuestbookEntry.objects.create(creator=user,
            message="A few more words posted")
        ok_(not b.is_awarded_to(user))
        eq_(10, b.progress_for(user).counter)

        # Post 90 more...
        msg = ' '.join('lots of words that repeat' for x in range(18))
        GuestbookEntry.objects.create(creator=user, message=msg)
        ok_(not b.is_awarded_to(user))
        eq_(100, b.progress_for(user).counter)

        # And 150 more for the finale...
        msg = ' '.join('lots of words that repeat' for x in range(30))
        GuestbookEntry.objects.create(creator=user, message=msg)

        # Should result in a badge award and reset progress.
        ok_(b.is_awarded_to(user))
        eq_(0, b.progress_for(user).counter)

        # But, just checking the reset counter shouldn't create a new DB row.
        eq_(0, Progress.objects.filter(user=user, badge=b).count())
示例#10
0
    def test_badge_awarded_on_content(self):
        """A badge should be awarded upon 250 words worth of guestbook posts
        created"""
        user = self._get_user()

        b = get_badge('250-words')

        # Post 5 words in progress...
        GuestbookEntry.objects.create(creator=user,
            message="A few words to start")
        ok_(not b.is_awarded_to(user))
        eq_(5, b.progress_for(user).counter)

        # Post 5 more words in progress...
        GuestbookEntry.objects.create(creator=user,
            message="A few more words posted")
        ok_(not b.is_awarded_to(user))
        eq_(10, b.progress_for(user).counter)

        # Post 90 more...
        msg = ' '.join('lots of words that repeat' for x in range(18))
        GuestbookEntry.objects.create(creator=user, message=msg)
        ok_(not b.is_awarded_to(user))
        eq_(100, b.progress_for(user).counter)

        # And 150 more for the finale...
        msg = ' '.join('lots of words that repeat' for x in range(30))
        GuestbookEntry.objects.create(creator=user, message=msg)

        # Should result in a badge award and reset progress.
        ok_(b.is_awarded_to(user))
        eq_(0, b.progress_for(user).counter)

        # But, just checking the reset counter shouldn't create a new DB row.
        eq_(0, Progress.objects.filter(user=user, badge=b).count())
示例#11
0
    def process_request(self, request):
        if not waffle.flag_is_active(request, 'badger'):
            return None
        if not hasattr(request, 'user'):
            return None
        if not request.user.is_authenticated():
            return None

        b_learner = get_badge('learner')
        if ('/learn' in request.path
                and not b_learner.is_awarded_to(request.user)):
            b_learner.award_to(request.user)

        b_dev_derbier = get_badge('dev-derbier')
        if ('/demos/devderby' in request.path
                and not b_dev_derbier.is_awarded_to(request.user)):
            b_dev_derbier.award_to(request.user)

        return None
示例#12
0
文件: badges.py 项目: Faldrian/kuma
    def process_request(self, request):
        if not waffle.flag_is_active(request, 'badger'):
            return None
        if not hasattr(request, 'user'):
            return None
        if not request.user.is_authenticated():
            return None

        b_learner = get_badge('learner')
        if ('/learn' in request.path and
                not b_learner.is_awarded_to(request.user)):
            b_learner.award_to(request.user)

        b_dev_derbier = get_badge('dev-derbier')
        if ('/demos/devderby' in request.path and
                not b_dev_derbier.is_awarded_to(request.user)):
            b_dev_derbier.award_to(request.user)

        return None
示例#13
0
    def test_badge_awarded_on_model_create(self):
        """A badge should be awarded on first guestbook post"""
        user = self._get_user()
        post = GuestbookEntry(message="This is my first post", creator=user)
        post.save()
        b = get_badge('first-post')
        ok_(b.is_awarded_to(user))

        # "first-post" badge should be unique
        post = GuestbookEntry(message="This is my first post", creator=user)
        post.save()
        eq_(1, Award.objects.filter(user=user, badge=b).count())
示例#14
0
def set_points_activitie(sender, user, badge, activitie, **kwargs):
	# se trae la medalla y el progreso del usario en esa medalla
	b = get_badge(badge)
	p = b.progress_for(user)
	# se consulta cuantos puntos tiene ese quiz 
	points = Scores.objects.get(id_event=activitie.id, event="Activity")

	# se llama a la funcion para asigna los puntos en el progreso 
	set_points(p, points.score, b, user)
	
	# se registra la accion de que hizo una actividad
	action.send(user, verb=u'Aprobó una actividad', action_object=activitie, target=activitie)
示例#15
0
    def test_badge_awarded_on_model_create(self):
        """A badge should be awarded on first guestbook post"""
        user = self._get_user()
        post = GuestbookEntry(message="This is my first post", creator=user)
        post.save()
        b = get_badge('first-post')
        ok_(b.is_awarded_to(user))

        # "first-post" badge should be unique
        post = GuestbookEntry(message="This is my first post", creator=user)
        post.save()
        eq_(1, Award.objects.filter(user=user, badge=b).count())
示例#16
0
def on_guestbook_post(sender, **kwargs):
    o = kwargs['instance']
    created = kwargs['created']

    if created:
        award_badge('first-post', o.creator)

    # Increment progress counter and track the completion condition ourselves.
    b = get_badge('250-words')
    p = b.progress_for(o.creator).increment_by(o.word_count)
    if p.counter >= 250:
        b.award_to(o.creator)

    # Update percentage from total word count, and Progress will award on 100%
    total_word_count = (GuestbookEntry.objects.filter(creator=o.creator)
                        .aggregate(s=Sum('word_count'))['s'])
    (get_progress("250-words-by-percent", o.creator)
           .update_percent(total_word_count, 250))
示例#17
0
def points_end_badge(sender, author, badge, points, action, element, instance_element, **kwargs):
	# se obtiene la medalla 
	b = get_badge(badge)
	
	#se compara si se va a agregar o eliminar un elemento para actualizar los puntos con los que se gana una medalla
	if action == 'add':
		b.points_end += points
		b.save()
		# se verifica si se deben borrar medallas asignadas 
		verify_remove_for_award(b, element, instance_element)
		create_remove_action.send(sender=verify_remove_for_award, author=author, action= 'add', instance = instance_element)
	elif action == 'remove':
		if b.points_end > 0:
			b.points_end -= points
			b.save()
		# se verifica el elemento que se borra y se recalcula el progreso teniendo en cuenta si gana o no la medalla
		verify_users_for_award(b, element, instance_element)
		create_remove_action.send(sender=verify_remove_for_award, author=author, action= 'remove', instance = instance_element)
示例#18
0
def on_guestbook_post(sender, **kwargs):
    o = kwargs['instance']
    created = kwargs['created']

    if created:
        award_badge('first-post', o.creator)

    # Increment progress counter and track the completion condition ourselves.
    b = get_badge('250-words')
    p = b.progress_for(o.creator).increment_by(o.word_count)
    if p.counter >= 250:
        b.award_to(o.creator)

    # Update percentage from total word count, and Progress will award on 100%
    total_word_count = (GuestbookEntry.objects.filter(
        creator=o.creator).aggregate(s=Sum('word_count'))['s'])
    (get_progress("250-words-by-percent",
                  o.creator).update_percent(total_word_count, 250))
示例#19
0
    def test_metabadge_awarded(self):
        """Upon completing collection of badges, award a meta-badge"""
        user = self._get_user()

        # Cover a few bases on award creation...
        award_badge('test-1', user)
        award_badge('test-2', user)
        a = Award(badge=get_badge('button-clicker'), user=user)
        a.save()
        Award.objects.create(badge=get_badge('awesomeness'), user=user)

        ok_(get_badge('master-badger').is_awarded_to(user))

        get_badge('awesomeness').award_to(user)
        eq_(1, Award.objects.filter(badge=get_badge("master-badger"),
                                    user=user).count())
示例#20
0
    def test_metabadge_awarded(self):
        """Upon completing collection of badges, award a meta-badge"""
        user = self._get_user()

        ok_(not get_badge('master-badger').is_awarded_to(user))

        # Cover a few bases on award creation...
        award_badge('test-1', user)
        award_badge('test-2', user)
        a = Award(badge=get_badge('button-clicker'), user=user)
        a.save()

        get_badge('awesomeness').award_to(user)
        eq_(1, Award.objects.filter(badge=get_badge("master-badger"),
                                    user=user).count())
        
        ok_(get_badge('master-badger').is_awarded_to(user))