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): class RadioControl(object):
ATTR = 'checked'
def __init__(self, name, controls): def __init__(self, name, controls):
self.name = name self.name = name
self.type = 'radio' self.type = 'radio'
self.values = {unicode(c.attribute('value')):c for c in controls} self.values = {unicode(c.attribute('value')):c for c in controls}
def __repr__(self): def __repr__(self):
return 'RadioControl(%s)'%(', '.join(self.values)) return '%s(%s)'%(self.__class__.__name__, ', '.join(self.values))
@dynamic_property @dynamic_property
def value(self): def value(self):
def fget(self): def fget(self):
for val, x in self.values.iteritems(): for val, x in self.values.iteritems():
if unicode(x.attribute('checked')) == 'checked': if unicode(x.attribute(self.ATTR)) == self.ATTR:
return val return val
def fset(self, val): def fset(self, val):
@ -64,11 +66,23 @@ class RadioControl(object):
break break
if control is not None: if control is not None:
for x in self.values.itervalues(): for x in self.values.itervalues():
x.removeAttribute('checked') x.removeAttribute(self.ATTR)
control.setAttribute('checked', 'checked') control.setAttribute(self.ATTR, self.ATTR)
return property(fget=fget, fset=fset) 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): class Form(object):
def __init__(self, qwe): 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'] self.input_controls = [x for x in self.input_controls if x.type != 'radio']
rc_names = {x.name for x in rc} 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} 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): def __getitem__(self, key):
for x in self.input_controls: for x in self.input_controls:
if key == x.name: if key == x.name:
return x return x
for x in (self.radio_controls, self.select_controls):
try: try:
return self.radio_controls.get(key) return x[key]
except KeyError: except KeyError:
pass continue
raise KeyError('No control with the name %s in this form'%key) raise KeyError('No control with the name %s in this form'%key)
def __repr__(self): 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><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="male" checked="checked" /> Male</div>
<div><input type="radio" name="sex" value="female" /> Female</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> <div><input type="submit" value="Submit" /></div>
</form> </form>
<form id="image_test" method="post" action="controls_test"> <form id="image_test" method="post" action="controls_test">
@ -101,6 +102,7 @@ class Test(unittest.TestCase):
'text': ('some text', 'some text'), 'text': ('some text', 'some text'),
'password': ('some password', 'some password'), 'password': ('some password', 'some password'),
'sex': ('female', 'female'), 'sex': ('female', 'female'),
'color': ('green', 'green'),
} }
f = self.browser.select_form('#controls_test') f = self.browser.select_form('#controls_test')
for k, vals in values.iteritems(): for k, vals in values.iteritems():
@ -111,7 +113,6 @@ class Test(unittest.TestCase):
self.assertEqual(vals[1], dat.get(k, None), self.assertEqual(vals[1], dat.get(k, None),
'Field %s: %r != %r'%(k, vals[1], dat.get(k, None))) 'Field %s: %r != %r'%(k, vals[1], dat.get(k, None)))
def test_image_submit(self): def test_image_submit(self):
'Test submitting a form with a image as the submit control' 'Test submitting a form with a image as the submit control'
self.assertEqual(self.browser.visit('http://127.0.0.1:%d'%self.port), self.assertEqual(self.browser.visit('http://127.0.0.1:%d'%self.port),