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

Revision 1240, 8.1 kB (checked in by uid67467, 21 years ago)

Should be ok now.

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