Ejemplo n.º 1
0
def log_answer(answer):
    pin_this_thread()

    # Record karma actions
    AnswerAction(answer.creator, answer.created.date()).save()
    try:
        from kitsune.questions.models import Answer
        answers = Answer.uncached.filter(question=answer.question_id)
        if answer == answers.order_by('created')[0]:
            FirstAnswerAction(answer.creator, answer.created.date()).save()
    except IndexError:
        # If we hit an IndexError, we assume this is the first answer.
        FirstAnswerAction(answer.creator, answer.created.date()).save()

    unpin_this_thread()
Ejemplo n.º 2
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)
Ejemplo 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)