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

Revision 2533, 6.6 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 DiscoveryState import DiscoveryState
2from ndgSearch import ndgSearch
3from DIF import DIF
4from htmlUtilities import *
5from renderService import *
6debug=1
7WIDTH="70%"
8
9def CalculateStride(start,stride,total):
10    ''' Work out the previous and next chunks, returns a list of lists, the first of which
11    is the start and offset of the next chunk, the second of which is the staft and offset
12    of the previous chunk '''
13    result=[]
14    defStride=10
15    if start+stride<total:
16        #there are more to look at
17        r=[start+stride,stride]
18        if r[0]+r[1]-1>total: r[1]=total+1-r[0]
19        result.append(r)
20    else:result.append([])
21    if start>1:
22        #there are previous records
23        b=max(stride,defStride)
24        r=[start-b,b]
25        if r[0]<1: r[0]=1
26        if r[1]>total: r[1]=total
27        result.append(r)
28    else: result.append([])
29    return result
30
31def htmlStride(state):
32    ''' Given the previous and next list from CalculateStride, and the
33    current state class (which provides a method for the fundamental URL,
34    return a previous next piece of html '''
35    html='Results %s to %s of %s'%(state.offset,state.offset+state.stride-1,state.hits)
36    r=CalculateStride(state.offset,state.stride,state.hits)
37    if r[0]<>[]:
38        s1='Next %s'%r[0][1]
39        nexturl=state.geturl(offset=r[0][0],stride=r[0][1])
41    if r[1]<>[]:
42        l1='Previous %s'%r[1][1]
43        lasturl=state.geturl(offset=r[1][0],stride=r[1][1])
45    html+='.'
46    print state
47    stml='Search for <span class="searchString">%s</span>%s.'%(state.searchString,state.constraints)
48    html='''<div class="stridePosition"><table width="100%%"><tbody>
49            <tr><td align="left" width="%s">%s</td><td align="right">%s</td></tr></tbody></table></div>'''%(WIDTH,stml,html)
50    return html
51
52
54    '''Takes a set of xml DIFS from a discovery search (difSet) and renders a
55    list of responses as a table. The argument is
56            linkto (string) - clicking on the select widget should add the url for
57                              this service to the selected list
58     Note that state is a DiscoveryState instance containing the sessionID from the search,
59     and the offset, stride, and total number of results ...selector is a class for adding
60     particular datasets to a selection dataset (and cookie).
61     '''
62
63    #
64    # List of methods which deal with the actual row content ...
65    #
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'):
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        print 'BBB[',rowlist[2:],']'
75
76        if ll==3:
77            r+='<td align="center" colspan="2" width="12%%">%s</td></tr>'%rowlist[2]
78        elif ll==4:
79            r+='<td align="center">%s</td><td align="center">%s</td></tr>'%tuple(rowlist[2:])
80        else:
81            raise 'Coding error in renderDiscovery'
82        print 'ok'
83        return r
84
85    def spatialBox(elem):
86        return elem.bbox.toHTMLbox()
87
88    def temporalRange(elem):
89        return ''
90
91    def serviceList(d):
93        ndg=0
94        serviceRenderer=renderService(config)
95        if d.binding is not None: ll+=serviceRenderer.get(d.binding)[0]
96        for i in d.services: print i.url
97        for item in d.services:
98            ll+=', '
99            #s=0
100            if item.contentType == 'NDG_A_SERVICE':
101                ndg+=1
102            elif item.contentType == 'NDG_B_SERVICE':
103                ndg+=1
104            print 'starting services',item.url
105            ll+=serviceRenderer.get(item)[0]
106            print 'finishing services',item.url
107        if ndg<2: ndg=0
108        return ndg,ll
109
110
111    # ##################################
112    # Actual html production follows
113    # ##################################
114
115    # previous versions of this code had options, but let's simplify this a lot ...
116
119    columns=['Dataset',spatial,temporal]
120    html='<table><tbody>'+row(columns,rclass="rbgBeige")
121
122    # ok, now let's cycle through the hits
123    i=1
124    for item in difSet:
125        d=item # we used to do the diffing here ... but we don't anymore
126        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
127        i=-1*i
128        if d.tree is None:
129            html+='<tr class="%s"><td colspan="4"> Unparseable record </td></tr>'%bgc
130        else:
131            ndg,slist=serviceList(d)
132            rlist=['',]
133            if d.briefCitation <>'':
134                rlist[0]='%s: %s'%(span('Citation','ndgem'),d.briefCitation)
135            else:
136                rlist[0]='%s: %s'%(span('Title','ndgem'),abbreviate(d.name,60))
137            rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
138            if d.binding is not None: rlist[0]+=' '+hyperlink('(more)',d.binding.url)
139            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.url())
140            rlist[0]+=slist
141            if ndg and selector !=None:
142                rlist[0]+=',&nbsp;'+selector.target(d.entryID,name=d.abbreviation)
143            rlist.append(spatialBox(d))
144            rlist.append(htmlTime(d.timeCoverage[0]))
145            rlist.append(htmlTime(d.timeCoverage[1]))
146            html+=row(rlist,rclass=bgc)
147
148    html+='</tbody></table>'
149    html='<div class="ListOfResults">%s\n%s</div>'%(htmlStride(state),html)
150    return html
151
152import unittest
153
154class TestCase(unittest.TestCase):
155
156    def testStrideself(self):
157        ''' Test striding through a dataset '''
158        res=CalculateStride(1,10,15)
159        self.assertEqual([[11,5],[]],res)
160        res=CalculateStride(11,5,15)
161        self.assertEqual([[],[1,10]],res)
162        res=CalculateStride(11,10,50)
163        self.assertEqual([[21,10],[1,10]],res)
164
165    def testHtmlStride(self):
166        ''' Test the html striding part '''
167        DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg',
168                      'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'}
169        d=DiscoveryState('123','blah',DummyEnviron,12)
170        html=htmlStride(d)
171        print html
172
173if __name__=="__main__":
174    unittest.main()
175
Note: See TracBrowser for help on using the repository browser.