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

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

Better error handling, better parameter showing. Shell example
javascript as an initial step to using the Vocabserver.

Line 
1from Utilities import myConfig
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
40        self.pageStart='''<?xml version="1.0" encoding="utf-8"?>
41                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
42                 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
43                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
44                <head>
45                  <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
46                  <title>%s</title>
47                  <link media="all, screen" href="layout/ndg.css" type="text/css" rel="stylesheet"/>
48                  <script type="text/javascript" language="javascript"> %s </script>
49                </head><body><div id="Main">'''%(pageName,ndgJavascript)
50       
51        self.header='''
52        <div id="Header">%s</div>'''% self.__buildHeader(configFile)
53       
54        self.disclaimer='''<p> This portal is a pre-alpha release of NDG software.
55        We expect bugs, and things not to work, so please let us know about such problems
56        (please email badc@rl.ac.uk, and include "NDG problem" in the subject line).
57        </p>'''
58       
59        self.discovery='''
60            <div class="simpleSearch">
61            <form action="%s">
62            <center><table><tbody>
63            <tr><td> New search for text in: <input type="radio" name="textTarget" value="All" checked="checked"/> Everything
64                <input type="radio" name="textTarget" value="Authors"/> Authors
65                <input type="radio" name="textTarget" value="Params"/>&nbsp;&nbsp;Parameters
66                (<a href="%s">Advanced Search</a>,&nbsp;<a href="%s">Help</a>)</td></tr>
67            <tr><td><input type="text" size="66" name="searchString"/> <input type="submit"></input></td></tr>
68            </tbody></table></center>
69            <input type="hidden" value="0" name="advanced"/>
70            </form></div>'''%(discoveryURL,advancedURL,helpAddress)
71
72    def __call__(self,environ,start_response):
73        ''' Let's prepend and postpend some stuff'''
74        if ('type','xml') in parse_querystring(environ):
75            response='''<?xml version="1.0" encoding="utf-8"?>'''
76            footer=0
77        else:
78            footer=1
79            response=self.pageStart+self.header
80        if environ['SCRIPT_NAME']=='/discovery':
81            insert=self.disclaimer
82        else:
83            insert=self.discovery+self.disclaimer
84        self.footer='''<div id="Footer">%s</div></div></body></html>'''%insert
85        try:
86            if debug:
87                for i in self.wrapTarget(environ,start_response): response+=i
88            else:
89                for i in self.wrapTarget(environ,start_response): response+=str(i)
90        except UnicodeEncodeError:
91            response+='''<p> Problems with data content, records untranslatable! </p>
92            <p>Please eport this error to the badc (badc@rl.ac.uk) with "Discovery UNICODE
93            error in the subject line" and cut and paste the url <i>to this message</i>
94            in the message body</p>'''
95        if footer: response+=self.footer
96        #currently need to coerce response into a string for flup ...
97        return [response]
98       
99    def __buildHeader(self,configFile):
100       
101        if configFile is None: return ''
102        config=myConfig(configFile)
103        localLink=config.get('layout','localLink',None)
104        localImage=config.get('layout','localImage',None)
105        localAlt=config.get('layout','localAlt','Visit Local Site')
106        ndgLink=config.get('layout','ndgLink','http://ndg.nerc.ac.uk')
107        ndgImage=config.get('layout','ndgImage',None)
108        ndgAlt=config.get('layout','ndgAlt','Visit NDG')
109        PageLogo=config.get('layout','pageLogo',None)
110       
111        banner='''   
112            <table cellpadding="0" cellspacing="0" width="100%%">
113            <tbody><tr>
114            <td width="10%%" align="center" valign="middle">
115              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
116            <td width="80%%" align="center" valign="middle" >
117              Natural Environment Research Council<br/>
118              Data Discovery Service</td>
119            <td width="10%%" align="center" valign="middle">
120              <a href="%s"> <img src="%s" alt="%s"/> </a> </td>
121            </tr>
122            </tbody></table>'''%(ndgLink,ndgImage,ndgAlt,localLink,localImage,localAlt)
123       
124        return banner
Note: See TracBrowser for help on using the repository browser.