source: TI04-geosplat/trunk/pygsc/CGIErrorHandler.py @ 798

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/pygsc/CGIErrorHandler.py@798
Revision 798, 3.2 KB checked in by astephen, 14 years ago (diff)

Latest working version with install method.
Can accept more than one file but doesn't combine variables yet.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6CGIErrorHandler.py
7==================
8
9Module to hold a suitable classes for displaying errors via the web
10to the user.
11
12"""
13
14# import standard library modules
15import os, sys
16import smtplib
17
18# import local modules
19from clientConfig import *
20from common import *
21
22
23class ErrorHandler:
24    """
25    ErrorHandler class - used to handle errors sensibly by reporting
26    on-screen to the user.
27    """
28   
29    def __init__(self, msg, noheader=0, request_id=None, user=None):
30        """
31        Method to build the instance variables required in the class and
32        calls the appropriate internal methods. Used to handle errors by calling displayError and
33        MailAdministrator class.
34        """
35
36        # First report the error to the user
37        self.displayError(msg, noheader)
38
39        stdout=sys.stdout
40        sys.stdout=RedirectStdout()
41
42        # Mail the administrator if switched on (1)       
43        if ADMIN_NOTIFICATION==1:
44            mailer=MailAdministrator("Error generated by %s: %s, %s" % (PACKAGE_NAME, user, request_id))
45            appendee="""
46        User: %s
47        Request ID: %s
48        """ % (user, request_id)
49            mailer.sendMail(msg+appendee)
50
51        sys.stdout=stdout
52        sys.exit()
53
54   
55    def displayError(self, msg, noheader=0):
56        """
57        displayError method - used to display a clean error to the user via
58        the main dx web interface page. The noheader argument tells the function
59        whether or not to display the full header before the error message.
60        """
61        if noheader==0:
62                print HTTP_HEADER
63                print " ".join(open(os.path.join(BASEDIR, "html", "header.html")).readlines())
64        print "<P>An error has occurred with this application. The error message received is:<P>"
65        print "<P><B>%s</B>" % msg
66        print "<P>The administrator has been informed of this error and will look into it."
67        print """<P>You can try pressing the <I>Back</I> button to return to the previous page.
68        Alternatively you can <A HREF="%s">restart your request</A>.""" % CGI_SCRIPT_URL_PATH
69        print " ".join(open(os.path.join(BASEDIR, "html", "footer.html")).readlines())
70   
71
72class CGIErrorHandler(ErrorHandler):
73    """
74    Class to handle errors when dx is called via a CGI interface.
75    """
76    pass
77
78
79class MailAdministrator:
80    """
81    MailAdministrator class - used to e-mail information to the administrator.
82    """
83   
84    def __init__(self, subject, adminEmail=ADMIN_MAIL_ADDRESS):
85        """
86        __init__ method - takes a subject and administrator's e-mail address and
87        constructs instance variables to be used in the mailing.
88        """
89        self.email=adminEmail
90        self.subject=subject
91        self.mailserver=smtplib.SMTP(LOCAL_MAIL_HOST)
92        self.mailserver.set_debuglevel(1)
93       
94    def sendMail(self, msg):
95        """
96        sendMail method - actually send out the e-mail to the administrator
97        with the message given in the msg argument.
98        """
99        fromaddr=ADMIN_MAIL_ADDRESS
100        mail_content="""To: %s
101From: %s
102Subject: %s
103This message has been automatically generated by the %s\n
104%s""" % (self.email, fromaddr, self.subject, PACKAGE_NAME, msg)
105
106        self.mailserver.sendmail(fromaddr, self.email, mail_content)
107        self.mailserver.quit()
108
109
Note: See TracBrowser for help on using the repository browser.