def __init__(self, *args, **kwargs):
queryset = kwargs.pop('queryset', None)
self.many = kwargs.pop('many', self.many)
if self.many:
self.widget = self.many_widget
self.form_field_class = self.many_form_field_class
kwargs['read_only'] = kwargs.pop('read_only', self.read_only)
super(RelatedField, self).__init__(*args, **kwargs)
if not self.required:
# Accessed in ModelChoiceIterator django/forms/models.py:1034
# If set adds empty choice.
self.empty_label = BLANK_CHOICE_DASH[0][1]
self.queryset = queryset
python类BLANK_CHOICE_DASH的实例源码
def test_new_enterprise_customer(self):
"""
Test that a new blank form can be created and is not valid.
"""
self.catalog_api.get_all_catalogs.return_value = [
{
"id": self.catalog_id,
"name": "My Catalog"
},
{
"id": 1,
"name": "Other catalog!"
}
]
form = EnterpriseCustomerAdminForm()
assert isinstance(form.fields['catalog'], forms.ChoiceField)
assert form.fields['catalog'].choices == BLANK_CHOICE_DASH + [
(self.catalog_id, 'My Catalog'),
(1, 'Other catalog!'),
]
assert not form.is_valid()
def get_catalog_options(self):
"""
Retrieve a list of catalog ID and name pairs.
Once retrieved, these name pairs can be used directly as a value
for the `choices` argument to a ChoiceField.
"""
# TODO: We will remove the discovery service catalog implementation
# once we have fully migrated customer's to EnterpriseCustomerCatalogs.
# For now, this code will prevent an admin from creating a new
# EnterpriseCustomer with a discovery service catalog. They will have to first
# save the EnterpriseCustomer admin form and then edit the EnterpriseCustomer
# to add a discovery service catalog.
if hasattr(self.instance, 'site'):
catalog_api = CourseCatalogApiClient(self.user, self.instance.site)
else:
catalog_api = CourseCatalogApiClient(self.user)
catalogs = catalog_api.get_all_catalogs()
# order catalogs by name.
catalogs = sorted(catalogs, key=lambda catalog: catalog.get('name', '').lower())
return BLANK_CHOICE_DASH + [
(catalog['id'], catalog['name'],)
for catalog in catalogs
]
def __init__(self, *args, **kwargs):
super(UserPreferencesForm, self).__init__(*args, **kwargs)
if not FAVORITE_CHARACTERS:
for i in range(1, 4):
self.fields.pop('favorite_character{}'.format(i))
else:
for i in range(1, 4):
self.fields['favorite_character{}'.format(i)] = forms.ChoiceField(
required=False,
choices=BLANK_CHOICE_DASH + [(name, localized) for (name, localized, image) in FAVORITE_CHARACTERS],
label=(_(FAVORITE_CHARACTER_NAME) if FAVORITE_CHARACTER_NAME
else _('{nth} Favorite Character')).format(nth=_(ordinalNumber(i))))
self.fields['birthdate'] = date_input(self.fields['birthdate'])
if USER_COLORS:
self.fields['color'].choices = BLANK_CHOICE_DASH + [(name, localized_name) for (name, localized_name, css_color, hex_color) in USER_COLORS]
if self.instance:
self.fields['color'].initial = self.instance.color
else:
self.fields.pop('color')
self.fields['language'].choices = [l for l in self.fields['language'].choices if l[0]]
self.old_location = self.instance.location if self.instance else None
if 'activity' not in RAW_CONTEXT['all_enabled']:
del(self.fields['view_activities_language_only'])
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def __init__(self, *args, **kwargs):
super(SeasonAdminForm, self).__init__(*args, **kwargs)
punctuation_choices = get_punctuation_label_dict()
self.fields['punctuation'] = forms.ChoiceField(choices=BLANK_CHOICE_DASH + list(punctuation_choices),
initial=None)
def __init__(self, task, *args, **kwargs):
super(ChoiceAnswerForm, self).__init__(*args, **kwargs)
if task:
qs = task.choices
widget = forms.RadioSelect
empty_label = None
count = task.no_of_choices
if count > settings.TASK_CHOICE_SELECT_CUTOFF:
widget = forms.Select
empty_label = BLANK_CHOICE_DASH[0][1]
self.fields['answer'] = forms.ModelChoiceField(queryset=qs,
widget=widget,
empty_label=empty_label)
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def __init__(self, *args, **kwargs):
super(FilterEvents, self).__init__(*args, **kwargs)
self.fields['i_kind'].choices = BLANK_CHOICE_DASH + self.fields['i_kind'].choices
self.fields['i_kind'].initial = None
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def __init__(self, *args, **kwargs):
super(PageBlockQueryForm, self).__init__(*args, **kwargs)
block_choices = BLANK_CHOICE_DASH + [
(b, b) for b in
PageBlock.objects.values_list('block', flat=True).distinct()
]
self.fields['block'].choices = block_choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def test_interface_displays_selected_option(self):
self.catalog_api.get_all_catalogs.return_value = [
{
"id": self.catalog_id,
"name": "My Catalog"
},
{
"id": 1,
"name": "Other catalog!"
}
]
customer = EnterpriseCustomerFactory(
catalog=99,
)
form = EnterpriseCustomerAdminForm(
{
'catalog': '',
'enforce_data_sharing_consent': customer.enforce_data_sharing_consent,
'site': customer.site.id,
'name': customer.name,
'active': customer.active
},
instance=customer,
)
assert isinstance(form.fields['catalog'], forms.ChoiceField)
assert form.fields['catalog'].choices == BLANK_CHOICE_DASH + [
(self.catalog_id, 'My Catalog'),
(1, 'Other catalog!'),
]
def test_with_mocked_get_edx_data(self):
self.catalog_api.get_all_catalogs.return_value = [
{
"id": self.catalog_id,
"name": "My Catalog"
},
{
"id": 1,
"name": "Other catalog!"
}
]
customer = EnterpriseCustomerFactory(
catalog=99,
)
form = EnterpriseCustomerAdminForm(
{
'catalog': '',
'enforce_data_sharing_consent': customer.enforce_data_sharing_consent,
'site': customer.site.id,
'name': customer.name,
'active': customer.active
},
instance=customer,
)
assert isinstance(form.fields['catalog'], forms.ChoiceField)
assert form.fields['catalog'].choices == BLANK_CHOICE_DASH + [
(self.catalog_id, 'My Catalog'),
(1, 'Other catalog!'),
]
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_option_label(self, value, choices=()):
option_label = BLANK_CHOICE_DASH[0][1]
for v, label in chain(self.choices, choices):
if str(v) == value:
option_label = label
break
if option_label == BLANK_CHOICE_DASH[0][1]:
option_label = _('All')
return option_label
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
tuple (name, description).
"""
choices = [] + default_choices
for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices