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

Revision 1172, 7.7 kB (checked in by jalet, 21 years ago)

Initial support for browser's language preference added.

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