Example #1
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'])
Example #2
0
 def test_registered_watch_with_email(self):
     form = WatchQuestionForm(UserFactory(),
                              data={
                                  "email": "*****@*****.**",
                                  "event_type": "reply"
                              })
     assert form.is_valid()
     assert not form.cleaned_data["email"]
Example #3
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"])
Example #4
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']
Example #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, is_spam=False)
    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 = render_to_string(tmpl,
                                context={
                                    "question": question,
                                    "watch_form": form
                                },
                                request=request)
        return HttpResponse(json.dumps({"html": html}))

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

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