source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DiscoveryState.py @ 2672

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DiscoveryState.py@2672
Revision 2672, 2.3 KB checked in by lawrence, 12 years ago (diff)

Interim commit of discovery/browse in ows stack.

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