Пример #1
0
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 as e:
                if e.code == basket.errors.BASKET_INVALID_EMAIL:
                    form.errors['email'] = form.error_class([invalid_email_address])
                else:
                    log.exception("Error subscribing %s to newsletter %s" %
                                  (data['email'], form.newsletters))
                    form.errors['__all__'] = form.error_class([general_error])
            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)
Пример #2
0
    def process_request(self, request):
        success = False
        form = NewsletterFooterForm(request.locale, request.POST or None)

        is_footer_form = (request.method == 'POST' and
                          'newsletter-footer' in request.POST)
        if is_footer_form:
            if form.is_valid():
                data = form.cleaned_data
                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'], data['newsletter'],
                                     **kwargs)
                    success = True
                except basket.BasketException:
                    msg = _lazy("We are sorry, but there was a problem "
                                "with our system. Please try again later!")
                    form.errors['__all__'] = form.error_class([msg])

        request.newsletter_form = form
        request.newsletter_success = success
Пример #3
0
def contribute(request, template, return_to_form):
    newsletter_id = 'about-mozilla'
    has_contribute_form = (request.method == 'POST' and
                           'contribute-form' in request.POST)

    has_newsletter_form = (request.method == 'POST' and
                           'newsletter-form' in request.POST)

    locale = getattr(request, 'locale', 'en-US')

    contribute_success = False
    newsletter_success = False

    # This is ugly, but we need to handle two forms. I would love if
    # these forms could post to separate pages and get redirected
    # back, but we're forced to keep the error/success workflow on the
    # same page. Please change this.
    if has_contribute_form:
        form = ContributeForm(request.POST)
        contribute_success = email_contribute.handle_form(request, form)
        if contribute_success:
            # If form was submitted successfully, return a new, empty
            # one.
            form = ContributeForm()
    else:
        form = ContributeForm()

    if has_newsletter_form:
        newsletter_form = NewsletterFooterForm(newsletter_id, locale,
                                               request.POST,
                                               prefix='newsletter')
        if newsletter_form.is_valid():
            data = newsletter_form.cleaned_data

            try:
                basket.subscribe(data['email'],
                                 newsletter_id,
                                 format=data['fmt'],
                                 country=data['country'])
                newsletter_success = True
            except basket.BasketException:
                msg = newsletter_form.error_class(
                    [_('We apologize, but an error occurred in our system. '
                       'Please try again later.')]
                )
                newsletter_form.errors['__all__'] = msg
    else:
        newsletter_form = NewsletterFooterForm(newsletter_id, locale, prefix='newsletter')

    return l10n_utils.render(request,
                             template,
                             {'form': form,
                              'contribute_success': contribute_success,
                              'newsletter_form': newsletter_form,
                              'newsletter_success': newsletter_success,
                              'return_to_form': return_to_form,
                              'hide_form': hide_contrib_form(request.locale),
                              'has_moz15': locale in settings.LOCALES_WITH_MOZ15})
Пример #4
0
def contribute(request, template, return_to_form):
    has_contribute_form = (request.method == 'POST'
                           and 'contribute-form' in request.POST)

    has_newsletter_form = (request.method == 'POST'
                           and 'newsletter-form' in request.POST)

    locale = getattr(request, 'locale', 'en-US')

    contribute_success = False
    newsletter_success = False

    # This is ugly, but we need to handle two forms. I would love if
    # these forms could post to separate pages and get redirected
    # back, but we're forced to keep the error/success workflow on the
    # same page. Please change this.
    if has_contribute_form:
        form = ContributeForm(request.POST)
        contribute_success = email_contribute.handle_form(request, form)
        if contribute_success:
            # If form was submitted successfully, return a new, empty
            # one.
            form = ContributeForm()
    else:
        form = ContributeForm()

    if has_newsletter_form:
        newsletter_form = NewsletterFooterForm(locale,
                                               request.POST,
                                               prefix='newsletter')
        if newsletter_form.is_valid():
            data = newsletter_form.cleaned_data

            try:
                basket.subscribe(data['email'],
                                 'about-mozilla',
                                 format=data['fmt'],
                                 country=data['country'])
                newsletter_success = True
            except basket.BasketException:
                msg = newsletter_form.error_class([
                    _('We apologize, but an error occurred in our system. '
                      'Please try again later.')
                ])
                newsletter_form.errors['__all__'] = msg
    else:
        newsletter_form = NewsletterFooterForm(locale, prefix='newsletter')

    return l10n_utils.render(
        request, template, {
            'form': form,
            'contribute_success': contribute_success,
            'newsletter_form': newsletter_form,
            'newsletter_success': newsletter_success,
            'return_to_form': return_to_form,
            'hide_form': hide_contrib_form(request.locale),
            'has_moz15': locale in settings.LOCALES_WITH_MOZ15
        })
Пример #5
0
    def process_request(self, request):
        success = False
        form = NewsletterFooterForm(request.locale, request.POST or None)

        is_footer_form = (request.method == 'POST'
                          and 'newsletter-footer' in request.POST)
        if is_footer_form:
            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'.
                request.newsletter_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'], data['newsletter'],
                                     **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_form = form
        request.newsletter_success = success
Пример #6
0
    def process_request(self, request):
        success = False
        form = NewsletterFooterForm(request.locale, request.POST or None)

        is_footer_form = (request.method == 'POST' and
                          'newsletter-footer' in request.POST)
        if is_footer_form:
            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'.
                request.newsletter_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'], data['newsletter'],
                                     **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_form = form
        request.newsletter_success = success
Пример #7
0
def contribute(request, template, return_to_form):
    newsletter_id = "about-mozilla"
    has_contribute_form = request.method == "POST" and "contribute-form" in request.POST

    has_newsletter_form = request.method == "POST" and "newsletter-form" in request.POST

    locale = getattr(request, "locale", "en-US")

    contribute_success = False
    newsletter_success = False

    # This is ugly, but we need to handle two forms. I would love if
    # these forms could post to separate pages and get redirected
    # back, but we're forced to keep the error/success workflow on the
    # same page. Please change this.
    if has_contribute_form:
        form = ContributeForm(request.POST)

        if form.is_valid():
            data = form.cleaned_data.copy()

            honeypot = data.pop("office_fax")

            if not honeypot:
                contribute_success = email_contribute.handle_form(request, form)
                if contribute_success:
                    # If form was submitted successfully, return a new, empty
                    # one.
                    form = ContributeForm()
            else:
                # send back a clean form if honeypot was filled in
                form = ContributeForm()
    else:
        form = ContributeForm()

    if has_newsletter_form:
        newsletter_form = NewsletterFooterForm(newsletter_id, locale, request.POST, prefix="newsletter")
        if newsletter_form.is_valid():
            data = newsletter_form.cleaned_data

            try:
                basket.subscribe(data["email"], newsletter_id, format=data["fmt"], country=data["country"])
                newsletter_success = True
            except basket.BasketException:
                msg = newsletter_form.error_class(
                    [_("We apologize, but an error occurred in our system. " "Please try again later.")]
                )
                newsletter_form.errors["__all__"] = msg
    else:
        newsletter_form = NewsletterFooterForm(newsletter_id, locale, prefix="newsletter")

    return l10n_utils.render(
        request,
        template,
        {
            "form": form,
            "contribute_success": contribute_success,
            "newsletter_form": newsletter_form,
            "newsletter_success": newsletter_success,
            "return_to_form": return_to_form,
            "hide_form": hide_contrib_form(request.locale),
        },
    )