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

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

More discovery/browse convergence, starting support
for linking to services ...

Line 
1from DiscoveryWS import DiscoveryWS
2from DIF import DIF
3
4def renderDiscoverySet(difSet,summary=0,services=0,spatial=0,temporal=0,
5    linkto='NDG_B_SERVICE'):
6    '''Takes a set of xml DIFS from a discovery search and renders a list of responses as a table,
7    with layout depending on a set of keywords:
8            summary (boolean) - show the abstract,
9            services (boolean) - show the service list,
10            spatial (boolean) - show the bounding box,
11            temporal (boolean) - show the date range,
12            linkto (string) - clicking on the name should go to a service binding to this url
13            '''
14    #
15    # List of methods which deal with the actual row content ...
16    #   
17    def span(x,id):
18        return '<span class="%s">%s</span>'%(id,x)
19   
20    def hyperlink(n,u):
21        return '<a href="%s">%s</a>'%(u,n)
22   
23    def row(rowList,bgcolor='rbgWhite'):
24        h='<tr class="%s">'%bgcolor
25        for item in rowList:
26            if type(item)==type((1,2)):
27                h+='<td colspan="%s">%s</td>'%(item[1],item[0])
28            else:h+='<td>%s</td>'%item
29        h+='</tr>'
30        return h#span(h,bgcolor)
31
32    def spatialBox(elem):
33        return elem.bbox.toHTMLbox()
34     
35    def temporalRange(elem):
36        return ''
37   
38    def serviceList(d):
39        ll='<br/>Services: '+hyperlink('Full Record','url')
40        for item in d.services:
41            ll+=', '
42            if item == 'NDG_A_SERVICE': 
43                ll+=hyperlink('data',d.services[item].url)
44            elif item == 'NDG_B_SERVICE':
45                ll+=hyperlink('metadata',d.services[item].url)
46            else:
47                ll+=hyperlink(item,d.services[item].url)
48        return ll
49   
50    def abbreviate(string,newlen):
51        ''' We want to ensure that a particular block of text does not exceed
52        a specific length - and finishes on a word boundary. '''
53        if len(string)< newlen:
54            return string
55        else:
56            r=''
57            for w in string[0:newlen].split(' ')[:-1]:r+='%s '%w
58            r+=' ...'
59            return r
60   
61    # ##################################
62    # Actual html production follows
63    # ##################################
64   
65    # depending on keyword options, provide column headings
66    #
67    columns=['Dataset','Repository']   
68    if spatial: columns.insert(-1,'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">Temporal Coverage</td></tr><tr>
72        <td align="center">Start Date</td><td align="center"> End Date</td></tr></tbody></table>'''
73        columns.insert(-1,(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=DIF(item)
80        bgc={1:'rbgWhite',-1:'rbgGrey'}[i]
81        i=-1*i
82        rlist=[abbreviate(d.name,60),d.centre.toHTML()]
83        if summary:
84            rlist[0]='Name: %s'%rlist[0]
85            rlist[0]+='<br/>Summary: %s'%abbreviate(d.abstract,200)
86        if services:
87            rlist[0]+=serviceList(d)
88        if spatial: rlist.insert(-1,spatialBox(d))
89        if temporal: 
90            rlist.insert(-1,d.timeCoverage[0])
91            rlist.insert(-1,d.timeCoverage[1])
92       
93       
94        html+=row(rlist,bgcolor=bgc)
95
96    html+='</tbody></table></div>'
97    return html
98
99if __name__=="__main__":
100   
101    from ETxmlView import xmlCleanup
102    ws=DiscoveryWS()
103    ws.SearchFullText('acsoe')
104    results=ws.GetResults(number=5)
105    difs=[]
106    #g=file('log.xml','w')
107    for result in results:
108        #g.write(xmlCleanup(result)+'\n')
109        difs.append(xmlCleanup(result))
110    html=renderDiscoverySet(difs,summary=1,spatial=1,temporal=1)
111    f=file('output.html','wb')
112    f.write(html)
113    ws.release()
Note: See TracBrowser for help on using the repository browser.