source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/templates/renderDiscoverySet.py @ 3501

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/templates/renderDiscoverySet.py@3501
Revision 3501, 7.8 KB checked in by cbyrom, 13 years ago (diff)

Update ows_server code to use new ndgUtils module - and remove files that have
been migrated to this module.

Line 
1from ows_server.models.DiscoveryState import DiscoveryState
2from ows_server.models.DIF import DIF
3from htmlUtilities import *
4from renderService import *
5debug=1
6WIDTH="70%"
7
8def 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   
30def 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='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+='.'
45    print state
46    stml='Search for <span class="searchString">%s</span>%s.'%(state.searchString,state.constraints)
47    html='''<div class="stridePosition"><table width="100%%"><tbody>
48            <tr><td align="left" width="%s">%s</td><td align="right">%s</td></tr></tbody></table></div>'''%(WIDTH,stml,html)
49    return html
50   
51       
52def renderDiscoverySet(difSet,state,config, linkto='NDG_B_SERVICE',selector=None):
53    '''Takes a set of xml DIFS from a discovery search (difSet) and renders a
54    list of responses as a table. The argument is
55            linkto (string) - clicking on the select widget should add the url for
56                              this service to the selected list
57     Note that state is a DiscoveryState instance containing the sessionID from the search,
58     and the offset, stride, and total number of results ...
59     If present, selector provides a class with two attributes: the url for adding more slections
60     and a list of already selected uris.
61     '''
62           
63    #
64    # List of methods which deal with the actual row content ...
65    #   
66    def doubleheader(args):
67        return '''<table><tbody><tr><td colspan="2" align="center">%s</td></tr>
68                  <tr><td align="center">%s</td><td align="center">%s</td></tr></tbody></table>'''%args
69    def row(rowlist,rclass='rbgWhite',header=0):
70        print rowlist
71        r= '''<tr class="%s"><td width="%s">%s</td><td width="12%%" align="center">%s</td>
72            '''%(rclass,WIDTH,rowlist[0],rowlist[1])
73        ll=len(rowlist)
74        if not header:
75            for d in rowlist[2:]: r+='<td align="center">%s</td>'%d
76        else:
77            r+='<td align="center" colspan="2" width="12%%">%s</td>'%rowlist[2]
78            for d in rowlist[3:]: r+='<td align="center">%s</td>'%d
79        r+='</tr>'
80        #if ll==3:
81        #    r+='<td align="center" colspan="2" width="12%%">%s</td></tr>'%rowlist[2]
82        #elif ll==4 or ll==5:
83        #    r+='<td align="center">%s</td><td align="center">%s</td>'%(rowlist[2],rowlist[3])
84        #    if ll==5:
85        #        r+='<td>%s</td></tr>'%rowlist[4]
86        #    else: r+='</tr>'
87        #else:
88        #    raise 'Coding error in renderDiscovery'
89        return r
90       
91    def spatialBox(elem):
92        return elem.bbox.toHTMLbox()
93     
94    def temporalRange(elem):
95        return ''
96   
97    def serviceList(d):
98        ll=span(' Links: ','ndgem')
99        ndg=0
100        serviceRenderer=renderService(config)
101        if d.binding is not None: ll+=serviceRenderer.get(d.binding)[0]
102        for i in d.services: print i.url
103        for item in d.services:
104            ll+=', '
105            #s=0
106            if item.contentType == 'NDG_A_SERVICE': 
107                ndg+=1
108            elif item.contentType == 'NDG_B_SERVICE':
109                ndg+=1
110            print 'starting services',item.url
111            ll+=serviceRenderer.get(item)[0]
112            print 'finishing services',item.url
113        if ndg<2: ndg=0
114        return ndg,ll
115
116   
117    # ##################################
118    # Actual html production follows
119    # ##################################
120   
121    if selector!=None:
122        html='<form action="%s">'%selector.url
123        alreadySelected=selector.alreadySelected
124        selector=1
125    else:
126        html=''
127        selector=0
128   
129    # previous versions of this code had options, but let's simplify this a lot ...
130   
131    spatial=doubleheader(('Spatial&nbsp;Coverage','(-ve:W,S;','+ve:N,E)'))
132    temporal=doubleheader(('Temporal&nbsp;Coverage','Start','End'))
133    columns=['Dataset',spatial,temporal] 
134    if selector: columns.append('Select')
135    html+='<table><tbody>'+row(columns,rclass="rbgBeige",header=1)
136   
137    # ok, now let's cycle through the hits
138    i=1
139    n=0
140    ncols=4
141    if selector:ncols=5
142    for item in difSet:
143        n+=1
144        d=item # we used to do the diffing here ... but we don't anymore
145        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
146        i=-1*i
147        if d.tree is None:
148            html+='<tr class="%s"><td colspan="%s"> Unparseable record </td></tr>'%(bgc,ncols)
149        else:
150            ndg,slist=serviceList(d)
151            rlist=['',]
152            if d.briefCitation <>'':
153                rlist[0]='%s: %s'%(span('Citation','ndgem'),d.briefCitation)
154            else:
155                rlist[0]='%s: %s'%(span('Title','ndgem'),abbreviate(d.name,60))
156            rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
157            if d.binding is not None: rlist[0]+=' '+hyperlink('(more)',d.binding.url)
158            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.url())
159            rlist[0]+=slist
160            rlist.append(spatialBox(d))
161            rlist.append(htmlTime(d.timeCoverage[0]))
162            rlist.append(htmlTime(d.timeCoverage[1]))
163            if selector:
164                if ndg:
165                    #nn='selector-%s'%n
166                    #uu=d.ndgObject.uri
167                    #if uu not in alreadySelected:
168                    #    ss='<input type="checkbox" name="%s"/><input type="hidden" value="%s" name="%s-uri"/>'%(nn,uu,nn)
169                    #else:
170                    #    ss+='selected'
171                    ss='selectable'
172                else:
173                    ss=''
174                rlist.append(ss)
175            html+=row(rlist,rclass=bgc)
176
177    html+='</tbody></table>'
178    if selector: 
179        html+='<input type="submit" value="Collect Selected Datasets"/></form>'
180    html='<div class="ListOfResults">%s\n%s</div>'%(htmlStride(state),html)
181    return html.encode('utf-8')
182
183import unittest
184
185class TestCase(unittest.TestCase):
186
187    def testStrideself(self):
188        ''' Test striding through a dataset '''
189        res=CalculateStride(1,10,15)
190        self.assertEqual([[11,5],[]],res)
191        res=CalculateStride(11,5,15)
192        self.assertEqual([[],[1,10]],res)
193        res=CalculateStride(11,10,50)
194        self.assertEqual([[21,10],[1,10]],res)
195 
196    def testHtmlStride(self):
197        ''' Test the html striding part '''
198        DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg',
199                      'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'}
200        d=DiscoveryState('123','blah',DummyEnviron,12)
201        html=htmlStride(d)
202        print html
203       
204if __name__=="__main__":
205    unittest.main()
206             
Note: See TracBrowser for help on using the repository browser.