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

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

This has the code to support a temporary patch for
the unicode and orphan html characters problems. Further
details are in ticket:311

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')
41        ndg=0
42        s=1
43        for item in d.services:
44            if not s: ll+=', '
45            s=0
46            if item == 'NDG_A_SERVICE': 
47                ll+=hyperlink('data',d.services[item].url)
48                ndg=1
49            elif item == 'NDG_B_SERVICE':
50                ll+=hyperlink('metadata',d.services[item].url)
51            else:
52                ll+=hyperlink(item,d.services[item].url)
53        return ndg,ll
54
55   
56    # ##################################
57    # Actual html production follows
58    # ##################################
59   
60    # depending on keyword options, provide column headings
61    #
62   
63    columns=['Dataset',]   
64    if spatial: columns.append('Location')
65    if temporal: 
66        #two columns for temporal but put a column header across both columns
67        header='''<table><tbody><tr><td colspan="2" align="center">Time&nbsp;Coverage</td></tr><tr>
68        <td align="center">Start</td><td align="center"> End</td></tr></tbody></table>'''
69        columns.append((header,2))
70    html='<div class="ListOfResults"><table><tbody>'+row(columns,bgcolor="rbgBeige")
71   
72    # ok, now let's cycle through the hits
73    i=1
74    for item in difSet:
75        d=DIF(item)
76        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
77        i=-1*i
78       
79        if d.elem is None:
80            html+='<tr class="%s"><td colspan="%s"> Unparseable record </td></tr>'%(bgc,len(columns))
81        else:
82            ndg,slist=serviceList(d)
83            rlist=[abbreviate(d.name,60),]
84            if summary:
85                rlist[0]='%s: %s'%(span('Name','ndgem'),rlist[0])
86                rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
87                rlist[0]+=' '+hyperlink('(more)',d.entryID)
88            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.toHTML())
89            if not summary:
90                rlist[0]+=' '+hyperlink('(more)',d.entryID)
91            if services:rlist[0]+=slist
92            if ndg and selector !=None: rlist[0]+=',&nbsp;'+selector.target(d.entryID)
93            if spatial: rlist.append(spatialBox(d))
94            if temporal: 
95                rlist.append(htmlTime(d.timeCoverage[0]))
96                rlist.append(htmlTime(d.timeCoverage[1]))
97            html+=row(rlist,bgcolor=bgc)
98
99    html+='</tbody></table>'
100   
101    #now work out the links to step through a large result set
102    html+='<p>Results %s to %s of %s'%(state.offset,min(state.offset+state.stride-1,state.hits),state.hits)
103    if state.stride < state.hits and selector is not None:
104        if state.offset+state.stride-1<state.hits:
105            next1=state.offset+state.stride
106            nextNum=min(next1+state.stride,state.hits)-next1
107            s1='Next %s'%nextNum
108            nexturl='%s&start=%s&howmany=%s'%(selector.baseURL,next1,nextNum)
109            html+=', %s'%hyperlink(s1,nexturl)
110        if state.offset>1:
111            last1=state.offset-state.stride
112            lastNum=state.stride
113            l1='Last %s'%lastNum
114            lasturl='%s&start=%s&howmany=%s'%(selector.baseURL,last1,lastNum)
115            html+=', %s'%hyperlink(l1,lasturl)
116    else:
117        html+='</p>'
118    html+='</div>'
119    return html
120
121if __name__=="__main__":
122   
123    from ETxmlView import xmlCleanup
124    ws=DiscoveryWS()
125    r=ws.SearchFullText('acsoe')
126    hits,time=r['hits'],r['queryTime']
127    results=ws.GetResults(offset=1,number=5)
128    difs=[]
129    querystring='dummyUrl'
130    state=Discovery(ws.sessionID,queryString,hits,stride=5)
131    #g=file('log.xml','w')
132    for result in results:
133        #g.write(xmlCleanup(result)+'\n')
134        difs.append(result)
135    html=renderDiscoverySet(difs,state,summary=1,spatial=1,temporal=1)
136    f=file('output.html','wb')
137    f.write(html)
138    ws.release()
Note: See TracBrowser for help on using the repository browser.