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

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

and another error in error handling (can only test this live folks,
sorry)

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        except Exception,e:
92            response+='''<p> Unable to fufill request, probably an error in the metadata record.
93            NDG staff have been advised. Please try another search. </p>'''
94            body='Error [%s]\n'%e
95            body+=str(environ)
96            mailError('b.n.lawrence@rl.ac.uk','Discovery/Browse Error',body,server=self.mailServer)
97            start_response('500 OK', [('Content-Type', 'text/html'),('charset','utf-8')])
98        if footer: response+=self.footer
99        #currently need to coerce response into a string for flup ...
100        return [response]
101       
102    def __buildHeader(self,configFile):
103       
104        if configFile is None: return ''
105        config=myConfig(configFile)
106        localLink=config.get('layout','localLink',None)
107        localImage=config.get('layout','localImage',None)
108        localAlt=config.get('layout','localAlt','Visit Local Site')
109        ndgLink=config.get('layout','ndgLink','http://ndg.nerc.ac.uk')
110        ndgImage=config.get('layout','ndgImage',None)
111        ndgAlt=config.get('layout','ndgAlt','Visit NDG')
112        PageLogo=config.get('layout','pageLogo',None)
113       
114        banner='''   
115            <table cellpadding="0" cellspacing="0" width="100%%">
116            <tbody><tr>
117            <td width="10%%" align="center" valign="middle">
118              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
119            <td width="80%%" align="center" valign="middle" >
120              Natural Environment Research Council<br/>
121              Data Discovery Service</td>
122            <td width="10%%" align="center" valign="middle">
123              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
124            </tr>
125            </tbody></table>'''%(ndgLink,ndgImage,ndgAlt,localLink,localImage,localAlt)
126       
127        return banner
Note: See TracBrowser for help on using the repository browser.