1 | from paste.request import parse_querystring,construct_url |
---|
2 | class DiscoveryState: |
---|
3 | ''' This class holds the state associated with presenting multiple slices |
---|
4 | of a large result set ''' |
---|
5 | def __init__(self,sessionID,environ,hits,offset=1,stride=10): |
---|
6 | ''' On instantiation, provide |
---|
7 | the backend sessionID |
---|
8 | the application URL that produced this query |
---|
9 | the stride through the result set ''' |
---|
10 | self.environ=environ # the wsgi environment |
---|
11 | self.sessID=sessionID |
---|
12 | self.hits=hits |
---|
13 | self.offset=offset |
---|
14 | self.stride=stride |
---|
15 | def geturl(self,**kw): |
---|
16 | ''' Get a url from the wsgi environment, modified by the keyword arguments offset and stride |
---|
17 | which are to be part of the querystring ''' |
---|
18 | args=dict(parse_querystring(self.environ)) |
---|
19 | offset,stride=kw.get('offset'),kw.get('stride') |
---|
20 | if offset is not None:args['start']=offset |
---|
21 | if stride is not None:args['howmany']=stride |
---|
22 | q='' |
---|
23 | for i in args: q+='%s=%s&'%(i,args[i]) |
---|
24 | q=q[0:-1] |
---|
25 | url=construct_url(self.environ, with_query_string=True, with_path_info=True, querystring=q) |
---|
26 | return url |
---|
27 | |
---|
28 | import unittest |
---|
29 | |
---|
30 | class TestCase(unittest.TestCase): |
---|
31 | |
---|
32 | def testDiscoveryState(self): |
---|
33 | ''' Test creation of a discovery state variable ''' |
---|
34 | DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg', |
---|
35 | 'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'} |
---|
36 | d=DiscoveryState('123',DummyEnviron,12) |
---|
37 | self.assertEqual(d.geturl(offset='11',stride='20'), |
---|
38 | 'http://example.ndg/discovery?start=11&howmany=20') |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | if __name__=="__main__": |
---|
43 | unittest.main() |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | |
---|