Exemple #1
0
def record_favorite_question(instance, created, **kwargs):
    """
    when user add the question in him favorite questions list.
    """
    if created:
        activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)
        activity.save()
Exemple #2
0
def record_answer_event(instance, created, **kwargs):
    if created:
        q_author = instance.question.author
        found_match = False
        for m in q_author.message_set.all():
            match = record_answer_event_re.search(m.message)
            if match:
                found_match = True
                try:
                    cnt = int(match.group(1))
                except:
                    cnt = 1
                m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
                            % (cnt+1, q_author.get_profile_url())
                m.save()
                break
        if not found_match:
            msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
                    % q_author.get_profile_url()
            q_author.message_set.create(message=msg)

        activity = Activity(user=instance.author, \
                            active_at=instance.added_at,\
                            content_object=instance, \
                            activity_type=TYPE_ACTIVITY_ANSWER)
        activity.save()
Exemple #3
0
def record_answer_event(instance, created, **kwargs):
    if created:
        q_author = instance.question.author
        found_match = False
        # print 'going through %d messages' % q_author.message_set.all().count()
        for m in q_author.message_set.all():
            # print m.message
            match = record_answer_event_re.search(m.message)
            if match:
                found_match = True
                try:
                    cnt = int(match.group(1))
                except:
                    cnt = 1
                m.message = u'You have received %d <a href="%s?sort=responses">new responses</a>.' % (
                    cnt + 1,
                    q_author.get_profile_url(),
                )
                # print 'updated message'
                # print m.message
                m.save()
                break
        if not found_match:
            msg = u'You have received a <a href="%s?sort=responses">new response</a>.' % q_author.get_profile_url()
            # print 'new message'
            # print msg
            q_author.message_set.create(message=msg)

        activity = Activity(
            user=instance.author,
            active_at=instance.added_at,
            content_object=instance,
            activity_type=TYPE_ACTIVITY_ANSWER,
        )
        activity.save()
Exemple #4
0
def record_answer_accepted(instance, created, **kwargs):
    """
    when answer is accepted, we record this for question author - who accepted it.
    """
    if not created and instance.accepted:
        activity = Activity(user=instance.question.author, active_at=datetime.datetime.now(), \
            content_object=instance, activity_type=TYPE_ACTIVITY_MARK_ANSWER)
        activity.save()
Exemple #5
0
def record_user_full_updated(instance, **kwargs):
    activity = Activity(
        user=instance,
        active_at=datetime.datetime.now(),
        content_object=instance,
        activity_type=TYPE_ACTIVITY_USER_FULL_UPDATED,
    )
    activity.save()
Exemple #6
0
def record_mark_offensive(instance, mark_by, **kwargs):
    activity = Activity(
        user=mark_by,
        active_at=datetime.datetime.now(),
        content_object=instance,
        activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE,
    )
    activity.save()
Exemple #7
0
def record_revision_answer_event(instance, created, **kwargs):
    if created and instance.revision <> 1:
        activity = Activity(
            user=instance.author,
            active_at=instance.revised_at,
            content_object=instance,
            activity_type=TYPE_ACTIVITY_UPDATE_ANSWER,
        )
        activity.save()
Exemple #8
0
def record_ask_event(instance, created, **kwargs):
    if created:
        activity = Activity(
            user=instance.author,
            active_at=instance.added_at,
            content_object=instance,
            activity_type=TYPE_ACTIVITY_ASK_QUESTION,
        )
        activity.save()
Exemple #9
0
def record_delete_question(instance, delete_by, **kwargs):
    """
    when user deleted the question
    """
    if instance.__class__ == "Question":
        activity_type = TYPE_ACTIVITY_DELETE_QUESTION
    else:
        activity_type = TYPE_ACTIVITY_DELETE_ANSWER

    activity = Activity(user=delete_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=activity_type)
    activity.save()
Exemple #10
0
def record_comment_event(instance, created, **kwargs):
    if created:
        from django.contrib.contenttypes.models import ContentType
        question_type = ContentType.objects.get_for_model(Question)
        question_type_id = question_type.id
        if (instance.content_type_id == question_type_id):
            type = TYPE_ACTIVITY_COMMENT_QUESTION
        else:
            type = TYPE_ACTIVITY_COMMENT_ANSWER
        activity = Activity(user=instance.user, active_at=instance.added_at, content_object=instance, activity_type=type)
        activity.save()
Exemple #11
0
def record_update_tags(question, **kwargs):
    """
    when user updated tags of the question
    """
    activity = Activity(
        user=question.author,
        active_at=datetime.datetime.now(),
        content_object=question,
        activity_type=TYPE_ACTIVITY_UPDATE_TAGS,
    )
    activity.save()
Exemple #12
0
def record_cancel_vote(instance, **kwargs):
    """
    when user canceled vote, the vote will be deleted.
    """
    activity = Activity(
        user=instance.user,
        active_at=datetime.datetime.now(),
        content_object=instance,
        activity_type=TYPE_ACTIVITY_CANCEL_VOTE,
    )
    activity.save()
Exemple #13
0
def record_vote(instance, created, **kwargs):
    """
    when user have voted
    """
    if created:
        if instance.vote == 1:
            vote_type = TYPE_ACTIVITY_VOTE_UP
        else:
            vote_type = TYPE_ACTIVITY_VOTE_DOWN

        activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=vote_type)
        activity.save()
Exemple #14
0
def record_award_event(instance, created, **kwargs):
    """
    After we awarded a badge to user, we need to record this activity and notify user.
    We also recaculate awarded_count of this badge and user information.
    """
    if created:
        activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,
            activity_type=TYPE_ACTIVITY_PRIZE)
        activity.save()

        instance.badge.awarded_count += 1
        instance.badge.save()

        if instance.badge.type == Badge.GOLD:
            instance.user.gold += 1
        if instance.badge.type == Badge.SILVER:
            instance.user.silver += 1
        if instance.badge.type == Badge.BRONZE:
            instance.user.bronze += 1
        instance.user.save()