def __init__(self, locale, *args, **kwargs): regions = product_details.get_regions(locale) regions = sorted(regions.iteritems(), key=lambda x: x[1]) lang = country = locale.lower() if '-' in lang: lang, country = lang.split('-', 1) lang_choices = get_lang_choices() 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, (lang, lang)) super(NewsletterFooterForm, self).__init__(*args, **kwargs) self.fields['country'] = forms.ChoiceField(choices=regions, initial=country, required=False) # TypedChoiceField knows that '' is an empty choice, and with # required=True, will not accept '' as valid input. select_widget = widgets.Select(attrs={'required': 'required'}) self.fields['lang'] = forms.TypedChoiceField(widget=select_widget, choices=lang_choices, initial=lang, required=True, empty_value='')
def __init__(self, locale, *args, **kwargs): regions = product_details.get_regions(locale) regions = sorted(regions.iteritems(), key=lambda x: x[1]) lang_choices = get_lang_choices() languages = [x[0] for x in lang_choices] lang = country = locale.lower() if '-' in lang: lang, country = lang.split('-', 1) lang = lang if lang in languages else 'en' self.newsletters = kwargs.pop('newsletters', []) # Get initial - work with a copy so we're not modifying the # data that was passed to us initial = kwargs.get('initial', {}).copy() if not initial.get('country', None): initial['country'] = country if not initial.get('lang', None): initial['lang'] = lang else: lang = initial['lang'] # Sometimes people are in ET with a language that is spelled a # little differently from our list. E.g. we have 'es' on our # list, but in ET their language is 'es-ES'. Try to find a match # for their current lang in our list and use that. If we can't # find one, then fall back to guessing from their locale, # ignoring what they had in ET. (This is just for the initial # value on the form; they can always change to another valid # language before submitting.) if lang not in languages: for valid_lang, _unused in lang_choices: # if the first two chars match, close enough if lang.lower()[:2] == valid_lang.lower()[:2]: lang = valid_lang break else: # No luck - guess from the locale lang = locale.lower() if '-' in lang: lang, _unused = lang.split('-', 1) initial['lang'] = lang kwargs['initial'] = initial super(ManageSubscriptionsForm, self).__init__(*args, **kwargs) self.fields['country'].choices = regions self.fields['lang'].choices = lang_choices self.already_subscribed = initial.get('newsletters', [])
def __init__(self, locale, *args, **kwargs): regions = product_details.get_regions(locale) regions = sorted(regions.iteritems(), key=lambda x: x[1]) lang = locale.lower() if '-' in lang: lang, country = lang.split('-', 1) else: country = '' regions.insert(0, ('', _lazy('Select country'))) lang_choices = get_lang_choices() 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, ('', _lazy('Available Languages'))) super(NewsletterFooterForm, self).__init__(*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)