Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Field.classes['file'] = File
class Textarea(Text):
"""Field representing ``<textarea>``"""
Field.classes['textarea'] = Textarea
class Hidden(Text, forms.Hidden):
"""Field representing ``<input type="hidden">``"""
Field.classes['hidden'] = Hidden
class Submit(Field, forms.Submit):
"""Field representing ``<input type="submit">`` and ``<button>``"""
settable = False
def value__get(self):
return None
value = property(value__get)
def value_if_submitted(self):
return self._value
Field.classes['submit'] = Submit
Field.classes['button'] = Submit
</textarea>
def simpleControlFactory(wtcontrol, form, elemindex, browser):
if isinstance(wtcontrol, webtest.forms.Radio):
elems = [e for e in elemindex
if e.attrs.get('name') == wtcontrol.name]
return RadioListControl(wtcontrol, form, elems, browser)
elem = elemindex[wtcontrol.pos]
if isinstance(wtcontrol, (webtest.forms.Select,
webtest.forms.MultipleSelect)):
return ListControl(wtcontrol, form, elem, browser)
elif isinstance(wtcontrol, webtest.forms.Submit):
if wtcontrol.attrs.get('type', 'submit') == 'image':
return ImageControl(wtcontrol, form, elem, browser)
else:
return SubmitControl(wtcontrol, form, elem, browser)
else:
return Control(wtcontrol, form, elem, browser)
def simpleControlFactory(wtcontrol, form, elemindex, browser):
if isinstance(wtcontrol, webtest.forms.Radio):
elems = [e for e in elemindex
if e.attrs.get('name') == wtcontrol.name]
return RadioListControl(wtcontrol, form, elems, browser)
elem = elemindex[wtcontrol.pos]
if isinstance(wtcontrol, (webtest.forms.Select,
webtest.forms.MultipleSelect)):
return ListControl(wtcontrol, form, elem, browser)
elif isinstance(wtcontrol, webtest.forms.Submit):
if wtcontrol.attrs.get('type', 'submit') == 'image':
return ImageControl(wtcontrol, form, elem, browser)
else:
return SubmitControl(wtcontrol, form, elem, browser)
else:
return Control(wtcontrol, form, elem, browser)
content_type=None):
"""
Do a generic request.
"""
environ = self._make_environ(extra_environ)
inline_uploads = []
# this supports OrderedDict
if isinstance(params, dict) or hasattr(params, 'items'):
params = list(params.items())
if isinstance(params, (list, tuple)):
inline_uploads = [v for (k, v) in params
if isinstance(v, (forms.File, forms.Upload))]
if len(inline_uploads) > 0:
content_type, params = self.encode_multipart(
params, upload_files or ())
environ['CONTENT_TYPE'] = content_type
else:
params = utils.encode_params(params, content_type)
if upload_files or \
(content_type and
to_bytes(content_type).startswith(b'multipart')):
params = urlparse.parse_qsl(params, keep_blank_values=True)
content_type, params = self.encode_multipart(
params, upload_files or ())
environ['CONTENT_TYPE'] = content_type
elif params:
environ.setdefault('CONTENT_TYPE',
infos = []
if 'readonly' in ctrl.attrs or tp == 'hidden':
infos.append('readonly')
if 'disabled' in ctrl.attrs:
infos.append('disabled')
classnames = {'password': "PasswordControl",
'hidden': "HiddenControl",
'email': "EMailControl",
}
clname = classnames.get(tp, "TextControl")
return "<%s(%s=%s)%s>" % (
clname, toStr(ctrl.name), toStr(ctrl.value),
' (%s)' % (', '.join(infos)) if infos else '')
if isinstance(ctrl, webtest.forms.File):
return repr(ctrl) + "<-- unknown"
raise NotImplementedError(str((self, ctrl)))
def mechRepr(self):
# emulate mechanize control representation
toStr = self.browser.toStr
ctrl = self._control
if isinstance(ctrl, (webtest.forms.Text, webtest.forms.Email)):
tp = ctrl.attrs.get('type')
infos = []
if 'readonly' in ctrl.attrs or tp == 'hidden':
infos.append('readonly')
if 'disabled' in ctrl.attrs:
infos.append('disabled')
classnames = {'password': "PasswordControl",
'hidden': "HiddenControl",
'email': "EMailControl",
}
clname = classnames.get(tp, "TextControl")
return "<%s(%s=%s)%s>" % (
clname, toStr(ctrl.name), toStr(ctrl.value),
' (%s)' % (', '.join(infos)) if infos else '')
def _selectedIndex(self, index):
self._control.force_value(webtest.forms.NoValue)
self._control.selectedIndex = index
def _validate_params(self, params, method):
if not params:
return
# params can be a list or a dict
if hasattr(params, 'items'):
params = params.items()
for k, v in params:
if not isinstance(k, basestring):
raise TypeError('%s key %s is %s, not str' %
(method, k, type(k)))
if not isinstance(v, (basestring, webtest.forms.File)):
raise TypeError(
'%s key %s has value %s of type %s, not str. ' %
(method, k, v, type(v)))
fcontent = mimetypes.guess_type(filename.decode('utf8'))[0]
fcontent = to_bytes(fcontent)
fcontent = fcontent or b'application/octet-stream'
lines.extend([
b'--' + boundary,
b'Content-Disposition: form-data; ' +
b'name="' + key + b'"; filename="' + filename + b'"',
b'Content-Type: ' + fcontent, b'', value])
for key, value in params:
if isinstance(key, text_type):
try:
key = key.encode('ascii')
except: # pragma: no cover
raise # field name are always ascii
if isinstance(value, forms.File):
if value.value:
_append_file([key] + list(value.value))
else:
# If no file was uploaded simulate an empty file with no
# name like real browsers do:
_append_file([key, b'', b''])
elif isinstance(value, forms.Upload):
file_info = [key, value.filename]
if value.content is not None:
file_info.append(value.content)
if value.content_type is not None:
file_info.append(value.content_type)
_append_file(file_info)
else:
if isinstance(value, int):
value = str(value).encode('utf8')