Beispiel #1
0
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.POST)

    # Process the form
    if form.is_valid():
        if request.user.is_authenticated():
            email = request.user.email
        else:
            email = form.cleaned_data['email']
        event_type = form.cleaned_data['event_type']
        create_watch(Question, question.id, email, event_type)

    # Respond to ajax request
    if request.is_ajax():
        if form.is_valid():
            msg = _('You will be notified of updates by email.')
            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():
        return HttpResponseRedirect(question.get_absolute_url())

    return answers(request, question.id, watch_form=form)
Beispiel #2
0
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)
Beispiel #3
0
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 message to that email address.')

    # Respond to ajax request
    if request.is_ajax():
        if form.is_valid():
            if not msg:
                msg = (_('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
    # TODO: show failure here if email fails to send.
    if form.is_valid():
        return HttpResponseRedirect(question.get_absolute_url())

    return answers(request, question.id, watch_form=form)
Beispiel #4
0
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)
            statsd.incr("questions.watches.new")
        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}))

    if msg:
        messages.add_message(request, messages.ERROR, msg)

    return HttpResponseRedirect(question.get_absolute_url())
Beispiel #5
0
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:
            QuestionSolvedEvent.notify(user_or_email, question)
            statsd.incr('questions.watches.new')
        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}))

        html = jingo.render_to_string(
            request,
            'questions/includes/question_vote_thanks.html',
            {'question': question, 'watch_form': form})

        return HttpResponse(json.dumps({'html': html}))

    if msg:
        messages.add_message(request, messages.ERROR, msg)

    return HttpResponseRedirect(question.get_absolute_url())
Beispiel #6
0
 def test_registered_watch_without_email(self):
     form = WatchQuestionForm(user(), data={'event_type': 'reply'})
     assert form.is_valid()
Beispiel #7
0
 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']
Beispiel #8
0
 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])
Beispiel #9
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'])
Beispiel #10
0
 def test_registered_watch_without_email(self):
     form = WatchQuestionForm(user(), data={})
     assert form.is_valid()
Beispiel #11
0
def _init_watch_form(request, event_type='solution'):
    initial = {'event_type': event_type}
    return WatchQuestionForm(request.user, initial=initial)