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

Revision 3481, 10.1 kB (checked in by jerome, 15 years ago)

Changed copyright years.
Copyright years are now dynamic when displayed by a command line tool.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2019]1#! /usr/bin/python
[3411]2# -*- coding: utf-8 -*-*-
[2019]3
4# PyKota Print Quota Reports generator
5#
[3259]6# PyKota - Print Quotas for CUPS
[2019]7#
[3481]8# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[3259]9# This program is free software: you can redistribute it and/or modify
[2019]10# it under the terms of the GNU General Public License as published by
[3259]11# the Free Software Foundation, either version 3 of the License, or
[2019]12# (at your option) any later version.
[3413]13#
[2019]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.
[3413]18#
[2019]19# You should have received a copy of the GNU General Public License
[3259]20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[2019]21#
22# $Id$
23#
[2032]24#
[2019]25
26import sys
27import os
28import cgi
29import urllib
[3333]30from xml.sax import saxutils
[2019]31
[3295]32import pykota.appinit
33from pykota import version, utils
[2019]34from pykota.dumper import DumPyKota
35
[3055]36header = """Content-type: text/html;charset=%s
[2019]37
38<html>
39  <head>
40    <title>%s</title>
41    <link rel="stylesheet" type="text/css" href="/pykota.css" />
[3032]42    <script type="text/javascript">
43    <!--
[3413]44      function checkvalues()
[3032]45      {
46          if ((document.mainform.format.value == "cups") && (document.mainform.datatype.value != "history"))
47          {
48              alert("Output format and data type are incompatible.");
49              return false;
50          }
[3413]51
[3032]52          if (document.mainform.sum.checked && (document.mainform.datatype.value != "payments") && (document.mainform.datatype.value != "history"))
53          {
54              alert("Summarize is only possible for History and Payments.");
55              return false;
56          }
[3413]57
[3032]58          if (document.mainform.sum.checked && (document.mainform.format.value == "cups"))
59          {
60              alert("Summarize is not possible with CUPS' page_log format.");
61              return false;
62          }
[3413]63
[3032]64          return true;
65      }
66    //-->
67    </script>
[2019]68  </head>
69  <body>
[2265]70    <p>
[3032]71      <form action="dumpykota.cgi" method="GET" name="mainform" onsubmit="return checkvalues()">
[2265]72        <table>
73          <tr>
74            <td>
75              <p>
76                <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a>
77                <br />
78                <a href="%s">PyKota v%s</a>
79              </p>
80            </td>
81            <td colspan="2">
82              <h1>%s</h1>
83            </td>
84          </tr>
85          <tr>
86            <td colspan="3" align="center">
87              <input type="submit" name="report" value="%s" />
88            </td>
89          </tr>
90        </table>
91        <p>
92          %s
93        </p>"""
[3413]94
[2019]95footer = """
[2265]96        <table>
97          <tr>
98            <td colspan="3" align="center">
99              <input type="submit" name="report" value="%s" />
100            </td>
101          </tr>
[3413]102        </table>
[2265]103      </form>
[3413]104    </p>
[2265]105    <hr width="25%%" />
106    <p>
107      <font size="-2">
[2909]108        <a href="http://www.pykota.com/">%s</a>
[3413]109        &copy; %s %s
[2586]110        <br />
111        <pre>
112%s
113        </pre>
[2265]114      </font>
115    </p>
[2019]116  </body>
[3413]117</html>"""
[2019]118
119class PyKotaDumperGUI(DumPyKota) :
120    """PyKota Dumper GUI"""
121    def guiDisplay(self) :
122        """Displays the dumper interface."""
123        global header, footer
[3295]124        content = [ header % (self.charset, _("PyKota Data Dumper"), \
[2265]125                        self.config.getLogoLink(), \
126                        self.config.getLogoURL(), version.__version__, \
127                        self.config.getLogoLink(), \
128                        version.__version__, _("PyKota Data Dumper"), \
[3295]129                        _("Dump"), _("Please click on the above button")) ]
[3406]130        content.append(self.htmlListDataTypes(self.options.data))
[3295]131        content.append(u"<br />")
[3406]132        content.append(self.htmlListFormats(self.options.format))
[3295]133        content.append(u"<br />")
134        content.append(self.htmlFilterInput(" ".join(self.arguments)))
135        content.append(u"<br />")
[3406]136        content.append(self.htmlOrderbyInput(self.options.orderby))
[3295]137        content.append(u"<br />")
[3406]138        content.append(self.htmlSumCheckbox(self.options.sum))
[3413]139        content.append(footer % (_("Dump"),
140                                 version.__doc__,
141                                 version.__years__,
142                                 version.__author__,
[3333]143                                 saxutils.escape(version.__gplblurb__)))
[3295]144        for c in content :
145            sys.stdout.write(c.encode(self.charset, "replace"))
146        sys.stdout.flush()
[3413]147
148    def htmlListDataTypes(self, selected="") :
[2019]149        """Displays the datatype selection list."""
150        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
151        for dt in self.validdatatypes.items() :
152            if dt[0] == selected :
153                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
154            else :
155                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
156        message += '</select></td></tr></table>'
157        return message
[3413]158
159    def htmlListFormats(self, selected="") :
[2019]160        """Displays the formats selection list."""
161        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
162        for fmt in self.validformats.items() :
163            if fmt[0] == selected :
164                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
165            else :
166                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
167        message += '</select></td></tr></table>'
168        return message
[3413]169
170    def htmlFilterInput(self, value="") :
[2019]171        """Input the optional dump filter."""
[3030]172        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100 start=today-30</strong></em>' % (value or ""))
[3413]173
174    def htmlOrderbyInput(self, value="") :
[3211]175        """Input the optional ordering."""
176        return _("Ordering") + (' : <input type="text" name="orderby" size="40" value="%s" /> <em>e.g. <strong>+username,-printername</strong></em>' % (value or ""))
[3413]177
178    def htmlSumCheckbox(self, checked="") :
[2284]179        """Input the optional Sum option."""
[2298]180        return _("Summarize") + (' : <input type="checkbox" name="sum" %s /> <em>%s</em>' % ((checked and 'checked="checked"'), _("only for payments or history")))
[3413]181
[2019]182    def guiAction(self) :
183        """Main function"""
184        try :
185            wantreport = self.form.has_key("report")
[3413]186        except TypeError :
[2019]187            pass # WebDAV request probably, seen when trying to open a csv file in OOo
[3413]188        else :
[2019]189            if wantreport :
[2032]190                try :
[2229]191                    if self.form.has_key("datatype") :
[3406]192                        self.options.data = self.form["datatype"].value
[2229]193                    if self.form.has_key("format") :
[3406]194                        self.options.format = self.form["format"].value
[3413]195                    if self.form.has_key("filter") :
[2229]196                        self.arguments = self.form["filter"].value.split()
[3413]197                    if self.form.has_key("sum") :
[3406]198                        self.options.sum = self.form["sum"].value
[3413]199                    if self.form.has_key("orderby") :
[3406]200                        self.options.orderby = self.form["orderby"].value
[3413]201                    # when no authentication is done, or when the remote username
[2229]202                    # is 'root' (even if not run as root of course), then unrestricted
203                    # dump is allowed.
[3413]204                    remuser = os.environ.get("REMOTE_USER", "root")
[2229]205                    # special hack to accomodate mod_auth_ldap Apache module
206                    try :
207                        remuser = remuser.split("=")[1].split(",")[0]
[3413]208                    except IndexError :
[2229]209                        pass
210                    if remuser != "root" :
211                        # non-'root' users when the script is password protected
212                        # can not dump any data as they like, we restrict them
213                        # to their own datas.
[3406]214                        if self.options.data not in ["printers", "pmembers", "groups", "gpquotas"] :
[2229]215                            self.arguments.append("username=%s" % remuser)
[3413]216
217                    fname = "error"
[2630]218                    ctype = "text/plain"
[3406]219                    if self.options.format in ("csv", "ssv") :
[2229]220                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
221                        ctype = "text/comma-separated-values"
222                        fname = "dump.csv"
[3406]223                    elif self.options.format == "tsv" :
[2229]224                        #ctype = "application/vnd.sun.xml.calc"     # OpenOffice.org
225                        ctype = "text/tab-separated-values"
226                        fname = "dump.tsv"
[3406]227                    elif self.options.format == "xml" :
[2229]228                        ctype = "text/xml"
229                        fname = "dump.xml"
[3406]230                    elif self.options.format == "cups" :
[2229]231                        ctype = "text/plain"
232                        fname = "page_log"
[3469]233                    sys.stdout.write("Content-type: %s\n" % ctype)
234                    sys.stdout.write("Content-disposition: attachment; filename=%s\n\n" % fname)
[2032]235                    self.main(self.arguments, self.options, restricted=0)
[2229]236                except :
[3469]237                    sys.stdout.write('Content-type: text/html\n\n<html><head><title>CGI Error</title></head><body><p><font color="red">%s</font></p></body></html>\n' % self.crashed("CGI Error").replace("\n", "<br />"))
[3413]238            else :
[2019]239                self.guiDisplay()
[3413]240
241class FakeCommandLineOptions :
[3406]242    """A class to fake command line options."""
243    output = "-"
244    data = "history"
245    format = "cups"
246    sum = None
247    orderby = None
[3413]248
[2019]249if __name__ == "__main__" :
[3295]250    utils.reinitcgilocale()
251    admin = PyKotaDumperGUI()
[2210]252    admin.deferredInit()
[2019]253    admin.form = cgi.FieldStorage()
[3406]254    admin.options = FakeCommandLineOptions()
[2019]255    admin.arguments = []
256    admin.guiAction()
257    try :
258        admin.storage.close()
[3413]259    except (TypeError, NameError, AttributeError) :
[2019]260        pass
[3413]261
[2019]262    sys.exit(0)
Note: See TracBrowser for help on using the browser.