def watch_question(request, question_id): """Start watching a question for replies or solution.""" question = get_object_or_404(Question, pk=question_id) form = WatchQuestionForm(request.user, request.POST) # Process the form msg = None if form.is_valid(): user_or_email = (request.user if request.user.is_authenticated() else form.cleaned_data['email']) try: if form.cleaned_data['event_type'] == 'reply': QuestionReplyEvent.notify(user_or_email, question) else: QuestionSolvedEvent.notify(user_or_email, question) except ActivationRequestFailed: msg = _('Could not send a message to that email address.') # Respond to ajax request if request.is_ajax(): if form.is_valid(): msg = msg or (_('You will be notified of updates by email.') if request.user.is_authenticated() else _( 'You should receive an email shortly ' 'to confirm your subscription.')) return HttpResponse(json.dumps({'message': msg})) if request.POST.get('from_vote'): tmpl = 'questions/includes/question_vote_thanks.html' else: tmpl = 'questions/includes/email_subscribe.html' html = jingo.render_to_string(request, tmpl, { 'question': question, 'watch_form': form }) return HttpResponse(json.dumps({'html': html})) # Respond to normal request if form.is_valid() and not msg: return HttpResponseRedirect(question.get_absolute_url()) return answers(request, question.id, watch_form=form, message=msg)
def test_registered_watch_without_email(self): form = WatchQuestionForm(user(), data={'event_type': 'reply'}) assert form.is_valid()
def test_registered_watch_with_email(self): form = WatchQuestionForm(user(), data={'email': '*****@*****.**', 'event_type': 'reply'}) assert form.is_valid() assert not form.cleaned_data['email']
def test_anonymous_watch_without_email(self): form = WatchQuestionForm(AnonymousUser(), data={'event_type': 'reply'}) assert not form.is_valid() eq_('Please provide an email.', form.errors['email'][0])
def test_anonymous_watch_with_email(self): form = WatchQuestionForm(AnonymousUser(), data={'email': '*****@*****.**', 'event_type': 'reply'}) assert form.is_valid() eq_('*****@*****.**', form.cleaned_data['email'])
def test_registered_watch_without_email(self): form = WatchQuestionForm(user(), data={}) assert form.is_valid()
def _init_watch_form(request, event_type='solution'): initial = {'event_type': event_type} return WatchQuestionForm(request.user, initial=initial)