Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BC to Bootstrap form converter - INCOMPLETE but almost there
from bs4 import BeautifulSoup
## TODO: Add wrapper for non-input fields and embed fields into labels
def exctract_field(el):
print el
inputs = el.find_all('input')
if inputs:
return inputs, "input"
selects = el.find_all('select')
if selects:
return selects, "select"
tareas = el.find_all('textarea')
if tareas:
return tareas, "textarea"
def input_wrap(label, inputs, newsoup):
try:
label = label[0]
except IndexError:
pass
else:
input_type = input_type_check(inputs)
for i in inputs:
i['class'] = input_type
bst = newsoup.new_tag('div')
bst['class'] = 'form-group'
label.wrap(bst)
i.wrap(bst)
newsoup.form.insert(1, bst)
print newsoup
def input_type_check(inputs):
for i in inputs:
if i['type'] == 'checkbox':
return "checkbox"
elif i['type'] == 'radio':
return 'radio'
else:
return "form-control"
form = """
<form name="catwebformform83394" method="post" onsubmit="return checkWholeForm83394(this)" enctype="multipart/form-data" action="http://twoblokes.businesscatalyst.com/FormProcessv2.aspx?WebFormID=10090&OID={module_oid}&OTYPE={module_otype}&EID={module_eid}&CID={module_cid}"> <span class="req">*</span> Required<table class="webform" cellspacing="0" cellpadding="2" border="0"><tr><td><label for="CellPhone">Cell Phone Number <span class="req">*</span></label><br /><input type="text" name="CellPhone" id="CellPhone" class="cat_textbox" maxlength="255" /></td></tr><tr><td><label for="HomePhone">Home Phone Number <span class="req">*</span></label><br /><input type="text" name="HomePhone" id="HomePhone" class="cat_textbox" maxlength="255" /></td></tr><tr><td><label for="EmailAddress">Email Address <span class="req">*</span></label><br /><input type="text" name="EmailAddress" id="EmailAddress" class="cat_textbox" maxlength="255" /> </td></tr><tr><td><label for="Title">Title</label><br /><select name="Title" id="Title" class="cat_dropdown_smaller"><option value="331225">DR</option><option value="331224">MISS</option><option value="331221" selected="selected">MR</option><option value="331222">MRS</option><option value="331223">MS</option></select></td></tr><tr><td><label for="FirstName">First Name <span class="req">*</span></label><br /><input type="text" name="FirstName" id="FirstName" class="cat_textbox" maxlength="255" /> </td></tr><tr><td><label for="LastName">Last Name <span class="req">*</span></label><br /><input type="text" name="LastName" id="LastName" class="cat_textbox" maxlength="255" /> </td></tr><tr><td><label for="CAT_Custom_869">Comments/Enquiries</label><br /><textarea name="CAT_Custom_869" id="CAT_Custom_869" cols="10" rows="4" class="cat_listbox" onkeydown="if(this.value.length>=4000)this.value=this.value.substring(0,3999);"></textarea></td></tr><tr><td><label>Preferred Contact Method</label><br /><input type="radio" name="CAT_Custom_868" id="CAT_Custom_868_0" value="Phone" />Phone<br /><input type="radio" name="CAT_Custom_868" id="CAT_Custom_868_1" value="Email" />Email</td></tr><tr><td><input class="cat_button" type="submit" value="Submit" id="catwebformbutton" /></td></tr></table><script type="text/javascript" src="http://twoblokes.businesscatalyst.com/CatalystScripts/ValidationFunctions.js?vs=b1641.r459020-phase1"></script><script type="text/javascript">
//<![CDATA[
var submitcount83394 = 0;function checkWholeForm83394(theForm){var why = "";if (theForm.CellPhone) why += isEmpty(theForm.CellPhone.value, "Cell Phone Number"); if (theForm.HomePhone) why += isEmpty(theForm.HomePhone.value, "Home Phone Number"); if (theForm.EmailAddress) why += checkEmail(theForm.EmailAddress.value); if (theForm.FirstName) why += isEmpty(theForm.FirstName.value, "First Name"); if (theForm.LastName) why += isEmpty(theForm.LastName.value, "Last Name"); if(why != ""){alert(why);return false;}if(submitcount83394 == 0){submitcount83394++;theForm.submit();return false;}else{alert("Form submission is in progress.");return false;}}
//]]>
</script></form>
"""
soup = BeautifulSoup(form)
newsoup = BeautifulSoup(form)
newform = newsoup.form
newform.clear()
tds = soup.find_all('td')
for td in tds:
label = td.find_all('label')
fields, field_type = exctract_field(td)
if field_type == 'input':
input_wrap(label, fields, newsoup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment