Exemplo n.º 1
0
    def delete(self, *args, **kwargs):
        """Override delete method to update parent question info."""
        question = Question.uncached.get(pk=self.question.id)
        if question.last_answer == self:
            answers = question.answers.all().order_by('-created')
            try:
                question.last_answer = answers[1]
            except IndexError:
                # The question has only one answer
                question.last_answer = None
        if question.solution == self:
            question.solution = None

            # Delete karma solution action.
            SolutionAction(user=self.creator, day=self.created).delete()

        # Delete karma answer action and first answer action if it was first.
        AnswerAction(user=self.creator, day=self.created).delete()
        if self.id == question.answers.all().order_by('created')[0].id:
            FirstAnswerAction(user=self.creator, day=self.created).delete()

        answers = question.answers.filter(is_spam=False)
        question.num_answers = answers.count() - 1
        question.save()
        question.clear_cached_contributors()

        super(Answer, self).delete(*args, **kwargs)

        update_answer_pages.delay(question)
Exemplo n.º 2
0
    def set_solution(self, answer, solver):
        """
        Sets the solution, and fires any needed events.

        Does not check permission of the user making the change.
        """
        # Avoid circular import
        from kitsune.questions.events import QuestionSolvedEvent

        self.solution = answer
        self.save()
        self.add_metadata(solver_id=str(solver.id))
        statsd.incr('questions.solution')
        QuestionSolvedEvent(answer).fire(exclude=self.creator)
        SolutionAction(user=answer.creator, day=answer.created).save()
Exemplo n.º 3
0
def _process_question_chunk(data):
    """Save karma data for a chunk of questions."""
    redis = redis_client(name='karma')
    q_qs = Question.objects.select_related('solution').defer('content')
    for question in q_qs.filter(pk__in=data):
        first = True
        a_qs = question.answers.order_by('created').select_related('creator')
        for answer in a_qs.values_list('creator', 'created'):
            AnswerAction(answer[0], answer[1]).save(async=False, redis=redis)
            if first:
                FirstAnswerAction(answer[0], answer[1]).save(async=False,
                                                             redis=redis)
                first = False
        soln = question.solution
        if soln:
            SolutionAction(soln.creator, soln.created).save(async=False,
                                                            redis=redis)
Exemplo n.º 4
0
    def test_creator_nums_redis(self, switch_is_active):
        """Test creator_num_* pulled from karma data."""
        try:
            KarmaManager()
            redis_client('karma').flushdb()
        except RedisError:
            raise SkipTest

        switch_is_active.return_value = True
        a = answer(save=True)

        AnswerAction(a.creator).save()
        AnswerAction(a.creator).save()
        SolutionAction(a.creator).save()

        eq_(a.creator_num_solutions, 1)
        eq_(a.creator_num_answers, 3)