def newsletter_subscribe(request): if request.method == 'POST': newsletters = request.POST.get('newsletters', None) form = NewsletterFooterForm(newsletters, l10n_utils.get_locale(request), request.POST) errors = [] 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', 'first_name', 'last_name', ] if data[k])) try: basket.subscribe(data['email'], data['newsletters'], **kwargs) except basket.BasketException as e: if e.code == basket.errors.BASKET_INVALID_EMAIL: errors.append(unicode(invalid_email_address)) else: log.exception("Error subscribing %s to newsletter %s" % (data['email'], data['newsletters'])) errors.append(unicode(general_error)) else: if 'email' in form.errors: errors.append(_('Please enter a valid email address')) if 'privacy' in form.errors: errors.append(_('You must agree to the privacy notice')) for fieldname in ('fmt', 'lang', 'country'): if fieldname in form.errors: errors.extend(form.errors[fieldname]) # form error messages may contain unsanitized user input errors = map(escape, errors) if request.is_ajax(): # return JSON if errors: resp = { 'success': False, 'errors': errors, } else: resp = {'success': True} return HttpResponseJSON(resp) else: ctx = {'newsletter_form': form} if not errors: ctx['success'] = True return l10n_utils.render(request, 'newsletter/mozilla-and-you.html', ctx) return l10n_utils.render(request, 'newsletter/mozilla-and-you.html')
def vpn_invite_waitlist(request): errors = [] locale = l10n_utils.get_locale(request) form = VPNWaitlistForm(locale, request.POST) if form.is_valid(): data = form.cleaned_data kwargs = { 'email': data['email'], 'fpn_platform': ','.join(data['platforms']), 'fpn_country': data['country'], 'lang': data['lang'], 'newsletters': 'guardian-vpn-waitlist', } if settings.BASKET_API_KEY: kwargs['api_key'] = settings.BASKET_API_KEY # NOTE this is not a typo; Referrer is misspelled in the HTTP spec # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36 if not kwargs.get('source_url') and request.META.get('HTTP_REFERER'): kwargs['source_url'] = request.META['HTTP_REFERER'] try: basket.subscribe(**kwargs) except basket.BasketException as e: if e.code == basket.errors.BASKET_INVALID_EMAIL: errors.append(str(invalid_email_address)) else: errors.append(str(general_error)) else: if 'email' in form.errors: errors.append(ftl('newsletter-form-please-enter-a-valid')) if 'privacy' in form.errors: errors.append(ftl('newsletter-form-you-must-agree-to')) for fieldname in ('fmt', 'lang', 'country'): if fieldname in form.errors: errors.extend(form.errors[fieldname]) if errors: errors = [escape(e) for e in errors] resp = { 'success': False, 'errors': errors, } else: resp = {'success': True} return JsonResponse(resp)
def vpn_invite_waitlist(request): errors = [] locale = l10n_utils.get_locale(request) form = VPNWaitlistForm(locale, request.POST) if form.is_valid(): data = form.cleaned_data kwargs = { "email": data["email"], "fpn_platform": ",".join(data["platforms"]), "fpn_country": data["country"], "lang": data["lang"], "newsletters": "guardian-vpn-waitlist", } if settings.BASKET_API_KEY: kwargs["api_key"] = settings.BASKET_API_KEY # NOTE this is not a typo; Referrer is misspelled in the HTTP spec # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36 if not kwargs.get("source_url") and request.headers.get("Referer"): kwargs["source_url"] = request.headers["Referer"] try: basket.subscribe(**kwargs) except basket.BasketException as e: if e.code == basket.errors.BASKET_INVALID_EMAIL: errors.append(str(invalid_email_address)) else: errors.append(str(general_error)) else: if "email" in form.errors: errors.append(ftl("newsletter-form-please-enter-a-valid")) if "privacy" in form.errors: errors.append(ftl("newsletter-form-you-must-agree-to")) for fieldname in ("fmt", "lang", "country"): if fieldname in form.errors: errors.extend(form.errors[fieldname]) if errors: errors = [escape(e) for e in errors] resp = { "success": False, "errors": errors, } else: resp = {"success": True} return JsonResponse(resp)
def newsletter_subscribe(request): if request.method == 'POST': newsletters = request.POST.get('newsletters', None) form = NewsletterFooterForm(newsletters, l10n_utils.get_locale(request), request.POST) errors = [] 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', 'first_name', 'last_name', ] if data[k])) # NOTE this is not a typo; Referrer is misspelled in the HTTP spec # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36 if not kwargs.get('source_url') and request.META.get( 'HTTP_REFERER'): kwargs['source_url'] = request.META['HTTP_REFERER'] try: basket.subscribe(data['email'], data['newsletters'], **kwargs) except basket.BasketException as e: if e.code == basket.errors.BASKET_INVALID_EMAIL: errors.append(str(invalid_email_address)) else: log.exception("Error subscribing %s to newsletter %s" % (data['email'], data['newsletters'])) errors.append(str(general_error)) else: if 'email' in form.errors: errors.append(ftl('newsletter-form-please-enter-a-valid')) if 'privacy' in form.errors: errors.append(ftl('newsletter-form-you-must-agree-to')) for fieldname in ('fmt', 'lang', 'country'): if fieldname in form.errors: errors.extend(form.errors[fieldname]) # form error messages may contain unsanitized user input errors = list(map(escape, errors)) if request.is_ajax(): # return JSON if errors: resp = { 'success': False, 'errors': errors, } else: resp = {'success': True} return JsonResponse(resp) else: ctx = {'newsletter_form': form} if not errors: ctx['success'] = True return l10n_utils.render(request, 'newsletter/index.html', ctx) return l10n_utils.render(request, 'newsletter/index.html')
def newsletter_subscribe(request): if request.method == "POST": newsletters = request.POST.getlist("newsletters") form = NewsletterFooterForm(newsletters, l10n_utils.get_locale(request), request.POST) errors = [] if form.is_valid(): data = form.cleaned_data kwargs = {"format": data["fmt"]} # add optional data kwargs.update({ k: data[k] for k in [ "country", "lang", "source_url", "first_name", "last_name", ] if data[k] }) # NOTE this is not a typo; Referrer is misspelled in the HTTP spec # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36 if not kwargs.get("source_url") and request.headers.get("Referer"): kwargs["source_url"] = request.headers["Referer"] # Convert data["newsletters"] to a comma separated string. newsletters = data["newsletters"] if isinstance(newsletters, list): newsletters = ",".join(newsletters) try: basket.subscribe(data["email"], newsletters, **kwargs) except basket.BasketException as e: if e.code == basket.errors.BASKET_INVALID_EMAIL: errors.append(str(invalid_email_address)) else: log.exception( f"Error subscribing {data['email']} to newsletter(s) {newsletters}" ) errors.append(str(general_error)) else: if "email" in form.errors: errors.append(ftl("newsletter-form-please-enter-a-valid")) if "privacy" in form.errors: errors.append(ftl("newsletter-form-you-must-agree-to")) for fieldname in ("newsletters", "fmt", "lang", "country"): if fieldname in form.errors: errors.extend(form.errors[fieldname]) # form error messages may contain unsanitized user input errors = [escape(e) for e in errors] if request.headers.get("x-requested-with") == "XMLHttpRequest": # return JSON if errors: resp = { "success": False, "errors": errors, } else: resp = {"success": True} return JsonResponse(resp) else: ctx = {"newsletter_form": form} if not errors: ctx["success"] = True return l10n_utils.render(request, "newsletter/index.html", ctx, ftl_files=FTL_FILES) return l10n_utils.render(request, "newsletter/index.html", ftl_files=FTL_FILES)