# source:TI07-MOLES/trunk/PythonCode/wsgi/renderDiscoverySet.py@2609

Revision 2609, 7.7 KB checked in by lawrence, 13 years ago (diff)

First changes (in standard wsgi branch) for supporting shopping cart
functionality etc.

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