<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import logging

import basket
import jingo
import jinja2

from lib.l10n_utils import get_locale
from lib.l10n_utils.dotlang import _lazy
from bedrock.newsletter.forms import NewsletterFooterForm


log = logging.getLogger(__name__)


@jingo.register.function
@jinja2.contextfunction
def email_newsletter_form(ctx, newsletters='mozilla-and-you', title=None,
                          include_country=True, include_language=True,
                          use_thankyou=True, footer=True, process_form=True):
    request = ctx['request']
    context = ctx.get_all()
    context.update(dict(
        id=newsletters,
        title=title,
        include_country=include_country,
        include_language=include_language,
        use_thankyou=use_thankyou,
        footer=footer,
    ))
    success = False
    form = NewsletterFooterForm(newsletters, get_locale(request),
                                request.POST or None)

    if process_form and request.method == 'POST':
        if form.is_valid():
            data = form.cleaned_data

            # If data['lang'] is set, pass it to the template.
            # If it's None, empty, or nonexistent, pass 'en'.
            context['lang'] = data.get('lang', 'en').strip() or 'en'

            kwargs = {'format': data['fmt']}
            # add optional data
            kwargs.update(dict((k, data[k]) for k in ['country',
                                                      'lang',
                                                      'source_url']
                               if data[k]))
            try:
                basket.subscribe(data['email'], form.newsletters,
                                 **kwargs)
            except basket.BasketException:
                log.exception("Error subscribing %s to newsletter %s" %
                              (data['email'], data['newsletter']))
                msg = _lazy("We are sorry, but there was a problem "
                            "with our system. Please try again later!")
                form.errors['__all__'] = form.error_class([msg])
            else:
                success = True

    request.newsletter_success = success
    context.update(dict(form=form, success=success))
    html = jingo.render_to_string(request, 'newsletter/includes/form.html', context)
    if not (success and not use_thankyou):
        return jinja2.Markup(html)<commit_msg>Fix error in newsletter helper error handling<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import logging

import basket
import jingo
import jinja2

from lib.l10n_utils import get_locale
from lib.l10n_utils.dotlang import _lazy
from bedrock.newsletter.forms import NewsletterFooterForm


log = logging.getLogger(__name__)


@jingo.register.function
@jinja2.contextfunction
def email_newsletter_form(ctx, newsletters='mozilla-and-you', title=None,
                          include_country=True, include_language=True,
                          use_thankyou=True, footer=True, process_form=True):
    request = ctx['request']
    context = ctx.get_all()
    context.update(dict(
        id=newsletters,
        title=title,
        include_country=include_country,
        include_language=include_language,
        use_thankyou=use_thankyou,
        footer=footer,
    ))
    success = False
    form = NewsletterFooterForm(newsletters, get_locale(request),
                                request.POST or None)

    if process_form and request.method == 'POST':
        if form.is_valid():
            data = form.cleaned_data

            # If data['lang'] is set, pass it to the template.
            # If it's None, empty, or nonexistent, pass 'en'.
            context['lang'] = data.get('lang', 'en').strip() or 'en'

            kwargs = {'format': data['fmt']}
            # add optional data
            kwargs.update(dict((k, data[k]) for k in ['country',
                                                      'lang',
                                                      'source_url']
                               if data[k]))
            try:
                basket.subscribe(data['email'], form.newsletters,
                                 **kwargs)
            except basket.BasketException:
                log.exception("Error subscribing %s to newsletter %s" %
                              (data['email'], newsletters))
                msg = _lazy("We are sorry, but there was a problem "
                            "with our system. Please try again later!")
                form.errors['__all__'] = form.error_class([msg])
            else:
                success = True

    request.newsletter_success = success
    context.update(dict(form=form, success=success))
    html = jingo.render_to_string(request, 'newsletter/includes/form.html', context)
    if not (success and not use_thankyou):
        return jinja2.Markup(html)
{'fraction_non_alphanumeric': 0.09584052137243626, 'fraction_numerical': 0.004216982940387195, 'mean_word_length': 1.9331084879145588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306041', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1352, 'n_words': 375}