def test_post_with_file_upload(self): uploaded_file_name = 'test.txt' uploaded_file_contents = 'test content file upload' if PY3: uploaded_file_contents = to_bytes(uploaded_file_contents) deform_upload_file_app = get_submit_app('deform', deform_upload_fields_text) app = webtest.TestApp(deform_upload_file_app) display = app.post( "/", OrderedDict([('_charset_', ''), ('__formid__', 'deform'), ('title', 'testtitle'), ('__start__', 'fileupload:mapping'), ('fileupload', webtest.Upload(uploaded_file_name, uploaded_file_contents)), ('__end__', 'fileupload:mapping'), ('description', 'testdescription'), ('Submit', 'Submit')])) self.assertIn( """ _charset_: __formid__:deform title:testtitle __start__:fileupload:mapping fileupload:test.txt:test content file upload __end__:fileupload:mapping description:testdescription Submit:Submit""".strip(), display, display)
def _parse_fields(self): fields = OrderedDict() field_order = [] tags = ('input', 'select', 'textarea', 'button') for pos, node in enumerate(self.html.findAll(tags)): attrs = dict(node.attrs) tag = node.name name = None if 'name' in attrs: name = attrs.pop('name') if tag == 'textarea': if node.text.startswith('\r\n'): text = node.text[2:] elif node.text.startswith('\n'): text = node.text[1:] else: text = node.text attrs['value'] = text tag_type = attrs.get('type', 'text').lower() if tag == 'select': tag_type = 'select' if tag_type == "select" and "multiple" in attrs: tag_type = "multiple_select" if tag == 'button': tag_type = 'submit' FieldClass = self.FieldClass.classes.get(tag_type, self.FieldClass) if tag == 'input': if tag_type == 'radio': field = fields.get(name) if not field: field = FieldClass(self, tag, name, pos, **attrs) fields.setdefault(name, []).append(field) field_order.append((name, field)) else: field = field[0] assert isinstance(field, self.FieldClass.classes['radio']) field.options.append((attrs.get('value'), 'checked' in attrs)) continue elif tag_type == 'file': if 'value' in attrs: del attrs['value'] field = FieldClass(self, tag, name, pos, **attrs) fields.setdefault(name, []).append(field) field_order.append((name, field)) if tag == 'select': for option in node('option'): field.options.append( (option.attrs.get('value', option.text), 'selected' in option.attrs)) self.field_order = field_order self.fields = fields
def _parse_fields(self): fields = OrderedDict() field_order = [] tags = ('input', 'select', 'textarea', 'button') for pos, node in enumerate(self.html.findAll(tags)): attrs = dict(node.attrs) tag = node.name name = None if 'name' in attrs: name = attrs.pop('name') if tag == 'textarea': if node.text.startswith('\r\n'): # pragma: no cover text = node.text[2:] elif node.text.startswith('\n'): text = node.text[1:] else: text = node.text attrs['value'] = text tag_type = attrs.get('type', 'text').lower() if tag == 'select': tag_type = 'select' if tag_type == "select" and "multiple" in attrs: tag_type = "multiple_select" if tag == 'button': tag_type = 'submit' FieldClass = self.FieldClass.classes.get(tag_type, self.FieldClass) # https://github.com/Pylons/webtest/issues/73 if sys.version_info[:2] <= (2, 6): attrs = dict( (k.encode('utf-8') if isinstance(k, unicode) else k, v) for k, v in attrs.items()) # https://github.com/Pylons/webtest/issues/131 reserved_attributes = ('form', 'tag', 'pos') for attr in reserved_attributes: if attr in attrs: del attrs[attr] if tag == 'input': if tag_type == 'radio': field = fields.get(name) if not field: field = FieldClass(self, tag, name, pos, **attrs) fields.setdefault(name, []).append(field) field_order.append((name, field)) else: field = field[0] assert isinstance(field, self.FieldClass.classes['radio']) field.options.append((attrs.get('value'), 'checked' in attrs, None)) continue elif tag_type == 'file': if 'value' in attrs: del attrs['value'] field = FieldClass(self, tag, name, pos, **attrs) fields.setdefault(name, []).append(field) field_order.append((name, field)) if tag == 'select': for option in node('option'): field.options.append( (option.attrs.get('value', option.text), 'selected' in option.attrs, option.text)) self.field_order = field_order self.fields = fields