source: TI07-MOLES/tags/wsgi-beta/DiscoveryState.py @ 2533

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/tags/wsgi-beta/DiscoveryState.py@5134
Revision 2533, 2.2 KB checked in by lawrence, 13 years ago (diff)

Discovery now defaults to an advanced interface which is getting close
to that required by ticket:731. The first point on ticket:733 is now done too.

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,constraints,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.constraints=constraints # some text to show constraints on search
12        self.sessID=sessionID
13        self.hits=hits
14        self.offset=offset
15        self.stride=stride
16        self.searchString=searchString
17    def geturl(self,**kw):
18        ''' Get a url from the wsgi environment, modified by the keyword arguments offset and stride
19        which are to be part of the querystring '''
20        args=dict(parse_querystring(self.environ))
21        offset,stride=kw.get('offset'),kw.get('stride')
22        if offset is not None:args['start']=offset
23        if stride is not None:args['howmany']=stride
24        q=''
25        for i in args: q+='%s=%s&'%(i,args[i])
26        q=q[0:-1]
27        url=construct_url(self.environ, with_query_string=True, with_path_info=True, querystring=q)
28        return url
29    def __str__(self):
30        return '''
31___ Discovery State ___
32searchString: %s
33constraints: %s
34sessionID: %s
35hits: %s
36offset,stride: %s,%s
37environment: %s
38========================
39'''%(self.searchString,self.constraints,self.sessID,self.hits,self.offset,self.stride,self.environ)
40       
41import unittest
42
43class TestCase(unittest.TestCase):
44   
45    def testDiscoveryState(self):
46        ''' Test creation of a discovery state variable '''
47        DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg',
48                      'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'}
49        d=DiscoveryState('123','blah',DummyEnviron,12,'(none)')
50        self.assertEqual(d.geturl(offset='11',stride='20'),
51                         'http://example.ndg/discovery?start=11&howmany=20')
52        print d
53 
54       
55if __name__=="__main__":
56    unittest.main()
57               
58
59                     
60       
Note: See TracBrowser for help on using the repository browser.