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

Revision 2344, 8.6 kB (checked in by jerome, 19 years ago)

Moved the GPL blurb into a single location.
Now uses named parameters in commands' help.

  • 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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    <p>
47      <form action="dumpykota.cgi" method="GET">
48        <table>
49          <tr>
50            <td>
51              <p>
52                <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a>
53                <br />
54                <a href="%s">PyKota v%s</a>
55              </p>
56            </td>
57            <td colspan="2">
58              <h1>%s</h1>
59            </td>
60          </tr>
61          <tr>
62            <td colspan="3" align="center">
63              <input type="submit" name="report" value="%s" />
64            </td>
65          </tr>
66        </table>
67        <p>
68          %s
69        </p>"""
70   
71footer = """
72        <table>
73          <tr>
74            <td colspan="3" align="center">
75              <input type="submit" name="report" value="%s" />
76            </td>
77          </tr>
78        </table> 
79      </form>
80    </p> 
81    <hr width="25%%" />
82    <p>
83      <font size="-2">
84        <a href="http://www.librelogiciel.com/software/">%s</a>
85        &copy; %s %s
86      </font>
87    </p>
88  </body>
89</html>""" 
90
91class PyKotaDumperGUI(DumPyKota) :
92    """PyKota Dumper GUI"""
93    def guiDisplay(self) :
94        """Displays the dumper interface."""
95        global header, footer
96        print header % (self.getCharset(), _("PyKota Data Dumper"), \
97                        self.config.getLogoLink(), \
98                        self.config.getLogoURL(), version.__version__, \
99                        self.config.getLogoLink(), \
100                        version.__version__, _("PyKota Data Dumper"), \
101                        _("Dump"), _("Please click on the above button"))
102        print self.htmlListDataTypes(self.options.get("data", ""))
103        print "<br />"
104        print self.htmlListFormats(self.options.get("format", ""))
105        print "<br />"
106        print self.htmlFilterInput(" ".join(self.arguments))
107        print "<br />"
108        print self.htmlSumCheckbox(self.options.get("sum", ""))
109        print footer % (_("Dump"), version.__doc__, version.__years__, version.__author__)
110       
111    def htmlListDataTypes(self, selected="") :   
112        """Displays the datatype selection list."""
113        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
114        for dt in self.validdatatypes.items() :
115            if dt[0] == selected :
116                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
117            else :
118                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
119        message += '</select></td></tr></table>'
120        return message
121       
122    def htmlListFormats(self, selected="") :   
123        """Displays the formats selection list."""
124        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
125        for fmt in self.validformats.items() :
126            if fmt[0] == selected :
127                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
128            else :
129                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
130        message += '</select></td></tr></table>'
131        return message
132       
133    def htmlFilterInput(self, value="") :   
134        """Input the optional dump filter."""
135        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or ""))
136       
137    def htmlSumCheckbox(self, checked="") :   
138        """Input the optional Sum option."""
139        return _("Summarize") + (' : <input type="checkbox" name="sum" %s /> <em>%s</em>' % ((checked and 'checked="checked"'), _("only for payments or history")))
140       
141    def guiAction(self) :
142        """Main function"""
143        try :
144            wantreport = self.form.has_key("report")
145        except TypeError :   
146            pass # WebDAV request probably, seen when trying to open a csv file in OOo
147        else :   
148            if wantreport :
149                try :
150                    if self.form.has_key("datatype") :
151                        self.options["data"] = self.form["datatype"].value
152                    if self.form.has_key("format") :
153                        self.options["format"] = self.form["format"].value
154                    if self.form.has_key("filter") :   
155                        self.arguments = self.form["filter"].value.split()
156                    if self.form.has_key("sum") :   
157                        self.options["sum"] = self.form["sum"].value
158                    # when no authentication is done, or when the remote username   
159                    # is 'root' (even if not run as root of course), then unrestricted
160                    # dump is allowed.
161                    remuser = os.environ.get("REMOTE_USER", "root")   
162                    # special hack to accomodate mod_auth_ldap Apache module
163                    try :
164                        remuser = remuser.split("=")[1].split(",")[0]
165                    except IndexError :   
166                        pass
167                    if remuser != "root" :
168                        # non-'root' users when the script is password protected
169                        # can not dump any data as they like, we restrict them
170                        # to their own datas.
171                        if self.options["data"] not in ["printers", "pmembers", "groups", "gpquotas"] :
172                            self.arguments.append("username=%s" % remuser)
173                       
174                    if self.options["format"] in ("csv", "ssv") :
175                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
176                        ctype = "text/comma-separated-values"
177                        fname = "dump.csv"
178                    elif self.options["format"] == "tsv" :
179                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
180                        ctype = "text/tab-separated-values"
181                        fname = "dump.tsv"
182                    elif self.options["format"] == "xml" :
183                        ctype = "text/xml"
184                        fname = "dump.xml"
185                    elif self.options["format"] == "cups" :
186                        ctype = "text/plain"
187                        fname = "page_log"
188                    print "Content-type: %s" % ctype   
189                    print "Content-disposition: attachment; filename=%s" % fname
190                    print
191                    self.main(self.arguments, self.options, restricted=0)
192                except :
193                    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 />")
194            else :       
195                self.guiDisplay()
196           
197if __name__ == "__main__" :
198    os.environ["LC_ALL"] = getLanguagePreference()
199    admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
200    admin.deferredInit()
201    admin.form = cgi.FieldStorage()
202    admin.options = { "output" : "-",
203                "data" : "history",
204                "format" : "cups",
205                "sum" : None,
206              }
207    admin.arguments = []
208    admin.guiAction()
209    try :
210        admin.storage.close()
211    except (TypeError, NameError, AttributeError) :   
212        pass
213       
214    sys.exit(0)
Note: See TracBrowser for help on using the browser.