root / pykota / trunk / cgi-bin / printquota.cgi @ 1113

Revision 1113, 6.4 kB (checked in by jalet, 21 years ago)

1.14 is out !

  • 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
3# PyKota Print Quota Reports generator
4#
5# PyKota - Print Quotas for CUPS and LPRng
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21#
22# $Id$
23#
24# $Log$
25# Revision 1.12  2003/07/29 20:55:17  jalet
26# 1.14 is out !
27#
28# Revision 1.11  2003/07/01 12:37:31  jalet
29# Nicer UI
30#
31# Revision 1.10  2003/07/01 07:30:32  jalet
32# Message changed.
33#
34# Revision 1.9  2003/06/30 13:47:26  jalet
35# Allows multiple user / group names masks in the input field
36#
37# Revision 1.8  2003/06/30 13:32:01  jalet
38# Much more powerful CGI script for quota reports
39#
40# Revision 1.7  2003/06/30 12:46:15  jalet
41# Extracted reporting code.
42#
43# Revision 1.6  2003/04/23 22:13:56  jalet
44# Preliminary support for LPRng added BUT STILL UNTESTED.
45#
46# Revision 1.5  2003/04/17 21:30:09  jalet
47# Now includes the logo
48#
49# Revision 1.4  2003/04/08 21:20:25  jalet
50# CGI Script now displays a link to PyKota's website.
51#
52# Revision 1.3  2003/03/29 13:45:27  jalet
53# GPL paragraphs were incorrectly (from memory) copied into the sources.
54# Two README files were added.
55# Upgrade script for PostgreSQL pre 1.01 schema was added.
56#
57# Revision 1.2  2003/02/12 11:31:51  jalet
58# doesn't use the jaxml module anymore
59#
60# Revision 1.1  2003/02/10 13:41:38  jalet
61# repykota cgi script added.
62# cleaner doc.
63#
64
65import sys
66import os
67import cgi
68
69from pykota import version
70from pykota.tool import PyKotaTool, PyKotaToolError
71from pykota.reporter import PyKotaReporterError, openReporter
72
73header = """Content-type: text/html
74
75<?xml version="1.0" encoding="iso-8859-1"?>
76<html>
77  <head>
78    <title>PyKota Reports</title>
79  </head>
80  <body>
81    <form action="printquota.cgi" method="POST">
82      <table>
83        <tr>
84          <td>
85            <p>
86              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" alt="PyKota's Logo" /></a>
87              <br />
88              <a href="http://www.librelogiciel.com/software/">PyKota</a>
89            </p>
90          </td>
91          <td colspan="2">
92            <h2>PyKota Reports</h2>
93          </td>
94        </tr>
95        <tr>
96          <td colspan="3" align="center">
97            <input type="submit" name="action" value="Report" />
98          </td>
99        </tr>
100      </table>"""
101   
102footer = """
103    </form>
104  </body>
105</html>""" 
106
107
108class PyKotaReportGUI(PyKotaTool) :
109    """PyKota Administrative GUI"""
110    def guiDisplay(self) :
111        """Displays the administrative interface."""
112        global header, footer
113        print header
114        print self.body
115        print footer
116       
117    def error(self, message) :
118        """Adds an error message to the GUI's body."""
119        if message :
120            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
121       
122    def htmlListPrinters(self, selected=[], mask="*") :   
123        """Displays the printers multiple selection list."""
124        printers = self.storage.getMatchingPrinters(mask)
125        selectednames = [p.Name for p in selected]
126        message = '<table><tr><td valign="top">Printer :</td><td valign="top"><select name="printers" multiple="multiple">'
127        for printer in printers :
128            if printer.Name in selectednames :
129                message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name)
130            else :
131                message += '<option value="%s">%s</option>' % (printer.Name, printer.Name)
132        message += '</select></td></tr></table>'
133        return message
134       
135    def htmlUGNamesInput(self, value="*") :   
136        """Input field for user/group names wildcard."""
137        return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*")
138       
139    def htmlGroupsCheckbox(self, isgroup=0) :
140        """Groups checkbox."""
141        if isgroup :
142            return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />'
143        else :   
144            return 'Groups report : <input type="checkbox" name="isgroup" />'
145           
146    def guiAction(self) :
147        """Main function"""
148        printers = ugmask = isgroup = None
149        self.body = "<p>Please click on the button above</p>\n"
150        if self.form.has_key("action") :
151            action = self.form["action"].value
152            if action == "Report" :
153                if self.form.has_key("printers") :
154                    printersfield = self.form["printers"]
155                    if type(printersfield) != type([]) :
156                        printersfield = [ printersfield ]
157                    printers = [self.storage.getPrinter(p.value) for p in printersfield]
158                else :   
159                    printers = self.storage.getMatchingPrinters("*")
160                if self.form.has_key("ugmask") :     
161                    ugmask = self.form["ugmask"].value
162                else :     
163                    ugmask = "*"
164                if self.form.has_key("isgroup") :   
165                    isgroup = 1
166                else :   
167                    isgroup = 0
168            else :
169                self.error(body, "Invalid action [%s]" % action)
170        self.body += self.htmlListPrinters(printers or [])           
171        self.body += "<br />"
172        self.body += self.htmlUGNamesInput(ugmask)
173        self.body += "<br />"
174        self.body += self.htmlGroupsCheckbox(isgroup)
175        if printers and ugmask :
176            self.reportingtool = openReporter(admin, "text", printers, ugmask.split(), isgroup)
177            self.body += "<pre>%s</pre>" % self.reportingtool.generateReport()
178   
179if __name__ == "__main__" :
180    admin = PyKotaReportGUI()
181    admin.form = cgi.FieldStorage()
182    admin.guiAction()
183    admin.guiDisplay()
184
185    try :
186        admin.storage.close()
187    except (TypeError, NameError, AttributeError) :   
188        pass
189       
190    sys.exit(0)
Note: See TracBrowser for help on using the browser.