source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/renderDiscoverySet.py @ 1615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/renderDiscoverySet.py@1615
Revision 1615, 5.7 KB checked in by lawrence, 13 years ago (diff)

Modifications to discovery (and browse) to support ndgDoc as an interim
measure ...

Line 
1from DiscoveryState import DiscoveryState
2from ndgSearch import ndgSearch
3from DIF import DIF
4from htmlUtilities import *
5
6def renderDiscoverySet(difSet,state,summary=0,services=0,spatial=0,temporal=0,
7    linkto='NDG_B_SERVICE',selector=None):
8    '''Takes a set of xml DIFS from a discovery search (difSet) and renders a
9    list of responses as a table, with layout depending on a set of keywords:
10            summary (boolean) - show the abstract,
11            services (boolean) - show the service list,
12            spatial (boolean) - show the bounding box,
13            temporal (boolean) - show the date range,
14            linkto (string) - clicking on the select widget should add the url for
15                              this service to the selected list
16     Note that state is a DiscoveryState instance containing the sessionID from the search,
17     and the offset, stride, and total number of results ...selector is a class for adding
18     particular datasets to a selection dataset (and cookie).
19     '''
20           
21    #
22    # List of methods which deal with the actual row content ...
23    #   
24   
25    def row(rowList,bgcolor='rbgWhite'):
26        h='<tr class="%s">'%bgcolor
27        for item in rowList:
28            if type(item)==type((1,2)):
29                h+='<td colspan="%s">%s</td>'%(item[1],item[0])
30            else:h+='<td>%s</td>'%item
31        h+='</tr>'
32        return h#span(h,bgcolor)
33
34    def spatialBox(elem):
35        return elem.bbox.toHTMLbox()
36     
37    def temporalRange(elem):
38        return ''
39   
40    def serviceList(d):
41        ll=span(' Links: ','ndgem')
42        ndg=0
43        ll+=d.binding.icon()
44        for item in d.services:
45            ll+=', '
46            #s=0
47            if item.serviceType == 'NDG_A_SERVICE': 
48                ll+=item.icon()
49                ndg+=1
50            elif item.serviceType == 'NDG_B_SERVICE':
51                ndg+=1
52                ll+=item.icon()
53            else:
54                ll+=item.icon()
55       
56        if ndg<2: ndg=0
57        return ndg,ll
58
59   
60    # ##################################
61    # Actual html production follows
62    # ##################################
63   
64    # depending on keyword options, provide column headings
65    #
66   
67    columns=['Dataset',]   
68    if spatial: columns.append('Location')
69    if temporal: 
70        #two columns for temporal but put a column header across both columns
71        header='''<table><tbody><tr><td colspan="2" align="center">Time&nbsp;Coverage</td></tr><tr>
72        <td align="center">Start</td><td align="center"> End</td></tr></tbody></table>'''
73        columns.append((header,2))
74    html='<div class="ListOfResults"><table><tbody>'+row(columns,bgcolor="rbgBeige")
75   
76    # ok, now let's cycle through the hits
77    i=1
78    for item in difSet:
79        d=item # we used to do the diffing here ... but we don't anymore
80        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
81        i=-1*i
82       
83        if d.tree is None:
84            html+='<tr class="%s"><td colspan="%s"> Unparseable record </td></tr>'%(bgc,len(columns))
85        else:
86            ndg,slist=serviceList(d)
87            rlist=['',]
88            if summary:
89                if d.briefCitation is not None:
90                    rlist[0]='%s: %s'%(span('Dataset','ndgem'),d.briefCitation)
91                else:
92                    rlist[0]='%s: %s'%(span('Title','ndgem'),abbreviate(d.name,60))
93                rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
94                rlist[0]+=' '+hyperlink('(more)',d.binding.url)
95            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.toHTML())
96            if not summary:
97                rlist[0]+=' '+hyperlink('(more)',d.binding.url)
98            if services:rlist[0]+=slist
99            if ndg and selector !=None: 
100                rlist[0]+=',&nbsp;'+selector.target(d.entryID,name=d.abbreviation)
101            if spatial: rlist.append(spatialBox(d))
102            if temporal: 
103                rlist.append(htmlTime(d.timeCoverage[0]))
104                rlist.append(htmlTime(d.timeCoverage[1]))
105            html+=row(rlist,bgcolor=bgc)
106
107    html+='</tbody></table>'
108   
109    #now work out the links to step through a large result set
110    #html+='<p>Results %s to %s of %s'%(state.offset,min(state.offset+state.stride-1,state.hits),state.hits)
111    html+='<p>Results %s to %s of %s'%(state.offset,state.offset+len(difSet)-1,state.hits)
112    if state.stride < state.hits and state.request is not None:
113        #1,10,21   11,20,21  21,21,21
114        if state.offset+state.stride-1<state.hits:
115            next1=state.offset+state.stride   #11,21
116            nextNum=min(state.stride,1+state.hits-next1) # 10,1
117            s1='Next %s'%nextNum
118            nexturl=state.request.modify({'start':next1,'howmany':nextNum,'searchSession':state.sessID})
119            html+=', %s'%hyperlink(s1,nexturl)
120        if state.offset>1:
121            last1=max(1,state.offset-state.stride)
122            lastNum=min(state.stride,1+state.hits-last1)
123            if lastNum<min(20,state.hits): lastNum=20
124            l1='Last %s'%lastNum
125            lasturl=state.request.modify({'start':last1,'howmany':lastNum,'searchSession':state.sessID})
126            html+=', %s'%hyperlink(l1,lasturl)
127    else:
128        html+='</p>'
129    html+='</div>'
130    return html
131
132if __name__=="__main__":
133
134    ws=ndgSearch()
135    ws.search('acsoe',start=1,howmany=5)
136    hits=ws.hits
137    sessID=ws.serverSessionID
138    results=ws.getAllDocs()
139    difs=[]
140    querystring='dummyUrl'
141    state=DiscoveryState(sessID,querystring,hits,stride=5)
142    print results[0]
143    for result in results:
144        difs.append(DIF(result[1],serviceFile='serviceMap.config'))
145    html=renderDiscoverySet(difs,state,summary=1,spatial=1,temporal=1)
146    f=file('output.html','wb')
147    f.write(html)
148   
Note: See TracBrowser for help on using the repository browser.