Changeset 1802:ad94f5745dac

Show
Ignore:
Timestamp:
10/20/09 15:51:41 (9 months ago)
Author:
Daniel Grana <dangra@…>
Branch:
default
Message:

Improve FormRequest?.from_response method to pass click data arguments to ClientForm? library

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • docs/topics/request-response.rst

    r1562 r1802  
    251251    addition to the standard :class:`Request` methods: 
    252252 
    253     .. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata, ...]) 
     253    .. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata=None, clickdata=None, ...]) 
    254254 
    255255       Returns a new :class:`FormRequest` object with its form field values 
     
    271271          overridden by the one passed in this parameter. 
    272272       :type formdata: dict 
     273 
     274       :param clickdata: The arguments in clickdata are passed directly to 
     275          ClientForm's click_request_data() method. See <ClientForm> for more 
     276          info. 
     277       :type clickdata: dict 
    273278 
    274279       The other parameters of this class method are passed directly to the 
  • scrapy/http/request/form.py

    r1559 r1802  
    3838 
    3939    @classmethod 
    40     def from_response(cls, response, formnumber=0, formdata=None, **kwargs): 
     40    def from_response(cls, response, formnumber=0, formdata=None, clickdata=None, **kwargs): 
    4141        encoding = getattr(response, 'encoding', 'utf-8') 
    4242        forms = ParseFile(StringIO(response.body), response.url, 
     
    5252            # formdata fields properly can properly override existing ones, 
    5353            # which is the desired behaviour 
    54             form.controls = [c for c in form.controls if c.name not in formdata.keys()] 
     54            form.controls = [c for c in form.controls if c.name not in formdata] 
    5555            for k, v in formdata.iteritems(): 
    5656                for v2 in v if hasattr(v, '__iter__') else [v]: 
    5757                    form.new_control('text', k, {'value': v2}) 
    58                      
    59         url, body, headers = form.click_request_data()        
    60         request = cls(url, method=form.method, body=body, headers=headers, **kwargs) 
    61         return request 
     58 
     59        url, body, headers = form.click_request_data(**(clickdata or {})) 
     60        return cls(url, method=form.method, body=body, headers=headers, **kwargs) 
  • scrapy/tests/test_http_request.py

    r1743 r1802  
    259259        self.assertEqual(fs['two'].value, '2') 
    260260 
     261    def test_from_response_submit_first_clickeable(self): 
     262        respbody = """ 
     263<form action="get.php" method="GET"> 
     264<input type="submit" name="clickeable1" value="clicked1"> 
     265<input type="hidden" name="one" value="1"> 
     266<input type="hidden" name="two" value="3"> 
     267<input type="submit" name="clickeable2" value="clicked2"> 
     268</form> 
     269        """ 
     270        response = Response("http://www.example.com/this/list.html", body=respbody) 
     271        r1 = self.request_class.from_response(response, formdata={'two': '2'}) 
     272        urlargs = cgi.parse_qs(urlparse(r1.url).query) 
     273        self.assertEqual(urlargs['clickeable1'], ['clicked1']) 
     274        self.assertFalse('clickeable2' in urlargs, urlargs) 
     275        self.assertEqual(urlargs['one'], ['1']) 
     276        self.assertEqual(urlargs['two'], ['2']) 
     277 
     278    def test_from_response_submit_not_first_clickeable(self): 
     279        respbody = """ 
     280<form action="get.php" method="GET"> 
     281<input type="submit" name="clickeable1" value="clicked1"> 
     282<input type="hidden" name="one" value="1"> 
     283<input type="hidden" name="two" value="3"> 
     284<input type="submit" name="clickeable2" value="clicked2"> 
     285</form> 
     286        """ 
     287        response = Response("http://www.example.com/this/list.html", body=respbody) 
     288        r1 = self.request_class.from_response(response, formdata={'two': '2'}, clickdata={'name': 'clickeable2'}) 
     289        urlargs = cgi.parse_qs(urlparse(r1.url).query) 
     290        self.assertEqual(urlargs['clickeable2'], ['clicked2']) 
     291        self.assertFalse('clickeable1' in urlargs, urlargs) 
     292        self.assertEqual(urlargs['one'], ['1']) 
     293        self.assertEqual(urlargs['two'], ['2']) 
     294 
    261295    def test_from_response_errors_noform(self): 
    262296        respbody = """<html></html>"""