From bc6fec67399090f44ce294d5c705f58437bd26f8 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Wed, 5 Jan 2011 02:00:47 -0500 Subject: added taxonomist badge and incremented the version --- askbot/__init__.py | 2 +- askbot/conf/badges.py | 9 +++++++++ askbot/models/__init__.py | 17 +++++++++++++---- askbot/models/badges.py | 11 +++++++++++ askbot/models/question.py | 8 ++++++++ askbot/models/signals.py | 4 +++- askbot/models/tag.py | 3 ++- askbot/tests/badge_tests.py | 9 +++++++++ 8 files changed, 56 insertions(+), 7 deletions(-) diff --git a/askbot/__init__.py b/askbot/__init__.py index 89397567..006f4498 100644 --- a/askbot/__init__.py +++ b/askbot/__init__.py @@ -19,4 +19,4 @@ def get_version(): """returns version of the askbot app this version is meaningful for pypi only """ - return '0.6.53' + return '0.6.54' diff --git a/askbot/conf/badges.py b/askbot/conf/badges.py index b4d80a7b..3db0cca0 100644 --- a/askbot/conf/badges.py +++ b/askbot/conf/badges.py @@ -210,3 +210,12 @@ settings.register( description=_('Commentator: minimum comments') ) ) + +settings.register( + IntegerValue( + BADGES, + 'TAXONOMIST_BADGE_MIN_USE_COUNT', + default = 10, + description = _('Taxonomist: minimum tag use count') + ) +) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 97d1f9ac..fc2f8602 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -1963,12 +1963,21 @@ def record_flag_offensive(instance, mark_by, **kwargs): ) activity.add_recipients(recipients) -def record_update_tags(question, **kwargs): +def record_update_tags(question, tags, user, timestamp, **kwargs): """ - when user updated tags of the question + This function sends award badges signal on each updated tag + the badges that respond to the 'ta """ + for tag in tags: + award_badges_signal.send(None, + event = 'update_tag', + actor = user, + context_object = tag, + timestamp = timestamp + ) + activity = Activity( - user=question.author, + user=user, active_at=datetime.datetime.now(), content_object=question, activity_type=const.TYPE_ACTIVITY_UPDATE_TAGS, @@ -2053,7 +2062,7 @@ signals.delete_question_or_answer.connect(record_delete_question, sender=Questio signals.delete_question_or_answer.connect(record_delete_question, sender=Answer) signals.flag_offensive.connect(record_flag_offensive, sender=Question) signals.flag_offensive.connect(record_flag_offensive, sender=Answer) -signals.tags_updated.connect(record_update_tags, sender=Question) +signals.tags_updated.connect(record_update_tags) signals.user_updated.connect(record_user_full_updated, sender=User) signals.user_logged_in.connect(post_stored_anonymous_content) signals.post_updated.connect( diff --git a/askbot/models/badges.py b/askbot/models/badges.py index cd35c232..9ac11c18 100644 --- a/askbot/models/badges.py +++ b/askbot/models/badges.py @@ -743,6 +743,16 @@ class Taxonomist(Badge): description = _('Created a tag used by 50 questions') ) + def consider_award(self, actor = None, + context_object = None, timestamp = None): + + tag = context_object + taxonomist_threshold = askbot_settings.TAXONOMIST_BADGE_MIN_USE_COUNT + #the "-1" is used because tag counts are updated in a bulk query + #that does not update the value in the python object + if tag.used_count == taxonomist_threshold - 1: + self.award(tag.created_by, tag, timestamp) + class Expert(Badge): """Stub badge""" def __init__(self): @@ -821,6 +831,7 @@ EVENTS_TO_BADGES = { 'post_answer': (Necromancer,), 'post_comment': (Commentator,), 'retag_question': (Organizer,), + 'update_tag': (Taxonomist,), 'select_favorite_question': (FavoriteQuestion, StellarQuestion,), 'update_user_profile': (Autobiographer,), 'upvote_answer': ( diff --git a/askbot/models/question.py b/askbot/models/question.py index d8d60c6e..73b0323e 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -17,6 +17,7 @@ from askbot.models.tag import Tag, MarkedTag from askbot.models.base import AnonymousContent, DeletableContent, ContentRevision from askbot.models.base import parse_post_text, parse_and_save_post from askbot.models import content +from askbot.models import signals from askbot import const from askbot.utils.lists import LazyList from askbot.utils.slug import slugify @@ -428,6 +429,7 @@ class Question(content.Content, DeletableContent): if tag.used_count == 1: #we won't modify used count b/c it's done below anyway removed_tags.remove(tag) + #todo - do we need to use fields deleted_by and deleted_at? tag.delete()#auto-delete tags whose use count dwindled #remember modified tags, we'll need to update use counts on them @@ -466,6 +468,12 @@ class Question(content.Content, DeletableContent): #if there are any modified tags, update their use counts if modified_tags: Tag.objects.update_use_counts(modified_tags) + signals.tags_updated.send(None, + question = self, + tags = modified_tags, + user = user, + timestamp = timestamp + ) return True return False diff --git a/askbot/models/signals.py b/askbot/models/signals.py index 825f72e7..cfd75294 100644 --- a/askbot/models/signals.py +++ b/askbot/models/signals.py @@ -1,6 +1,8 @@ import django.dispatch -tags_updated = django.dispatch.Signal(providing_args=['question']) +tags_updated = django.dispatch.Signal( + providing_args=['tags', 'user', 'timestamp'] + ) #todo: this one seems to be unused edit_question_or_answer = django.dispatch.Signal( diff --git a/askbot/models/tag.py b/askbot/models/tag.py index 473c3cd7..1d1eba49 100644 --- a/askbot/models/tag.py +++ b/askbot/models/tag.py @@ -3,6 +3,7 @@ from django.db import connection, transaction from django.contrib.auth.models import User from django.utils.translation import ugettext as _ from askbot.models.base import DeletableContent +from askbot.conf import settings as askbot_settings class TagManager(models.Manager): @@ -27,8 +28,8 @@ class TagManager(models.Manager): cursor = connection.cursor() query = self.UPDATE_USED_COUNTS_QUERY % ','.join(['%s'] * len(tags)) cursor.execute(query, [tag.id for tag in tags]) - transaction.commit_unless_managed() + transaction.commit_unless_managed() def get_related_to_search( self, questions=None, diff --git a/askbot/tests/badge_tests.py b/askbot/tests/badge_tests.py index d7f2de30..3d79aa3a 100644 --- a/askbot/tests/badge_tests.py +++ b/askbot/tests/badge_tests.py @@ -481,3 +481,12 @@ class BadgeTests(AskbotTestCase): self.assert_have_badge('commentator', self.u1, 1) self.post_comment(user = self.u1, parent_post = question) self.assert_have_badge('commentator', self.u1, 1) + + def test_taxonomist_badge(self): + self.post_question(user = self.u1, tags = 'test') + min_use = settings.TAXONOMIST_BADGE_MIN_USE_COUNT + for i in xrange(min_use - 2): + self.post_question(user = self.u2, tags = 'test') + self.assert_have_badge('taxonomist', self.u1, 0) + self.post_question(user = self.u2, tags = 'test') + self.assert_have_badge('taxonomist', self.u1, 1) -- cgit v1.2.3-1-g7c22