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

Revision 1054, 6.3 kB (checked in by jalet, 21 years ago)

Nicer UI

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