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

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

Look and feel of result set is now close to Kay's concepts.

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        self.alternatives=None
19    def geturl(self,**kw):
20        ''' Get a url from the wsgi environment, modified by the keyword arguments offset and stride
21        which are to be part of the querystring '''
22        args=dict(parse_querystring(self.environ))
23        offset,stride=kw.get('offset'),kw.get('stride')
24        if offset is not None:args['start']=offset
25        if stride is not None:args['howmany']=stride
26        q=''
27        for i in args: q+='%s=%s&'%(i,args[i])
28        q=q[0:-1]
29        url=construct_url(self.environ, with_query_string=True, with_path_info=True, querystring=q)
30        url=cgi.escape(url)
31        return url
32    def getNext(self):
33        ''' Get the next slice '''
34        result=[]
35        defStride=10
36        if self.offset+self.stride<self.hits:
37            #there are more to look at
38            r=[self.offset+self.stride,self.stride]
39            if r[0]+r[1]-1>self.hits: r[1]=self.hits+1-r[0]
40            result.append(r)
41        else:result.append([])
42        if self.offset>1:
43            #there are previous records
44            b=max(self.stride,defStride)
45            r=[self.offset-b,b]
46            if r[0]<1: r[0]=1
47            if r[1]>self.hits: r[1]=self.hits
48            result.append(r)
49        else: result.append([])
50        return result
51       
52    def __str__(self):
53        return '''
54___ Discovery State ___
55searchString: %s
56constraints: %s
57sessionID: %s
58hits: %s
59offset,stride: %s,%s
60environment: %s
61========================
62'''%(self.searchString,self.constraints,self.sessID,self.hits,self.offset,self.stride,self.environ)
63       
64if __name__=="__main__":
65   
66    import unittest
67    DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg',
68                      'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'}
69
70    class TestCase(unittest.TestCase):
71       
72        def testDiscoveryState(self):
73            ''' Test creation of a discovery state variable '''
74            d=DiscoveryState('123','blah',DummyEnviron,12,'(none)')
75            self.assertEqual(d.geturl(offset='11',stride='20'),
76                            'http://example.ndg/discovery?start=11&amp;howmany=20')
77            print d
78   
79        def testStrideself(self):
80            ''' Test striding through a dataset '''
81           
82            s=DiscoveryState('123','blah',DummyEnviron,15,'(none)',1,10)
83            res=s.getNext()
84            self.assertEqual([[11,5],[]],res)
85           
86            s=DiscoveryState('123','blah',DummyEnviron,15,'(none)',11,5)
87            res=s.getNext()
88            self.assertEqual([[],[1,10]],res)
89           
90            s=DiscoveryState('123','blah',DummyEnviron,50,'(none)',11,10)
91            res=s.getNext()
92            self.assertEqual([[21,10],[1,10]],res) 
93
94               
95    unittest.main()
96                     
97       
Note: See TracBrowser for help on using the repository browser.