Esempio n. 1
0
def recovery(request):
    """
    Let user enter their email address and be sent a message with a link
    to manage their subscriptions.
    """

    if request.method == "POST":
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            try:
                # Try it - basket will return an error if the email is unknown
                basket.send_recovery_message(email)
            except basket.BasketException as e:
                # Was it that their email was not known?  Or it could be invalid,
                # but that doesn't really make a difference.
                if e.code in (basket.errors.BASKET_UNKNOWN_EMAIL,
                              basket.errors.BASKET_INVALID_EMAIL):
                    # Tell them, give them a link to go subscribe if they want
                    url = reverse("newsletter.subscribe")
                    form.errors["email"] = form.error_class([
                        ftl("newsletters-this-email-address-is-not",
                            url=url,
                            ftl_files=FTL_FILES)
                    ])
                else:
                    # Log the details
                    log.exception("Error sending recovery message")
                    # and tell the user that something went wrong
                    form.errors["__all__"] = form.error_class([general_error])
            else:
                messages.add_message(request, messages.INFO, recovery_text)
                # Redir as GET, signalling success
                return redirect(request.path + "?success")
    elif "success" in request.GET:
        # We were redirected after a successful submission.
        # A message will be displayed; don't display the form again.
        form = None
    else:
        form = EmailForm()

    # This view is shared between two different templates. For context see bug 1442129.
    if "/newsletter/opt-out-confirmation/" in request.get_full_path():
        template = "newsletter/opt-out-confirmation.html"
        ftl_files = ["newsletter/opt-out-confirmation"]
    else:
        template = "newsletter/recovery.html"
        ftl_files = FTL_FILES

    return l10n_utils.render(request,
                             template, {"form": form},
                             ftl_files=ftl_files)
Esempio n. 2
0
def recovery(request):
    """
    Let user enter their email address and be sent a message with a link
    to manage their subscriptions.
    """

    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                # Try it - basket will return an error if the email is unknown
                basket.send_recovery_message(email)
            except basket.BasketException as e:
                # Was it that their email was not known?  Or it could be invalid,
                # but that doesn't really make a difference.
                if e.code in (basket.errors.BASKET_UNKNOWN_EMAIL,
                              basket.errors.BASKET_INVALID_EMAIL):
                    # Tell them, give them a link to go subscribe if they want
                    url = reverse('newsletter.subscribe')
                    form.errors['email'] = \
                        form.error_class([unknown_address_text % url])
                else:
                    # Log the details
                    log.exception("Error sending recovery message")
                    # and tell the user that something went wrong
                    form.errors['__all__'] = form.error_class([general_error])
            else:
                messages.add_message(request, messages.INFO, recovery_text)
                # Redir as GET, signalling success
                return redirect(request.path + "?success")
    elif 'success' in request.GET:
        # We were redirected after a successful submission.
        # A message will be displayed; don't display the form again.
        form = None
    else:
        form = EmailForm()

    # This view is shared between two different templates. For context see bug 1442129.
    if '/newsletter/opt-out-confirmation/' in request.get_full_path():
        template = "newsletter/opt-out-confirmation.html"
    else:
        template = "newsletter/recovery.html"

    return l10n_utils.render(request, template, {'form': form})
Esempio n. 3
0
def recovery(request):
    """
    Let user enter their email address and be sent a message with a link
    to manage their subscriptions.
    """

    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                # Try it - basket will return an error if the email is unknown
                basket.send_recovery_message(email)
            except basket.BasketException as e:
                # Was it that their email was not known?  Or it could be invalid,
                # but that doesn't really make a difference.
                if e.code in (basket.errors.BASKET_UNKNOWN_EMAIL,
                              basket.errors.BASKET_INVALID_EMAIL):
                    # Tell them, give them a link to go subscribe if they want
                    url = reverse('newsletter.subscribe')
                    form.errors['email'] = \
                        form.error_class([unknown_address_text % url])
                else:
                    # Log the details
                    log.exception("Error sending recovery message")
                    # and tell the user that something went wrong
                    form.errors['__all__'] = form.error_class([general_error])
            else:
                messages.add_message(request, messages.INFO, recovery_text)
                # Redir as GET, signalling success
                return redirect(request.path + "?success")
    elif 'success' in request.GET:
        # We were redirected after a successful submission.
        # A message will be displayed; don't display the form again.
        form = None
    else:
        form = EmailForm()

    return l10n_utils.render(
        request,
        "newsletter/recovery.html",
        {
            'form': form,
        })
Esempio n. 4
0
def recovery(request):
    """
    Let user enter their email address and be sent a message with a link
    to manage their subscriptions.
    """

    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                # Try it - basket will return an error if the email is unknown
                basket.send_recovery_message(email)
            except basket.BasketException as e:
                # Was it that their email was not known?
                if e.status_code == 404:
                    # Tell them, give them a link to go subscribe if they want
                    url = reverse('newsletter.mozilla-and-you')
                    form.errors['email'] = \
                        form.error_class([unknown_address_text % url])
                else:
                    # Log the details
                    log.exception("Error sending recovery message")
                    # and tell the user that something went wrong
                    form.errors['__all__'] = form.error_class([general_error])
            else:
                messages.add_message(request, messages.INFO, recovery_text)
                # Redir as GET, signalling success
                return redirect(request.path + "?success")
    elif 'success' in request.GET:
        # We were redirected after a successful submission.
        # A message will be displayed; don't display the form again.
        form = None
    else:
        form = EmailForm()

    return l10n_utils.render(
        request,
        "newsletter/recovery.html",
        {
            'form': form,
        })
Esempio n. 5
0
 def test_send_recovery_email(self):
     """
     send_recovery_email() passes the expected args to request,
     and returns the result.
     """
     email = "*****@*****.**"
     with patch('basket.base.request', autospec=True) as mock_request:
         result = send_recovery_message(email)
     data = {'email': email}
     mock_request.assert_called_with('post', 'recover', data=data)
     self.assertEqual(mock_request.return_value, result)
Esempio n. 6
0
 def test_send_recovery_email(self):
     """
     send_recovery_email() passes the expected args to request,
     and returns the result.
     """
     email = "*****@*****.**"
     with patch('basket.base.request', autospec=True) as mock_request:
         result = send_recovery_message(email)
     data = {'email': email}
     mock_request.assert_called_with('post', 'recover', data=data)
     self.assertEqual(mock_request.return_value, result)