This commit is contained in:
Kovid Goyal 2011-09-20 18:50:26 -06:00
parent 42254a6f64
commit d4c9b0b6fd
2 changed files with 27 additions and 9 deletions

View File

@ -41,19 +41,21 @@ class Control(object):
class RadioControl(object):
ATTR = 'checked'
def __init__(self, name, controls):
self.name = name
self.type = 'radio'
self.values = {unicode(c.attribute('value')):c for c in controls}
def __repr__(self):
return 'RadioControl(%s)'%(', '.join(self.values))
return '%s(%s)'%(self.__class__.__name__, ', '.join(self.values))
@dynamic_property
def value(self):
def fget(self):
for val, x in self.values.iteritems():
if unicode(x.attribute('checked')) == 'checked':
if unicode(x.attribute(self.ATTR)) == self.ATTR:
return val
def fset(self, val):
@ -64,11 +66,23 @@ class RadioControl(object):
break
if control is not None:
for x in self.values.itervalues():
x.removeAttribute('checked')
control.setAttribute('checked', 'checked')
x.removeAttribute(self.ATTR)
control.setAttribute(self.ATTR, self.ATTR)
return property(fget=fget, fset=fset)
class SelectControl(RadioControl):
ATTR = 'selected'
def __init__(self, qwe):
self.qwe = qwe
self.name = unicode(qwe.attribute('name'))
self.type = 'select'
self.values = {unicode(c.attribute('value')):c for c in
qwe.findAll('option')}
class Form(object):
def __init__(self, qwe):
@ -80,15 +94,18 @@ class Form(object):
self.input_controls = [x for x in self.input_controls if x.type != 'radio']
rc_names = {x.name for x in rc}
self.radio_controls = {name:RadioControl(name, [x.qwe for x in rc if x.name == name]) for name in rc_names}
selects = list(map(SelectControl, qwe.findAll('select')))
self.select_controls = {x.name:x for x in selects}
def __getitem__(self, key):
for x in self.input_controls:
if key == x.name:
return x
for x in (self.radio_controls, self.select_controls):
try:
return self.radio_controls.get(key)
return x[key]
except KeyError:
pass
continue
raise KeyError('No control with the name %s in this form'%key)
def __repr__(self):

View File

@ -32,6 +32,7 @@ class Server(object):
<div><label>UnChecked Checkbox:</label><input type="checkbox" name="unchecked_checkbox"/></div>
<div><input type="radio" name="sex" value="male" checked="checked" /> Male</div>
<div><input type="radio" name="sex" value="female" /> Female</div>
<div><label>Color:</label><select name="color"><option value="red" selected="selected" /><option value="green" /></select></div>
<div><input type="submit" value="Submit" /></div>
</form>
<form id="image_test" method="post" action="controls_test">
@ -101,6 +102,7 @@ class Test(unittest.TestCase):
'text': ('some text', 'some text'),
'password': ('some password', 'some password'),
'sex': ('female', 'female'),
'color': ('green', 'green'),
}
f = self.browser.select_form('#controls_test')
for k, vals in values.iteritems():
@ -111,7 +113,6 @@ class Test(unittest.TestCase):
self.assertEqual(vals[1], dat.get(k, None),
'Field %s: %r != %r'%(k, vals[1], dat.get(k, None)))
def test_image_submit(self):
'Test submitting a form with a image as the submit control'
self.assertEqual(self.browser.visit('http://127.0.0.1:%d'%self.port),