示例#1
0
def create_form_helper(form_data):
    '''
    Creates a dc.form object from a dict container
    
    @param form_data: A list containing dicts representing a form's
        internal structure
    @return: A dc.form object from `form_data`
    '''
    new_form = form.form()
    
    for elem_data in form_data:
        elem_type = elem_data['tagname']
        attrs = elem_data.items()
        
        if elem_type == 'input':
            type = elem_data['type']
            
            if type == 'radio':
                new_form.addRadio(attrs)
            elif type == 'checkbox':
                new_form.addCheckBox(attrs)
            else:
                pass
            
        elif elem_type == 'select':
            new_form.addSelect(elem_data['name'], elem_data['options'])
    
    return new_form
示例#2
0
    def _handle_form_tag(self, tag, attrs):
        """
        Handles the form tags.

        This method also looks if there are "pending inputs" in the self._saved_inputs list
        and parses them.
        """
        # Find the method
        method = "GET"
        foundMethod = False
        for attr in attrs:
            if attr[0].lower() == "method":
                method = attr[1].upper()
                foundMethod = True

        if not foundMethod:
            om.out.debug("htmlParser found a form without a method. Using GET as the default.")

        # Find the action
        foundAction = False
        for attr in attrs:
            if attr[0].lower() == "action":
                decoded_action = self._decode_URL(attr[1], self._encoding)
                action = urlParser.urlJoin(self._baseUrl, decoded_action)
                foundAction = True

        if not foundAction:
            msg = "htmlParser found a form without an action attribute. Javascript may be used..."
            msg += " but another option (mozilla does this) is that the form is expected to be "
            msg += " posted back to the same URL (the one that returned the HTML that we are "
            msg += " parsing)."
            om.out.debug(msg)
            action = self._source_url

        # Create the form object and store everything for later use
        self._insideForm = True
        form_obj = form.form()
        form_obj.setMethod(method)
        form_obj.setAction(action)
        self._forms.append(form_obj)

        # Now I verify if they are any input tags that were found outside the scope of a form tag
        for tag, attrs in self._saved_inputs:
            # Parse them just like if they were found AFTER the form tag opening
            self._handle_input_tag_inside_form(tag, attrs)
        # All parsed, remove them.
        self._saved_inputs = []
示例#3
0
    def _findForms(self, tag, attrs):
        '''
        This method finds forms inside an WML document.

        This is a WML form example:
            <go method="post" href="dataReceptor.php">
                <postfield name="clave" value="$(clave)"/>
                <postfield name="cuenta" value="$(cuenta)"/>
                <postfield name="tipdat" value="D"/>
            </go>        
        '''
        if tag == 'go' :
            #Find the method
            method = 'GET'
            foundMethod = False
            for attr in attrs:
                if attr[0] == 'method':
                    method = attr[1]
                    foundMethod = True
            
            if not foundMethod:
                om.out.debug('wmlParser found a form without a method. Using GET as the default.')
            
            #Find the action
            foundAction = False
            for attr in attrs:
                if attr[0] == 'href':
                    action = self._baseUrl.urlJoin( attr[1] )
                    action = self._decode_URL( action , self._encoding)
                    foundAction = True
                    
            if not foundAction:
                om.out.debug('wmlParser found a form without an action. Javascript is being used.')
                # <form name="frmRegistrar" onsubmit="valida();">
            else:
                self._insideForm = True
                f = form.form()
                f.setMethod( method )           
                f.setAction( action )
                self._forms.append( f )
        
        if self._insideForm:
            # I am inside a form, I should parse input tags
            if tag in [ 'input', 'postfield','setvar' ]:
                # We are working with the last form
                f = self._forms[ len(self._forms) -1 ]
                f.addInput( attrs )
                    
            elif tag == 'select':
                self._insideSelect = True
                name = ''
                
                # Get the name
                self._selectTagName = ''
                for attr in attrs:
                    if attr[0].lower() == 'name':
                        self._selectTagName = attr[1]
                
                if not self._selectTagName:
                    for attr in attrs:
                        if attr[0].lower() == 'id':
                            self._selectTagName = attr[1]
                    
                if not self._selectTagName:
                    om.out.debug('wmlParser found a select tag without a name attr !')
                    self._insideSelect = False
            
            if self._insideSelect:
                if tag == 'option':
                    # We are working with the last form in the list
                    f = self._forms[ len(self._forms) -1 ]
                    attrs.append( ('name',self._selectTagName) ) 
                    f.addInput( attrs )