def test_edit_attachments_auto_device_name(self):
volume = self.cinder_volumes.first()
servers = [s for s in self.servers.list()
if s.tenant_id == self.request.user.tenant_id]
volume.attachments = [{'id': volume.id,
'volume_id': volume.id,
'volume_name': volume.name,
'instance': servers[0],
'device': '',
'server_id': servers[0].id}]
cinder.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
api.nova.server_list(IsA(http.HttpRequest)).AndReturn([servers, False])
self.mox.ReplayAll()
url = reverse('horizon:project:volumes:volumes:attach',
args=[volume.id])
res = self.client.get(url)
form = res.context['form']
self.assertIsInstance(form.fields['device'].widget,
widgets.TextInput)
self.assertFalse(form.fields['device'].required)
python类TextInput()的实例源码
def __init__(self, attrs=None):
color_widget = widgets.TextInput(attrs={
'type': 'color',
'class': 'colorfield-preview input-small',
})
input_widget = widgets.TextInput(attrs={
'max_length': 7,
'placeholder': 'hex color',
'class': 'colorfield-hex input-small',
'pattern': '#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})',
})
opacity_widget = widgets.TextInput(attrs={
'type': 'number',
'placeholder': 'opacity',
'class': 'colorfield-opacity input-mini',
'step': '0.05',
'min': 0,
'max': 1,
})
_widgets = (color_widget, input_widget, opacity_widget)
super().__init__(_widgets, attrs)
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self):
names = [factory.make_string(), factory.make_string()]
initials = []
labels = [factory.make_string(), factory.make_string()]
values = [factory.make_string(), factory.make_string()]
widget = DictCharWidget(
[widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
names, initials, labels, skip_check=True)
name = factory.make_string()
html_widget = fromstring(
'<root>' + widget.render(name, values) + '</root>')
widget_names = XPath('fieldset/input/@name')(html_widget)
widget_labels = XPath('fieldset/label/text()')(html_widget)
widget_values = XPath('fieldset/input/@value')(html_widget)
expected_names = [
"%s_%s" % (name, widget_name) for widget_name in names]
self.assertEqual(
[expected_names, labels, values],
[widget_names, widget_labels, widget_values])
def test_DictCharWidget_value_from_datadict_values_from_data(self):
# 'value_from_datadict' extracts the values corresponding to the
# field as a dictionary.
names = [factory.make_string(), factory.make_string()]
initials = []
labels = [factory.make_string(), factory.make_string()]
name = factory.make_string()
field_1_value = factory.make_string()
field_2_value = factory.make_string()
# Create a query string with the field2 before the field1 and another
# (unknown) value.
data = QueryDict(
'%s_%s=%s&%s_%s=%s&%s=%s' % (
name, names[1], field_2_value,
name, names[0], field_1_value,
factory.make_string(), factory.make_string())
)
widget = DictCharWidget(
[widgets.TextInput, widgets.TextInput], names, initials, labels)
self.assertEqual(
{names[0]: field_1_value, names[1]: field_2_value},
widget.value_from_datadict(data, None, name))
def test_DictCharWidget_renders_with_empty_string_as_input_data(self):
names = [factory.make_string(), factory.make_string()]
initials = []
labels = [factory.make_string(), factory.make_string()]
widget = DictCharWidget(
[widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
names, initials, labels, skip_check=True)
name = factory.make_string()
html_widget = fromstring(
'<root>' + widget.render(name, '') + '</root>')
widget_names = XPath('fieldset/input/@name')(html_widget)
widget_labels = XPath('fieldset/label/text()')(html_widget)
expected_names = [
"%s_%s" % (name, widget_name) for widget_name in names]
self.assertEqual(
[expected_names, labels],
[widget_names, widget_labels])
def test_edit_attachments(self):
volume = self.cinder_volumes.first()
servers = [s for s in self.servers.list()
if s.tenant_id == self.request.user.tenant_id]
volume.attachments = [{'id': volume.id,
'volume_id': volume.id,
'volume_name': volume.name,
'instance': servers[0],
'device': '/dev/vdb',
'server_id': servers[0].id}]
cinder.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
api.nova.server_list(IsA(http.HttpRequest)).AndReturn([servers, False])
self.mox.ReplayAll()
url = reverse('horizon:project:volumes:volumes:attach',
args=[volume.id])
res = self.client.get(url)
msg = 'Volume %s on instance %s' % (volume.name, servers[0].name)
self.assertContains(res, msg)
# Asserting length of 2 accounts for the one instance option,
# and the one 'Choose Instance' option.
form = res.context['form']
self.assertEqual(len(form.fields['instance']._choices),
1)
self.assertEqual(res.status_code, 200)
self.assertIsInstance(form.fields['device'].widget,
widgets.TextInput)
self.assertFalse(form.fields['device'].required)
def test_launch_form_instance_device_name_showed(self):
self._test_launch_form_instance_show_device_name(
u'vda', widgets.TextInput, {
'name': 'device_name', 'value': 'vda',
'attrs': {'id': 'id_device_name'}}
)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def __init__(self, *args, **kwargs):
super(PackageForm, self).__init__(*args, **kwargs)
self.fields['category'].help_text = package_help_text()
self.fields['repo_url'].widget = TextInput(attrs={
'placeholder': 'ex: https://github.com/steemit/steem'
})
self.fields['description'].widget = Textarea(attrs={
"placeholder": "Write few sentences about this projects. What problem does it solve? Who is it for?"
})
self.fields['contact_url'].widget = TextInput(attrs={
"placeholder": "Link to channel on steemit.chat, discord, slack, etc"
})
def form_class_from_model(self):
if self.model_name in self.model_form_fields:
Form = modelform_factory(self.model, fields=self.model_form_fields[self.model_name], widgets={'user': widgets.TextInput})
else:
Form = modelform_factory(self.model, exclude=self.excluded_fields, widgets={'user': widgets.TextInput})
return Form
def __init__(self, attrs=None):
color_widget = widgets.TextInput(attrs={
'type': 'color',
'class': 'colorfield-preview input-small',
})
input_widget = widgets.TextInput(attrs={
'max_length': 7,
'placeholder': 'hex color',
'class': 'colorfield-hex input-small',
'pattern': '#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})',
})
_widgets = (color_widget, input_widget)
super().__init__(_widgets, attrs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def __init__(self, send_label, fieldsetup_id, email_attrs, code_attrs):
self.send_label = send_label
self.fieldsetup_id = fieldsetup_id
widgets = (EmailInput(attrs=email_attrs), TextInput(attrs=code_attrs))
super(VerifiedEmailWidget, self).__init__(widgets)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def __init__(self, attrs=None):
_widgets = (
widgets.TextInput(attrs=attrs),
widgets.Select(attrs=attrs, choices=Event.DURATION_MULTIPLIER),
)
super(UnitsAndDurationWidget, self).__init__(_widgets, attrs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "username", "class": "form-control"})
self.fields['password'].widget = widgets.PasswordInput(
attrs={'placeholder': "password", "class": "form-control"})
def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs)
self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "username", "class": "form-control"})
self.fields['email'].widget = widgets.EmailInput(attrs={'placeholder': "email", "class": "form-control"})
self.fields['password1'].widget = widgets.PasswordInput(
attrs={'placeholder': "password", "class": "form-control"})
self.fields['password2'].widget = widgets.PasswordInput(
attrs={'placeholder': "repeat password", "class": "form-control"})
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)
def as_text(self, attrs=None, **kwargs):
"""
Returns a string of HTML for representing this as an <input type="text">.
"""
return self.as_widget(TextInput(), attrs, **kwargs)