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

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

Better handling of request URLs for both discovery and browse

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'):
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=[abbreviate(d.name,60),]
88            if summary:
89                rlist[0]='%s: %s'%(span('Name','ndgem'),rlist[0])
90                rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200))
91                rlist[0]+=' '+hyperlink('(more)',d.binding.url)
92            rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.toHTML())
93            if not summary:
94                rlist[0]+=' '+hyperlink('(more)',d.binding.url)
95            if services:rlist[0]+=slist
96            if ndg and selector !=None: 
97                rlist[0]+=',&nbsp;'+selector.target(d.entryID,name=d.abbreviation)
98            if spatial: rlist.append(spatialBox(d))
99            if temporal: 
100                rlist.append(htmlTime(d.timeCoverage[0]))
101                rlist.append(htmlTime(d.timeCoverage[1]))
102            html+=row(rlist,bgcolor=bgc)
103
104    html+='</tbody></table>'
105   
106    #now work out the links to step through a large result set
107    #html+='<p>Results %s to %s of %s'%(state.offset,min(state.offset+state.stride-1,state.hits),state.hits)
108    html+='<p>Results %s to %s of %s'%(state.offset,state.offset+len(difSet)-1,state.hits)
109    if state.stride < state.hits and state.request is not None:
110        #1,10,21   11,20,21  21,21,21
111        if state.offset+state.stride-1<state.hits:
112            next1=state.offset+state.stride   #11,21
113            nextNum=min(state.stride,1+state.hits-next1) # 10,1
114            s1='Next %s'%nextNum
115            nexturl=state.request.modify({'start':next1,'howmany':nextNum,'searchSession':state.sessID})
116            html+=', %s'%hyperlink(s1,nexturl)
117        if state.offset>1:
118            last1=max(1,state.offset-state.stride)
119            lastNum=min(state.stride,1+state.hits-last1)
120            if lastNum<min(20,state.hits): lastNum=20
121            l1='Last %s'%lastNum
122            lasturl=state.request.modify({'start':last1,'howmany':lastNum,'searchSession':state.sessID})
123            html+=', %s'%hyperlink(l1,lasturl)
124    else:
125        html+='</p>'
126    html+='</div>'
127    return html
128
129if __name__=="__main__":
130
131    ws=ndgSearch()
132    ws.search('acsoe',start=1,howmany=5)
133    hits=ws.hits
134    sessID=ws.serverSessionID
135    results=ws.getAllDocs()
136    difs=[]
137    querystring='dummyUrl'
138    state=DiscoveryState(sessID,querystring,hits,stride=5)
139    print results[0]
140    for result in results:
141        difs.append(DIF(result[1],serviceFile='serviceMap.config'))
142    html=renderDiscoverySet(difs,state,summary=1,spatial=1,temporal=1)
143    f=file('output.html','wb')
144    f.write(html)
145   
Note: See TracBrowser for help on using the repository browser.