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

Revision 1049, 6.0 kB (checked in by jalet, 21 years ago)

Much more powerful CGI script for quota reports

  • 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.8  2003/06/30 13:32:01  jalet
26# Much more powerful CGI script for quota reports
27#
28# Revision 1.7  2003/06/30 12:46:15  jalet
29# Extracted reporting code.
30#
31# Revision 1.6  2003/04/23 22:13:56  jalet
32# Preliminary support for LPRng added BUT STILL UNTESTED.
33#
34# Revision 1.5  2003/04/17 21:30:09  jalet
35# Now includes the logo
36#
37# Revision 1.4  2003/04/08 21:20:25  jalet
38# CGI Script now displays a link to PyKota's website.
39#
40# Revision 1.3  2003/03/29 13:45:27  jalet
41# GPL paragraphs were incorrectly (from memory) copied into the sources.
42# Two README files were added.
43# Upgrade script for PostgreSQL pre 1.01 schema was added.
44#
45# Revision 1.2  2003/02/12 11:31:51  jalet
46# doesn't use the jaxml module anymore
47#
48# Revision 1.1  2003/02/10 13:41:38  jalet
49# repykota cgi script added.
50# cleaner doc.
51#
52
53import sys
54import os
55import cgi
56
57from pykota import version
58from pykota.tool import PyKotaTool, PyKotaToolError
59from pykota.reporter import PyKotaReporterError, openReporter
60
61header = """Content-type: text/html
62
63<?xml version="1.0" encoding="iso-8859-1"?>
64<html>
65  <head>
66    <title>PyKota Reports</title>
67  </head>
68  <body>
69    <form action="printquota.cgi" method="POST">
70      <table>
71        <tr>
72          <td>
73            <p>
74              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" alt="PyKota's Logo" /></a>
75              <br />
76              <a href="http://www.librelogiciel.com/software/">PyKota</a>
77            </p>
78          </td>
79          <td colspan="2">
80            <h2>PyKota Reports</h2>
81          </td>
82        </tr>
83        <tr>
84          <td colspan="3" align="center">
85            <input type="submit" name="action" value="Report" />
86          </td>
87        </tr>
88      </table>"""
89   
90footer = """
91    </form>
92  </body>
93</html>""" 
94
95
96class PyKotaReportGUI(PyKotaTool) :
97    """PyKota Administrative GUI"""
98    def guiDisplay(self) :
99        """Displays the administrative interface."""
100        global header, footer
101        print header
102        print self.body
103        print footer
104       
105    def error(self, message) :
106        """Adds an error message to the GUI's body."""
107        if message :
108            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
109       
110    def htmlListPrinters(self, selected=[], mask="*") :   
111        """Displays the printers multiple selection list."""
112        printers = self.storage.getMatchingPrinters(mask)
113        selectednames = [p.Name for p in selected]
114        message = 'Printer : <select name="printers" multiple="multiple">'
115        for printer in printers :
116            if printer.Name in selectednames :
117                message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name)
118            else :
119                message += '<option value="%s">%s</option>' % (printer.Name, printer.Name)
120        message += '</select>'
121        return message
122       
123    def htmlUGNamesInput(self, value="*") :   
124        """Input field for user/group names wildcard."""
125        return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*")
126       
127    def htmlGroupsCheckbox(self, isgroup=0) :
128        """Groups checkbox."""
129        if isgroup :
130            return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />'
131        else :   
132            return 'Groups report : <input type="checkbox" name="isgroup" />'
133           
134    def guiAction(self) :
135        """Main function"""
136        printers = ugmask = isgroup = None
137        self.body = "<p>Please click on the menu above</p>\n"
138        if self.form.has_key("action") :
139            action = self.form["action"].value
140            if action == "Report" :
141                if self.form.has_key("printers") :
142                    printersfield = self.form["printers"]
143                    if type(printersfield) != type([]) :
144                        printersfield = [ printersfield ]
145                    printers = [self.storage.getPrinter(p.value) for p in printersfield]
146                else :   
147                    printers = self.storage.getMatchingPrinters("*")
148                if self.form.has_key("ugmask") :     
149                    ugmask = self.form["ugmask"].value
150                else :     
151                    ugmask = "*"
152                if self.form.has_key("isgroup") :   
153                    isgroup = 1
154                else :   
155                    isgroup = 0
156            else :
157                self.error(body, "Invalid action [%s]" % action)
158        self.body += self.htmlListPrinters(printers or [])           
159        self.body += "<br />"
160        self.body += self.htmlUGNamesInput(ugmask)
161        self.body += "<br />"
162        self.body += self.htmlGroupsCheckbox(isgroup)
163        if printers and ugmask :
164            self.reportingtool = openReporter(admin, "text", printers, [ ugmask ], isgroup)
165            self.body += "<pre>%s</pre>" % self.reportingtool.generateReport()
166   
167if __name__ == "__main__" :
168    admin = PyKotaReportGUI()
169    admin.form = cgi.FieldStorage()
170    admin.guiAction()
171    admin.guiDisplay()
Note: See TracBrowser for help on using the browser.