def solution(request, question_id, answer_id): """Accept an answer as the solution to the question.""" question = get_object_or_404(Question, pk=question_id) answer = get_object_or_404(Answer, pk=answer_id) if question.is_locked: raise PermissionDenied if question.creator != request.user: return HttpResponseForbidden() question.solution = answer question.save() QuestionSolvedEvent(answer).fire(exclude=question.creator) return HttpResponseRedirect(answer.get_absolute_url())
def solve(request, question_id, answer_id): """Accept an answer as the solution to the question.""" question = get_object_or_404(Question, pk=question_id) # It is possible this was clicked from the email. if not request.user.is_authenticated(): watch_secret = request.GET.get('watch', None) try: watch = Watch.objects.get(secret=watch_secret, event_type='question reply', user=question.creator) # Create a new secret. distinguishable_letters = \ 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ' new_secret = ''.join(random.choice(distinguishable_letters) for x in xrange(10)) watch.update(secret=new_secret) request.user = question.creator except Watch.DoesNotExist: # This user is neither authenticated nor using the correct secret return HttpResponseForbidden() answer = get_object_or_404(Answer, pk=answer_id) if question.is_locked: raise PermissionDenied if (question.creator != request.user and not request.user.has_perm('questions.change_solution')): return HttpResponseForbidden() question.solution = answer question.save() statsd.incr('questions.solution') QuestionSolvedEvent(answer).fire(exclude=question.creator) SolutionAction(user=answer.creator, day=answer.created).save() messages.add_message(request, messages.SUCCESS, _('Thank you for choosing a solution!')) return HttpResponseRedirect(question.get_absolute_url())
def test_solution_notification_deleted(self, get_current): """Calling QuestionSolvedEvent.fire() should not query the questions_question table. This test attempts to simulate the replication lag presumed to cause bug 585029. """ get_current.return_value.domain = 'testserver' answer = Answer.objects.get(pk=1) question = Question.objects.get(pk=1) question.solution = answer question.save() a_user = User.objects.get(username='******') QuestionSolvedEvent.notify(a_user, question) event = QuestionSolvedEvent(answer) # Delete the question, pretend it hasn't been replicated yet Question.objects.get(pk=question.pk).delete() event.fire(exclude=question.creator) eq_(1, len(mail.outbox))