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

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

Sundry minor improvements in layout etc.

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