source: TI07-MOLES/trunk/PythonCode/wsgi/renderDiscoverySet.py @ 2330

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/renderDiscoverySet.py@2330
Revision 2330, 5.4 KB checked in by lawrence, 12 years ago (diff)

Sundry testcases plus a a bug fix for ticket:666 ... it's done, but I
don't like this bugfix because it shows that I don't really understand
unicode properly. Need to revisit unicode again ... later.

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