source: TI07-MOLES/trunk/PythonCode/wsgi/ndgPageLayout.py @ 2417

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

Handling empty documents and errors from DIF loading better

Line 
1from Utilities import myConfig,mailError
2from paste.request import parse_querystring
3
4debug=1
5def DiscoveryFrameFactory(global_config,**local_conf):
6   
7    ''' This factory is intended to be used by paste-deploy to return
8    the discovery FRAME wsgi MIDDLEWARE application, configured to use the configDir
9    to find local configuration information '''
10   
11    if 'configDir' in local_conf.keys():
12        configDir=local_conf[configDir]
13    else:
14        try:
15            configDir=global_config['configDir']
16        except:
17            configDir=None
18           
19    configFile=configDir+'ndgDiscovery.config'
20   
21    def filter(app):
22        return DiscoveryFrame(app,'NERC DataGrid: Data Portal',configFile)
23   
24    return filter
25
26class DiscoveryFrame(object):
27     
28    def __init__(self, wrapTarget,pageName,configFile=None):
29        ''' This is simple WSGI middleware to provide a page wrapper, will eventually
30        include cookie handling for grabbing a "cart" of datasets '''
31       
32        self.wrapTarget=wrapTarget
33        self.config=myConfig(configFile)
34       
35        helpAddress=self.config.get('SEARCH','helpURL')
36        discoveryURL=self.config.get('SEARCH','discoveryURL')
37        advancedURL=self.config.get('SEARCH','advancedURL')
38        ndgJavascript=self.config.get('layout','ndgJavascript')
39        self.mailServer=self.config.get('DEFAULT','mailServer','outbox.rl.ac.uk')
40
41        self.pageStart='''<?xml version="1.0" encoding="utf-8"?>
42                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
43                 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
44                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
45                <head>
46                  <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
47                  <title>%s</title>
48                  <link media="all, screen" href="layout/ndg.css" type="text/css" rel="stylesheet"/>
49                  <script type="text/javascript" language="javascript"> %s </script>
50                </head><body><div id="Main">'''%(pageName,ndgJavascript)
51       
52        self.header='''
53        <div id="Header">%s</div>'''% self.__buildHeader(configFile)
54       
55        self.disclaimer='''<p> This portal is a pre-alpha release of NDG software.
56        We expect bugs, and things not to work, so please let us know about such problems
57        (please email badc@rl.ac.uk, and include "NDG problem" in the subject line).
58        </p>'''
59       
60        self.discovery='''
61            <div class="simpleSearch">
62            <form action="%s">
63            <center><table><tbody>
64            <tr><td> New search for text in: <input type="radio" name="textTarget" value="All" checked="checked"/> Everything
65                <input type="radio" name="textTarget" value="Authors"/> Authors
66                <input type="radio" name="textTarget" value="Params"/>&nbsp;&nbsp;Parameters
67                (<a href="%s">Advanced Search</a>,&nbsp;<a href="%s">Help</a>)</td></tr>
68            <tr><td><input type="text" size="66" name="searchString"/> <input type="submit"></input></td></tr>
69            </tbody></table></center>
70            <input type="hidden" value="0" name="advanced"/>
71            </form></div>'''%(discoveryURL,advancedURL,helpAddress)
72
73    def __call__(self,environ,start_response):
74        ''' Let's prepend and postpend some stuff'''
75        if ('type','xml') in parse_querystring(environ):
76            response='''<?xml version="1.0" encoding="utf-8"?>'''
77            footer=0
78        else:
79            footer=1
80            response=self.pageStart+self.header
81        if environ['SCRIPT_NAME']=='/discovery':
82            insert=self.disclaimer
83        else:
84            insert=self.discovery+self.disclaimer
85        self.footer='''<div id="Footer">%s</div></div></body></html>'''%insert
86        try:
87            if debug:
88                for i in self.wrapTarget(environ,start_response): response+=i
89            else:
90                for i in self.wrapTarget(environ,start_response): response+=str(i)
91            failure=0
92        except Exception,e:
93            response+='''<p> Unable to fufill request, probably an error in the metadata record.
94            NDG staff have been advised. Please try another search. </p>'''
95            body='Error [%s]\n'%e
96            body+=str(environ)
97            mailError('b.n.lawrence@rl.ac.uk','Discovery/Browse Error',body,server=self.mailServer)
98            failure=1
99        if footer: response+=self.footer
100        if failure: start_response('500 OK', [('Content-Type', 'text/html'),('charset','utf-8')])
101        #currently need to coerce response into a string for flup ...
102        return [response]
103       
104    def __buildHeader(self,configFile):
105       
106        if configFile is None: return ''
107        config=myConfig(configFile)
108        localLink=config.get('layout','localLink',None)
109        localImage=config.get('layout','localImage',None)
110        localAlt=config.get('layout','localAlt','Visit Local Site')
111        ndgLink=config.get('layout','ndgLink','http://ndg.nerc.ac.uk')
112        ndgImage=config.get('layout','ndgImage',None)
113        ndgAlt=config.get('layout','ndgAlt','Visit NDG')
114        PageLogo=config.get('layout','pageLogo',None)
115       
116        banner='''   
117            <table cellpadding="0" cellspacing="0" width="100%%">
118            <tbody><tr>
119            <td width="10%%" align="center" valign="middle">
120              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
121            <td width="80%%" align="center" valign="middle" >
122              Natural Environment Research Council<br/>
123              Data Discovery Service</td>
124            <td width="10%%" align="center" valign="middle">
125              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
126            </tr>
127            </tbody></table>'''%(ndgLink,ndgImage,ndgAlt,localLink,localImage,localAlt)
128       
129        return banner
Note: See TracBrowser for help on using the repository browser.