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

Revision 1253, 8.2 kB (checked in by jalet, 21 years ago)

Will now search the stylesheet on my own website.

  • 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.19  2004/01/06 16:05:45  jalet
26# Will now search the stylesheet on my own website.
27#
28# Revision 1.18  2003/12/27 16:49:25  uid67467
29# Should be ok now.
30#
31# Revision 1.16  2003/12/02 14:40:20  jalet
32# Some code refactoring.
33# New HTML reporter added, which is now used in the CGI script for web based
34# print quota reports. It will need some de-uglyfication though...
35#
36# Revision 1.15  2003/10/24 22:06:42  jalet
37# Initial support for browser's language preference added.
38#
39# Revision 1.14  2003/10/10 19:48:07  jalet
40# Now displays version number
41#
42# Revision 1.13  2003/08/25 11:23:05  jalet
43# More powerful CGI script for quota reports
44#
45# Revision 1.12  2003/07/29 20:55:17  jalet
46# 1.14 is out !
47#
48# Revision 1.11  2003/07/01 12:37:31  jalet
49# Nicer UI
50#
51# Revision 1.10  2003/07/01 07:30:32  jalet
52# Message changed.
53#
54# Revision 1.9  2003/06/30 13:47:26  jalet
55# Allows multiple user / group names masks in the input field
56#
57# Revision 1.8  2003/06/30 13:32:01  jalet
58# Much more powerful CGI script for quota reports
59#
60# Revision 1.7  2003/06/30 12:46:15  jalet
61# Extracted reporting code.
62#
63# Revision 1.6  2003/04/23 22:13:56  jalet
64# Preliminary support for LPRng added BUT STILL UNTESTED.
65#
66# Revision 1.5  2003/04/17 21:30:09  jalet
67# Now includes the logo
68#
69# Revision 1.4  2003/04/08 21:20:25  jalet
70# CGI Script now displays a link to PyKota's website.
71#
72# Revision 1.3  2003/03/29 13:45:27  jalet
73# GPL paragraphs were incorrectly (from memory) copied into the sources.
74# Two README files were added.
75# Upgrade script for PostgreSQL pre 1.01 schema was added.
76#
77# Revision 1.2  2003/02/12 11:31:51  jalet
78# doesn't use the jaxml module anymore
79#
80# Revision 1.1  2003/02/10 13:41:38  jalet
81# repykota cgi script added.
82# cleaner doc.
83#
84
85import sys
86import os
87import cgi
88
89from pykota import version
90from pykota.tool import PyKotaTool, PyKotaToolError
91from pykota.reporter import PyKotaReporterError, openReporter
92
93header = """Content-type: text/html
94
95<?xml version="1.0" encoding="iso-8859-1"?>
96<html>
97  <head>
98    <title>PyKota Reports</title>
99    <link rel="stylesheet" type="text/css" href="http://www.librelogiciel.com/software/PyKota/pykota.css" />
100  </head>
101  <body>
102    <form action="printquota.cgi" method="POST">
103      <table>
104        <tr>
105          <td>
106            <p>
107              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" alt="PyKota's Logo" /></a>
108              <br />
109              <a href="http://www.librelogiciel.com/software/">PyKota version %s</a>
110            </p>
111          </td>
112          <td colspan="2">
113            <h1>PyKota Reports</h1>
114          </td>
115        </tr>
116        <tr>
117          <td colspan="3" align="center">
118            <input type="submit" name="action" value="Report" />
119          </td>
120        </tr>
121      </table>"""
122   
123footer = """
124    </form>
125  </body>
126</html>""" 
127
128def getLanguagePreference() :
129    """Returns the preferred language."""
130    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "")
131    langs = [l.strip().split(';')[0] for l in languages.split(",")]
132    return "%s_%s" % (langs[0], langs[0].upper())
133
134class PyKotaReportGUI(PyKotaTool) :
135    """PyKota Administrative GUI"""
136       
137    def guiDisplay(self) :
138        """Displays the administrative interface."""
139        global header, footer
140        print header % version.__version__
141        print self.body
142        print footer
143       
144    def error(self, message) :
145        """Adds an error message to the GUI's body."""
146        if message :
147            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
148       
149    def htmlListPrinters(self, selected=[], mask="*") :   
150        """Displays the printers multiple selection list."""
151        printers = self.storage.getMatchingPrinters(mask)
152        selectednames = [p.Name for p in selected]
153        message = '<table><tr><td valign="top">Printer :</td><td valign="top"><select name="printers" multiple="multiple">'
154        for printer in printers :
155            if printer.Name in selectednames :
156                message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name)
157            else :
158                message += '<option value="%s">%s</option>' % (printer.Name, printer.Name)
159        message += '</select></td></tr></table>'
160        return message
161       
162    def htmlUGNamesInput(self, value="*") :   
163        """Input field for user/group names wildcard."""
164        return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*")
165       
166    def htmlGroupsCheckbox(self, isgroup=0) :
167        """Groups checkbox."""
168        if isgroup :
169            return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />'
170        else :   
171            return 'Groups report : <input type="checkbox" name="isgroup" />'
172           
173    def guiAction(self) :
174        """Main function"""
175        printers = ugmask = isgroup = None
176        self.body = "<p>Please click on the button above</p>\n"
177        if self.form.has_key("action") :
178            action = self.form["action"].value
179            if action == "Report" :
180                if self.form.has_key("printers") :
181                    printersfield = self.form["printers"]
182                    if type(printersfield) != type([]) :
183                        printersfield = [ printersfield ]
184                    printers = [self.storage.getPrinter(p.value) for p in printersfield]
185                else :   
186                    printers = self.storage.getMatchingPrinters("*")
187                remuser = os.environ.get("REMOTE_USER", "root")   
188                if remuser == "root" :
189                    if self.form.has_key("ugmask") :     
190                        ugmask = self.form["ugmask"].value
191                    else :     
192                        ugmask = "*"
193                else :       
194                    if self.form.has_key("isgroup") :   
195                        user = self.storage.getUser(remuser)
196                        if user.Exists :
197                            ugmask = " ".join([ g.Name for g in self.storage.getUserGroups(user) ])
198                        else :   
199                            ugmask = remuser # result will probably be empty, we don't care
200                    else :   
201                        ugmask = remuser
202                if self.form.has_key("isgroup") :   
203                    isgroup = 1
204                else :   
205                    isgroup = 0
206            else :
207                self.error(body, "Invalid action [%s]" % action)
208        self.body += self.htmlListPrinters(printers or [])           
209        self.body += "<br />"
210        self.body += self.htmlUGNamesInput(ugmask)
211        self.body += "<br />"
212        self.body += self.htmlGroupsCheckbox(isgroup)
213        if printers and ugmask :
214            self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup)
215            # self.body += "<pre>%s</pre>" % self.reportingtool.generateReport()
216            self.body += "%s" % self.reportingtool.generateReport()
217           
218if __name__ == "__main__" :
219    os.environ["LC_ALL"] = getLanguagePreference()
220    admin = PyKotaReportGUI(lang=os.environ["LC_ALL"])
221    admin.form = cgi.FieldStorage()
222    admin.guiAction()
223    admin.guiDisplay()
224    try :
225        admin.storage.close()
226    except (TypeError, NameError, AttributeError) :   
227        pass
228       
229    sys.exit(0)
Note: See TracBrowser for help on using the browser.