source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/renderDiscoverySet.py @ 1020

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/renderDiscoverySet.py@1020
Revision 1020, 4.3 KB checked in by lawrence, 14 years ago (diff)

More discovery (from browse) related improvments

Line 
1from DiscoveryWS import DiscoveryWS,DiscoveryState
2from DIF import DIF
3from htmlUtilities import *
4
5def renderDiscoverySet(difSet,state,selector=None,summary=0,services=0,spatial=0,temporal=0,
6    linkto='NDG_B_SERVICE'):
7    '''Takes a set of xml DIFS from a discovery search (difSet) and renders a
8    list of responses as a table, with layout depending on a set of keywords:
9            summary (boolean) - show the abstract,
10            services (boolean) - show the service list,
11            spatial (boolean) - show the bounding box,
12            temporal (boolean) - show the date range,
13            linkto (string) - clicking on the select widget should add the url for
14                              this service to the selected list
15     Note that state is a DiscoveryState instance containing the sessionID from the search,
16     and the offset, stride, and total number of results ...selector is a class for adding
17     particular datasets to a selection dataset (and cookie).
18     '''
19           
20    #
21    # List of methods which deal with the actual row content ...
22    #   
23   
24    def row(rowList,bgcolor='rbgWhite'):
25        h='<tr class="%s">'%bgcolor
26        for item in rowList:
27            if type(item)==type((1,2)):
28                h+='<td colspan="%s">%s</td>'%(item[1],item[0])
29            else:h+='<td>%s</td>'%item
30        h+='</tr>'
31        return h#span(h,bgcolor)
32
33    def spatialBox(elem):
34        return elem.bbox.toHTMLbox()
35     
36    def temporalRange(elem):
37        return ''
38   
39    def serviceList(d):
40        ll=span(' Links: ','ndgem')+hyperlink('Full Record','url')
41        ndg=0
42        for item in d.services:
43            ll+=', '
44            if item == 'NDG_A_SERVICE': 
45                ll+=hyperlink('data',d.services[item].url)
46                ndg=1
47            elif item == 'NDG_B_SERVICE':
48                ll+=hyperlink('metadata',d.services[item].url)
49            else:
50                ll+=hyperlink(item,d.services[item].url)
51        return ndg,ll
52
53   
54    # ##################################
55    # Actual html production follows
56    # ##################################
57   
58    # depending on keyword options, provide column headings
59    #
60   
61    columns=['Dataset',]   
62    if spatial: columns.append('Location')
63    if temporal: 
64        #two columns for temporal but put a column header across both columns
65        header='''<table><tbody><tr><td colspan="2" align="center">Time&nbsp;Coverage</td></tr><tr>
66        <td align="center">Start</td><td align="center"> End</td></tr></tbody></table>'''
67        columns.append((header,2))
68    html='<div class="ListOfResults"><table><tbody>'+row(columns,bgcolor="rbgBeige")
69   
70    # ok, now let's cycle through the hits
71    i=1
72    for item in difSet:
73        d=DIF(item)
74        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
75        i=-1*i
76        ndg,slist=serviceList(d)
77        if d.elem is None:
78            html+='<tr class="%s"><td colspan="%s"> Unparseable record </td></tr>'%(bgc,len(columns))
79        else:
80            rlist=[abbreviate(d.name,60),]
81            if summary:
82                rlist[0]='%s: %s'%(span('Name','ndgem'),rlist[0])
83                rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
84            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.toHTML())
85            if services:rlist[0]+=slist
86            rlist[0]+=',&nbsp;'+selector.target(d.entryID)
87            if spatial: rlist.append(spatialBox(d))
88            if temporal: 
89                rlist.append(htmlTime(d.timeCoverage[0]))
90                rlist.append(htmlTime(d.timeCoverage[1]))
91            html+=row(rlist,bgcolor=bgc)
92
93    html+='</tbody></table>'
94    html+='<p>Results %s to %s of %s</p>'%(state.offset,state.offset+state.stride-1,state.hits)
95    html+='</div>'
96    return html
97
98if __name__=="__main__":
99   
100    from ETxmlView import xmlCleanup
101    ws=DiscoveryWS()
102    r=ws.SearchFullText('acsoe')
103    hits,time=r['hits'],r['queryTime']
104    results=ws.GetResults(offset=1,number=5)
105    difs=[]
106    querystring='dummyUrl'
107    state=Discovery(ws.sessionID,queryString,hits,stride=5)
108    #g=file('log.xml','w')
109    for result in results:
110        #g.write(xmlCleanup(result)+'\n')
111        difs.append(xmlCleanup(result))
112    html=renderDiscoverySet(difs,state,summary=1,spatial=1,temporal=1)
113    f=file('output.html','wb')
114    f.write(html)
115    ws.release()
Note: See TracBrowser for help on using the repository browser.