def __init__(self, newsletters, locale, data=None, *args, **kwargs): regions = product_details.get_regions(locale) regions = sorted(iter(regions.items()), key=itemgetter(1)) try: if isinstance(newsletters, str): newsletters = newsletters.split(",") newsletters = validate_newsletters(newsletters) except ValidationError: # replace with most common good newsletter # form validation will work with submitted data newsletters = ["mozilla-and-you"] lang = locale.lower() if "-" in lang: lang, country = lang.split("-", 1) else: country = "" regions.insert( 0, ("", ftl_lazy("newsletter-form-select-country-or-region", fallback="newsletter-form-select-country"))) lang_choices = get_lang_choices(newsletters) languages = [x[0] for x in lang_choices] if lang not in languages: # The lang from their locale is not one that our newsletters # are translated into. Initialize the language field to no # choice, to force the user to pick one of the languages that # we do support. lang = "" lang_choices.insert( 0, ("", ftl_lazy("newsletter-form-available-languages"))) super().__init__(data, *args, **kwargs) required_args = { "required": "required", "aria-required": "true", } country_widget = widgets.Select(attrs=required_args) country_label = ftl_lazy("newsletter-form-select-country-or-region", fallback="newsletter-form-select-country") self.fields["country"] = forms.ChoiceField(widget=country_widget, choices=regions, initial=country, required=False, label=country_label) lang_widget = widgets.Select(attrs=required_args) lang_label = ftl_lazy("newsletter-form-select-language", fallback="newsletter-form-available-languages") self.fields["lang"] = forms.TypedChoiceField(widget=lang_widget, choices=lang_choices, initial=lang, required=False, label=lang_label) self.fields["newsletters"].choices = [(n, self.choice_labels.get(n, n)) for n in newsletters] self.fields["newsletters"].initial = newsletters
def __init__(self, newsletters, locale, data=None, *args, **kwargs): regions = product_details.get_regions(locale) regions = sorted(iter(regions.items()), key=itemgetter(1)) try: newsletters = validate_newsletters(newsletters) except ValidationError: # replace with most common good newsletter # form validation will work with submitted data newsletters = 'mozilla-and-you' lang = locale.lower() if '-' in lang: lang, country = lang.split('-', 1) else: country = '' regions.insert( 0, ('', ftl_lazy('newsletter-form-select-country-or-region', fallback='newsletter-form-select-country'))) lang_choices = get_lang_choices(newsletters) languages = [x[0] for x in lang_choices] if lang not in languages: # The lang from their locale is not one that our newsletters # are translated into. Initialize the language field to no # choice, to force the user to pick one of the languages that # we do support. lang = '' lang_choices.insert( 0, ('', ftl_lazy('newsletter-form-available-languages'))) super(NewsletterFooterForm, self).__init__(data, *args, **kwargs) required_args = { 'required': 'required', 'aria-required': 'true', } country_widget = widgets.Select(attrs=required_args) self.fields['country'] = forms.ChoiceField(widget=country_widget, choices=regions, initial=country, required=False) lang_widget = widgets.Select(attrs=required_args) self.fields['lang'] = forms.TypedChoiceField(widget=lang_widget, choices=lang_choices, initial=lang, required=False) self.fields['newsletters'].initial = newsletters
from bedrock.newsletter import utils from lib.l10n_utils.fluent import ftl, ftl_lazy from .forms import ( CountrySelectForm, EmailForm, ManageSubscriptionsForm, NewsletterFooterForm, NewsletterForm, ) log = commonware.log.getLogger("b.newsletter") FTL_FILES = ["mozorg/newsletters"] general_error = ftl_lazy("newsletters-we-are-sorry-but-there", ftl_files=FTL_FILES) thank_you = ftl_lazy("newsletters-your-email-preferences", fallback="newsletters-thanks-for-updating-your", ftl_files=FTL_FILES) bad_token = ftl_lazy("newsletters-the-supplied-link-has-expired-long", ftl_files=FTL_FILES) recovery_text = ftl_lazy("newsletters-success-an-email-has-been-sent", ftl_files=FTL_FILES) invalid_email_address = ftl_lazy("newsletters-this-is-not-a-valid-email", ftl_files=FTL_FILES) NEWSLETTER_STRINGS = { "about-mozilla": { "description": ftl_lazy("newsletters-join-mozillians-all-around", ftl_files=FTL_FILES),
# 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 https://mozilla.org/MPL/2.0/. import re from datetime import datetime from random import randrange from django.forms import widgets from django.urls import reverse from django.utils.safestring import mark_safe from lib.l10n_utils.fluent import ftl, ftl_lazy FORMATS = (("H", ftl_lazy("newsletter-form-html")), ("T", ftl_lazy("newsletter-form-text"))) LANGS_TO_STRIP = ["en-US", "es"] PARENTHETIC_RE = re.compile(r" \([^)]+\)$") def strip_parenthetical(lang_name): """ Remove the parenthetical from the end of the language name string. """ return PARENTHETIC_RE.sub("", lang_name, 1) class PrivacyWidget(widgets.CheckboxInput): """Render a checkbox with privacy text. Lots of pages need this so it should be standardized""" def render(self, name, value, attrs=None, renderer=None):
# 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 re from datetime import datetime from random import randrange from django.urls import reverse from django.forms import widgets from django.utils.safestring import mark_safe from lib.l10n_utils.fluent import ftl, ftl_lazy FORMATS = (('H', ftl_lazy('newsletter-form-html')), ('T', ftl_lazy('newsletter-form-text'))) LANGS_TO_STRIP = ['en-US', 'es'] PARENTHETIC_RE = re.compile(r' \([^)]+\)$') def strip_parenthetical(lang_name): """ Remove the parenthetical from the end of the language name string. """ return PARENTHETIC_RE.sub('', lang_name, 1) class PrivacyWidget(widgets.CheckboxInput): """Render a checkbox with privacy text. Lots of pages need this so it should be standardized"""