root / pykota / trunk / cgi-bin / dumpykota.cgi @ 2232

Revision 2229, 7.7 kB (checked in by jerome, 20 years ago)

Improved stability and allowed tracebacks in CGI scripts.
Improved pykotme.cgi to display the cost of print jobs when the user is logged-in.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Reports generator
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import cgi
30import urllib
31
32from pykota import version
33from pykota.tool import PyKotaToolError
34from pykota.dumper import DumPyKota
35from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
36
37header = """Content-type: text/html
38
39<?xml version="1.0" encoding="%s"?>
40<html>
41  <head>
42    <title>%s</title>
43    <link rel="stylesheet" type="text/css" href="/pykota.css" />
44  </head>
45  <body>
46    <form action="dumpykota.cgi" method="GET">
47      <table>
48        <tr>
49          <td>
50            <p>
51              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png?version=%s" alt="PyKota's Logo" /></a>
52              <br />
53              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
54            </p>
55          </td>
56          <td colspan="2">
57            <h1>%s</h1>
58          </td>
59        </tr>
60        <tr>
61          <td colspan="3" align="center">
62            <input type="submit" name="report" value="%s" />
63          </td>
64        </tr>
65      </table>
66      <p>
67        %s
68      </p>"""
69   
70footer = """
71      <table>
72        <tr>
73          <td colspan="3" align="center">
74            <input type="submit" name="report" value="%s" />
75          </td>
76        </tr>
77      </table> 
78    </form>
79  </body>
80</html>""" 
81
82class PyKotaDumperGUI(DumPyKota) :
83    """PyKota Dumper GUI"""
84    def guiDisplay(self) :
85        """Displays the dumper interface."""
86        global header, footer
87        print header % (self.getCharset(), _("PyKota Data Dumper"), version.__version__, version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button"))
88        print self.htmlListDataTypes(self.options.get("data", ""))
89        print "<br />"
90        print self.htmlListFormats(self.options.get("format", ""))
91        print "<br />"
92        print self.htmlFilterInput(" ".join(self.arguments))
93        print footer % _("Dump")
94       
95    def htmlListDataTypes(self, selected="") :   
96        """Displays the datatype selection list."""
97        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
98        for dt in self.validdatatypes.items() :
99            if dt[0] == selected :
100                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
101            else :
102                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
103        message += '</select></td></tr></table>'
104        return message
105       
106    def htmlListFormats(self, selected="") :   
107        """Displays the formats selection list."""
108        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
109        for fmt in self.validformats.items() :
110            if fmt[0] == selected :
111                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
112            else :
113                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
114        message += '</select></td></tr></table>'
115        return message
116       
117    def htmlFilterInput(self, value="") :   
118        """Input the optional dump filter."""
119        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or ""))
120       
121    def guiAction(self) :
122        """Main function"""
123        try :
124            wantreport = self.form.has_key("report")
125        except TypeError :   
126            pass # WebDAV request probably, seen when trying to open a csv file in OOo
127        else :   
128            if wantreport :
129                try :
130                    if self.form.has_key("datatype") :
131                        self.options["data"] = self.form["datatype"].value
132                    if self.form.has_key("format") :
133                        self.options["format"] = self.form["format"].value
134                    if self.form.has_key("filter") :   
135                        self.arguments = self.form["filter"].value.split()
136                       
137                    # when no authentication is done, or when the remote username   
138                    # is 'root' (even if not run as root of course), then unrestricted
139                    # dump is allowed.
140                    remuser = os.environ.get("REMOTE_USER", "root")   
141                    # special hack to accomodate mod_auth_ldap Apache module
142                    try :
143                        remuser = remuser.split("=")[1].split(",")[0]
144                    except IndexError :   
145                        pass
146                    if remuser != "root" :
147                        # non-'root' users when the script is password protected
148                        # can not dump any data as they like, we restrict them
149                        # to their own datas.
150                        if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] :
151                            self.arguments.append("username=%s" % remuser)
152                       
153                    if self.options["format"] in ("csv", "ssv") :
154                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
155                        ctype = "text/comma-separated-values"
156                        fname = "dump.csv"
157                    elif self.options["format"] == "tsv" :
158                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
159                        ctype = "text/tab-separated-values"
160                        fname = "dump.tsv"
161                    elif self.options["format"] == "xml" :
162                        ctype = "text/xml"
163                        fname = "dump.xml"
164                    elif self.options["format"] == "cups" :
165                        ctype = "text/plain"
166                        fname = "page_log"
167                    print "Content-type: %s" % ctype   
168                    print "Content-disposition: attachment; filename=%s" % fname
169                    print
170                    self.main(self.arguments, self.options, restricted=0)
171                except :
172                    print 'Content-type: text/html\n\n<html><head><title>CGI Error</title></head><body><p><font color="red">%s</font></p></body></html>' % self.crashed("CGI Error").replace("\n", "<br />")
173            else :       
174                self.guiDisplay()
175           
176if __name__ == "__main__" :
177    os.environ["LC_ALL"] = getLanguagePreference()
178    admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
179    admin.deferredInit()
180    admin.form = cgi.FieldStorage()
181    admin.options = { "output" : "-",
182                "data" : "history",
183                "format" : "cups",
184              }
185    admin.arguments = []
186    admin.guiAction()
187    try :
188        admin.storage.close()
189    except (TypeError, NameError, AttributeError) :   
190        pass
191       
192    sys.exit(0)
Note: See TracBrowser for help on using the browser.