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

Revision 2021, 6.7 kB (checked in by jalet, 19 years ago)

Fixed a bad copy&paste

  • 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 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# $Log$
26# Revision 1.2  2005/01/08 19:47:00  jalet
27# Fixed a bad copy&paste
28#
29# Revision 1.1  2005/01/08 19:13:11  jalet
30# dumpykota.cgi was added to allow the use of dumpykota through the web.
31# This makes real time interfacing with the third party software phpPrintAnalyzer
32# a breeze !
33#
34#
35
36import sys
37import os
38import cgi
39import urllib
40
41from pykota import version
42from pykota.tool import PyKotaToolError
43from pykota.dumper import DumPyKota
44from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
45
46header = """Content-type: text/html
47
48<?xml version="1.0" encoding="%s"?>
49<html>
50  <head>
51    <title>%s</title>
52    <link rel="stylesheet" type="text/css" href="/pykota.css" />
53  </head>
54  <body>
55    <form action="dumpykota.cgi" method="GET">
56      <table>
57        <tr>
58          <td>
59            <p>
60              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a>
61              <br />
62              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
63            </p>
64          </td>
65          <td colspan="2">
66            <h1>%s</h1>
67          </td>
68        </tr>
69        <tr>
70          <td colspan="3" align="center">
71            <input type="submit" name="report" value="%s" />
72          </td>
73        </tr>
74      </table>
75      <p>
76        %s
77      </p>"""
78   
79footer = """
80      <table>
81        <tr>
82          <td colspan="3" align="center">
83            <input type="submit" name="report" value="%s" />
84          </td>
85        </tr>
86      </table> 
87    </form>
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"), version.__version__, _("PyKota Data Dumper"), _("Dump"), _("Please click on the above button"))
97        print self.htmlListDataTypes(self.options.get("datatype", ""))
98        print "<br />"
99        print self.htmlListFormats(self.options.get("format", ""))
100        print "<br />"
101        print self.htmlFilterInput(" ".join(self.arguments))
102        print footer % _("Dump")
103       
104    def htmlListDataTypes(self, selected="") :   
105        """Displays the datatype selection list."""
106        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="datatype">' % _("Data Type")
107        for dt in self.validdatatypes.items() :
108            if dt[0] == selected :
109                message += '<option value="%s" selected="selected">%s (%s)</option>' % (dt[0], dt[0], dt[1])
110            else :
111                message += '<option value="%s">%s (%s)</option>' % (dt[0], dt[0], dt[1])
112        message += '</select></td></tr></table>'
113        return message
114       
115    def htmlListFormats(self, selected="") :   
116        """Displays the formats selection list."""
117        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="format">' % _("Output Format")
118        for fmt in self.validformats.items() :
119            if fmt[0] == selected :
120                message += '<option value="%s" selected="selected">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
121            else :
122                message += '<option value="%s">%s (%s)</option>' % (fmt[0], fmt[0], fmt[1])
123        message += '</select></td></tr></table>'
124        return message
125       
126    def htmlFilterInput(self, value="") :   
127        """Input the optional dump filter."""
128        return _("Filter") + (' : <input type="text" name="filter" size="40" value="%s" /> <em>e.g. <strong>username=jerome printername=HP2100</strong></em>' % (value or ""))
129       
130    def guiAction(self) :
131        """Main function"""
132        try :
133            wantreport = self.form.has_key("report")
134        except TypeError :   
135            pass # WebDAV request probably, seen when trying to open a csv file in OOo
136        else :   
137            if wantreport :
138                if self.form.has_key("datatype") :
139                    self.options["data"] = self.form["datatype"].value
140                if self.form.has_key("format") :
141                    self.options["format"] = self.form["format"].value
142                if self.form.has_key("filter") :   
143                    self.arguments = self.form["filter"].value.split()
144                   
145                if self.options["format"] in ("csv", "ssv") :
146                    #ctype = "application/vnd.sun.xml.calc"
147                    ctype = "text/comma-separated-values"
148                    fname = "dump.csv"
149                elif self.options["format"] == "tsv" :
150                    #ctype = "application/vnd.sun.xml.calc"
151                    ctype = "text/tab-separated-values"
152                    fname = "dump.tsv"
153                elif self.options["format"] == "xml" :
154                    ctype = "text/xml"
155                    fname = "dump.xml"
156                elif self.options["format"] == "cups" :
157                    ctype = "text/plain"
158                    fname = "page_log"
159                print "Content-type: %s" % ctype   
160                print "Content-disposition: attachment; filename=%s" % fname
161                print
162                try :
163                    self.main(self.arguments, self.options)
164                except PyKotaToolError, msg :   
165                    print msg
166            else :       
167                self.guiDisplay()
168           
169if __name__ == "__main__" :
170    os.environ["LC_ALL"] = getLanguagePreference()
171    admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
172    admin.form = cgi.FieldStorage()
173    admin.options = { "output" : "-",
174                "data" : "history",
175                "format" : "cups",
176              }
177    admin.arguments = []
178    admin.guiAction()
179    try :
180        admin.storage.close()
181    except (TypeError, NameError, AttributeError) :   
182        pass
183       
184    sys.exit(0)
Note: See TracBrowser for help on using the browser.