form: Add comments
This commit is contained in:
parent
fd3484e063
commit
3fcfc9a980
1 changed files with 12 additions and 2 deletions
|
|
@ -165,27 +165,37 @@ class Form(OrderedDict):
|
||||||
self.name = el.attrib.get('name', '')
|
self.name = el.attrib.get('name', '')
|
||||||
submits = 0
|
submits = 0
|
||||||
|
|
||||||
|
# Find all elements of the form that will be useful to create the request
|
||||||
for inp in el.xpath('.//input | .//select | .//textarea'):
|
for inp in el.xpath('.//input | .//select | .//textarea'):
|
||||||
|
# Step 1: Ignore some elements
|
||||||
try:
|
try:
|
||||||
name = inp.attrib['name']
|
name = inp.attrib['name']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Ignore checkboxes and radios that are not selected
|
||||||
|
# as they are just not present in the request instead of being empty
|
||||||
|
# values.
|
||||||
try:
|
try:
|
||||||
if inp.attrib['type'] in ('checkbox', 'radio') and 'checked' not in inp.attrib:
|
if inp.attrib['type'] in ('checkbox', 'radio') and 'checked' not in inp.attrib:
|
||||||
continue
|
continue
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Either filter the submit buttons, or count how many we have found
|
||||||
try:
|
try:
|
||||||
if inp.attrib['type'] == 'submit':
|
if inp.attrib['type'] == 'submit':
|
||||||
|
# If we chose a submit button, ignore all others
|
||||||
if self.submit_el is not None and inp is not self.submit_el:
|
if self.submit_el is not None and inp is not self.submit_el:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
# Register that we have found a submit button, and that it will
|
||||||
|
# be used
|
||||||
submits += 1
|
submits += 1
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Step 2: Extract the key-value pair from the remaining elements
|
||||||
if inp.tag == 'select':
|
if inp.tag == 'select':
|
||||||
options = inp.xpath('.//option[@selected]')
|
options = inp.xpath('.//option[@selected]')
|
||||||
if len(options) == 0:
|
if len(options) == 0:
|
||||||
|
|
@ -196,15 +206,15 @@ class Form(OrderedDict):
|
||||||
value = options[0].attrib.get('value', options[0].text or u'')
|
value = options[0].attrib.get('value', options[0].text or u'')
|
||||||
else:
|
else:
|
||||||
value = inp.attrib.get('value', inp.text or u'')
|
value = inp.attrib.get('value', inp.text or u'')
|
||||||
|
# TODO check if value already exists, emit warning
|
||||||
self[name] = value
|
self[name] = value
|
||||||
|
|
||||||
|
# Sanity checks
|
||||||
if submits > 1:
|
if submits > 1:
|
||||||
warnings.warn('Form has more than one submit input, you should chose the correct one', FormSubmitWarning, stacklevel=3)
|
warnings.warn('Form has more than one submit input, you should chose the correct one', FormSubmitWarning, stacklevel=3)
|
||||||
if self.submit_el is not None and self.submit_el is not False and submits == 0:
|
if self.submit_el is not None and self.submit_el is not False and submits == 0:
|
||||||
warnings.warn('Form had a submit element provided, but it was not found', FormSubmitWarning, stacklevel=3)
|
warnings.warn('Form had a submit element provided, but it was not found', FormSubmitWarning, stacklevel=3)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def request(self):
|
def request(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue