Changeset 2340 for TI07-MOLES/trunk/PythonCode/wsgi/renderDiscoverySet.py
- Timestamp:
- 28/03/07 12:26:31 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TI07-MOLES/trunk/PythonCode/wsgi/renderDiscoverySet.py
r2330 r2340 5 5 from renderService import * 6 6 debug=1 7 8 def CalculateStride(start,stride,total): 9 ''' Work out the previous and next chunks, returns a list of lists, the first of which 10 is the start and offset of the next chunk, the second of which is the staft and offset 11 of the previous chunk ''' 12 result=[] 13 defStride=10 14 if start+stride<total: 15 #there are more to look at 16 r=[start+stride,stride] 17 if r[0]+r[1]-1>total: r[1]=total+1-r[0] 18 result.append(r) 19 else:result.append([]) 20 if start>1: 21 #there are previous records 22 b=max(stride,defStride) 23 r=[start-b,b] 24 if r[0]<1: r[0]=1 25 if r[1]>total: r[1]=total 26 result.append(r) 27 else: result.append([]) 28 return result 29 30 def htmlStride(state): 31 ''' Given the previous and next list from CalculateStride, and the 32 current state class (which provides a method for the fundamental URL, 33 return a previous next piece of html ''' 34 html='<p>Results %s to %s of %s'%(state.offset,state.offset+state.stride-1,state.hits) 35 r=CalculateStride(state.offset,state.stride,state.hits) 36 if r[0]<>[]: 37 s1='Next %s'%r[0][1] 38 nexturl=state.geturl(offset=r[0][0],stride=r[0][1]) 39 html+=', %s'%hyperlink(s1,nexturl) 40 if r[1]<>[]: 41 l1='Previous %s'%r[1][1] 42 lasturl=state.geturl(offset=r[1][0],stride=r[1][1]) 43 html+=', %s'%hyperlink(l1,lasturl) 44 html+='.</p>' 45 return html 46 47 7 48 def renderDiscoverySet(difSet,state,config,summary=0,services=0,spatial=0,temporal=0, 8 49 linkto='NDG_B_SERVICE',selector=None): … … 109 150 110 151 html+='</tbody></table>' 111 112 #now work out the links to step through a large result set 113 #html+='<p>Results %s to %s of %s'%(state.offset,min(state.offset+state.stride-1,state.hits),state.hits) 114 html+='<p>Results %s to %s of %s'%(state.offset,state.offset+len(difSet)-1,state.hits) 115 if state.stride < state.hits: 116 #1,10,21 11,20,21 21,21,21 117 if state.offset+state.stride-1<state.hits: 118 next1=state.offset+state.stride #11,21 119 nextNum=min(state.stride,1+state.hits-next1) # 10,1 120 s1='Next %s'%nextNum 121 nexturl=state.geturl(offset=next1,stride=nextNum) 122 html+=', %s'%hyperlink(s1,nexturl) 123 if state.offset>1: 124 last1=max(1,state.offset-state.stride) 125 lastNum=min(state.stride,1+state.hits-last1) 126 if lastNum<min(20,state.hits): lastNum=20 127 l1='Previous %s'%lastNum 128 lasturl=state.geturl(offset=last1,stride=lastNum) 129 html+=', %s'%hyperlink(l1,lasturl) 130 else: 131 html+='</p>' 132 html+='</div>' 152 html+=htmlStride(state)+'</div>' 133 153 return html 134 154 155 import unittest 156 157 class TestCase(unittest.TestCase): 158 159 def testStrideself(self): 160 ''' Test striding through a dataset ''' 161 res=CalculateStride(1,10,15) 162 self.assertEqual([[11,5],[]],res) 163 res=CalculateStride(11,5,15) 164 self.assertEqual([[],[1,10]],res) 165 res=CalculateStride(11,10,50) 166 self.assertEqual([[21,10],[1,10]],res) 167 168 def testHtmlStride(self): 169 ''' Test the html striding part ''' 170 DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg', 171 'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'} 172 d=DiscoveryState('123',DummyEnviron,12) 173 html=htmlStride(d) 174 print html 175 176 if __name__=="__main__": 177 unittest.main() 178
Note: See TracChangeset
for help on using the changeset viewer.