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

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

modified page layout to report unicode errors (temporary expedient)

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