def log_answer(answer): pin_this_thread() creator = answer.creator created = answer.created question = answer.question users = [ a.creator for a in question.answers.select_related('creator').exclude( creator=creator) ] if question.creator != creator: users += [question.creator] users = set(users) # Remove duplicates. if users: action = Action.objects.create( creator=creator, created=created, url=answer.get_absolute_url(), content_object=answer, formatter_cls='questions.formatters.AnswerFormatter') action.users.add(*users) transaction.commit_unless_managed() # Record karma actions AnswerAction(answer.creator, answer.created.date()).save() try: from 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()
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)