source: TI07-MOLES/trunk/PythonCode/wsgi/DiscoveryState.py @ 2487

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/DiscoveryState.py@2522
Revision 2487, 1.8 KB checked in by lawrence, 12 years ago (diff)

Modifications to support cut-down stub-b xquery.
Slight rearrangement of page positioning in discovery. (Needs CSS
support)

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