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

Revision 1614, 13.5 kB (checked in by jalet, 20 years ago)

All messages should be translatable 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-2004 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.31  2004/07/21 13:32:02  jalet
26# All messages should be translatable now.
27#
28# Revision 1.30  2004/07/01 17:45:48  jalet
29# Added code to handle the description field for printers
30#
31# Revision 1.29  2004/06/06 21:07:55  jalet
32# Improved CGI script to allow history on printers and hostnames.
33# Better (?) colors in stylesheet.
34#
35# Revision 1.28  2004/06/05 22:42:41  jalet
36# Improved web history reports
37#
38# Revision 1.27  2004/05/26 14:49:40  jalet
39# First try at saving the job-originating-hostname in the database
40#
41# Revision 1.26  2004/03/24 19:37:04  jalet
42# Doesn't retrieve users or printers objects to display the history,
43# this is not needed, and saves a lot of time (and database queries
44# if storagecahing is disabled)
45#
46# Revision 1.25  2004/01/12 15:28:45  jalet
47# Now can output the user's history on several printers at the same time.
48#
49# Revision 1.24  2004/01/12 14:52:03  jalet
50# Cuts the date string
51#
52# Revision 1.23  2004/01/12 14:35:01  jalet
53# Printing history added to CGI script.
54#
55# Revision 1.22  2004/01/09 07:58:53  jalet
56# Changed URL to PyKota's logo
57#
58# Revision 1.21  2004/01/08 14:10:32  jalet
59# Copyright year changed.
60#
61# Revision 1.20  2004/01/07 16:07:17  jalet
62# The stylesheet is again expected to be local, it was a bad idea to use
63# the one on my server.
64#
65# Revision 1.19  2004/01/06 16:05:45  jalet
66# Will now search the stylesheet on my own website.
67#
68# Revision 1.18  2003/12/27 16:49:25  uid67467
69# Should be ok now.
70#
71# Revision 1.16  2003/12/02 14:40:20  jalet
72# Some code refactoring.
73# New HTML reporter added, which is now used in the CGI script for web based
74# print quota reports. It will need some de-uglyfication though...
75#
76# Revision 1.15  2003/10/24 22:06:42  jalet
77# Initial support for browser's language preference added.
78#
79# Revision 1.14  2003/10/10 19:48:07  jalet
80# Now displays version number
81#
82# Revision 1.13  2003/08/25 11:23:05  jalet
83# More powerful CGI script for quota reports
84#
85# Revision 1.12  2003/07/29 20:55:17  jalet
86# 1.14 is out !
87#
88# Revision 1.11  2003/07/01 12:37:31  jalet
89# Nicer UI
90#
91# Revision 1.10  2003/07/01 07:30:32  jalet
92# Message changed.
93#
94# Revision 1.9  2003/06/30 13:47:26  jalet
95# Allows multiple user / group names masks in the input field
96#
97# Revision 1.8  2003/06/30 13:32:01  jalet
98# Much more powerful CGI script for quota reports
99#
100# Revision 1.7  2003/06/30 12:46:15  jalet
101# Extracted reporting code.
102#
103# Revision 1.6  2003/04/23 22:13:56  jalet
104# Preliminary support for LPRng added BUT STILL UNTESTED.
105#
106# Revision 1.5  2003/04/17 21:30:09  jalet
107# Now includes the logo
108#
109# Revision 1.4  2003/04/08 21:20:25  jalet
110# CGI Script now displays a link to PyKota's website.
111#
112# Revision 1.3  2003/03/29 13:45:27  jalet
113# GPL paragraphs were incorrectly (from memory) copied into the sources.
114# Two README files were added.
115# Upgrade script for PostgreSQL pre 1.01 schema was added.
116#
117# Revision 1.2  2003/02/12 11:31:51  jalet
118# doesn't use the jaxml module anymore
119#
120# Revision 1.1  2003/02/10 13:41:38  jalet
121# repykota cgi script added.
122# cleaner doc.
123#
124
125import sys
126import os
127import cgi
128import urllib
129
130from pykota import version
131from pykota.tool import PyKotaTool, PyKotaToolError
132from pykota.reporter import PyKotaReporterError, openReporter
133
134header = """Content-type: text/html
135
136<?xml version="1.0" encoding="iso-8859-1"?>
137<html>
138  <head>
139    <title>%s</title>
140    <link rel="stylesheet" type="text/css" href="/pykota.css" />
141  </head>
142  <body>
143    <form action="printquota.cgi" method="POST">
144      <table>
145        <tr>
146          <td>
147            <p>
148              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a>
149              <br />
150              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
151            </p>
152          </td>
153          <td colspan="2">
154            <h1>%s</h1>
155          </td>
156        </tr>
157        <tr>
158          <td colspan="3" align="center">
159            <input type="submit" name="action" value="%s" />
160          </td>
161        </tr>
162      </table>"""
163   
164footer = """
165      <table>
166        <tr>
167          <td colspan="3" align="center">
168            <input type="submit" name="action" value="%s" />
169          </td>
170        </tr>
171      </table> 
172    </form>
173  </body>
174</html>""" 
175
176def getLanguagePreference() :
177    """Returns the preferred language."""
178    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "")
179    langs = [l.strip().split(';')[0] for l in languages.split(",")]
180    return "%s_%s" % (langs[0], langs[0].upper())
181
182class PyKotaReportGUI(PyKotaTool) :
183    """PyKota Administrative GUI"""
184       
185    def guiDisplay(self) :
186        """Displays the administrative interface."""
187        global header, footer
188        print header % (_("PyKota Reports"), version.__version__, _("PyKota Reports"), _("Report"))
189        print self.body
190        print footer % _("Report")
191       
192    def error(self, message) :
193        """Adds an error message to the GUI's body."""
194        if message :
195            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
196       
197    def htmlListPrinters(self, selected=[], mask="*") :   
198        """Displays the printers multiple selection list."""
199        printers = self.storage.getMatchingPrinters(mask)
200        selectednames = [p.Name for p in selected]
201        message = '<table><tr><td valign="top">Printer :</td><td valign="top"><select name="printers" multiple="multiple">'
202        for printer in printers :
203            if printer.Name in selectednames :
204                message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
205            else :
206                message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
207        message += '</select></td></tr></table>'
208        return message
209       
210    def htmlUGNamesInput(self, value="*") :   
211        """Input field for user/group names wildcard."""
212        return _("User / Group names mask") + (' : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*"))
213       
214    def htmlGroupsCheckbox(self, isgroup=0) :
215        """Groups checkbox."""
216        if isgroup :
217            return _("Groups report") + ' : <input type="checkbox" checked="checked" name="isgroup" />'
218        else :   
219            return _("Groups report") + ' : <input type="checkbox" name="isgroup" />'
220           
221    def guiAction(self) :
222        """Main function"""
223        printers = ugmask = isgroup = None
224        self.body = "<p>Please click on the button above</p>\n"
225        if self.form.has_key("action") :
226            action = self.form["action"].value
227            if action == _("Report") :
228                if self.form.has_key("printers") :
229                    printersfield = self.form["printers"]
230                    if type(printersfield) != type([]) :
231                        printersfield = [ printersfield ]
232                    printers = [self.storage.getPrinter(p.value) for p in printersfield]
233                else :   
234                    printers = self.storage.getMatchingPrinters("*")
235                remuser = os.environ.get("REMOTE_USER", "root")   
236                if remuser == "root" :
237                    if self.form.has_key("ugmask") :     
238                        ugmask = self.form["ugmask"].value
239                    else :     
240                        ugmask = "*"
241                else :       
242                    if self.form.has_key("isgroup") :   
243                        user = self.storage.getUser(remuser)
244                        if user.Exists :
245                            ugmask = " ".join([ g.Name for g in self.storage.getUserGroups(user) ])
246                        else :   
247                            ugmask = remuser # result will probably be empty, we don't care
248                    else :   
249                        ugmask = remuser
250                if self.form.has_key("isgroup") :   
251                    isgroup = 1
252                else :   
253                    isgroup = 0
254            else :
255                self.error(body, "Invalid action [%s]" % action)
256        self.body += self.htmlListPrinters(printers or [])           
257        self.body += "<br />"
258        self.body += self.htmlUGNamesInput(ugmask)
259        self.body += "<br />"
260        self.body += self.htmlGroupsCheckbox(isgroup)
261        if not self.form.has_key("history") :
262            if printers and ugmask :
263                self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup)
264                self.body += "%s" % self.reportingtool.generateReport()
265        else :       
266            remuser = os.environ.get("REMOTE_USER", "root")   
267            if remuser != "root" :
268                username = remuser
269            elif self.form.has_key("username") :   
270                username = self.form["username"].value
271            else :   
272                username = None
273            if username is not None :   
274                user = self.storage.getUser(username)
275            else :   
276                user =None
277            if self.form.has_key("printername") :
278                printer = self.storage.getPrinter(self.form["printername"].value)
279            else :   
280                printer = None
281            if self.form.has_key("datelimit") :   
282                datelimit = self.form["datelimit"].value
283            else :   
284                datelimit = None
285            if self.form.has_key("hostname") :   
286                hostname = self.form["hostname"].value
287            else :   
288                hostname = None
289            self.report = ["<h2>%s</h2>" % _("History")]   
290            history = self.storage.retrieveHistory(user, printer, datelimit, hostname)
291            if not history :
292                self.report.append("<h3>%s</h3>" % _("Empty"))
293            else :
294                self.report.append('<table class="pykotatable" border="1">')
295                headers = [_("Date"), _("Action"), _("User"), _("Printer"), _("Hostname"), _("JobId"), _("JobSize"), _("JobPrice"), _("Copies"), _("JobBytes"), _("PageCounter"), _("Title"), _("Filename"), _("Options")]
296                self.report.append('<tr class="pykotacolsheader">%s</tr>' % "".join(["<th>%s</th>" % h for h in headers]))
297                oddeven = 0
298                for job in history :
299                    oddeven += 1
300                    if oddeven % 2 :
301                        oddevenclass = "odd"
302                    else :   
303                        oddevenclass = "even"
304                    if job.JobAction == "DENY" :
305                        oddevenclass = "deny"
306                    elif job.JobAction == "WARN" :   
307                        oddevenclass = "warn"
308                    username_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "username" : job.UserName}), job.UserName)
309                    printername_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "printername" : job.PrinterName}), job.PrinterName)
310                    if job.JobHostName :
311                        hostname_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "hostname" : job.JobHostName}), job.JobHostName)
312                    else :   
313                        hostname_url = None
314                    self.report.append('<tr class="%s">%s</tr>' % (oddevenclass, "".join(["<td>%s</td>" % (h or "&nbsp;") for h in (job.JobDate[:19], job.JobAction, username_url, printername_url, hostname_url, job.JobId, job.JobSize, job.JobPrice, job.JobCopies, job.JobSizeBytes, job.PrinterPageCounter, job.JobTitle, job.JobFileName, job.JobOptions)])))
315                self.report.append('</table>')
316                dico = { "history" : 1,
317                         "datelimit" : job.JobDate,
318                       }
319                if user and user.Exists :
320                    dico.update({ "username" : user.Name })
321                if printer and printer.Exists :
322                    dico.update({ "printername" : printer.Name })
323                if hostname :   
324                    dico.update({ "hostname" : hostname })
325                prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico))
326                self.report.append('<a href="%s">Previous page</a>' % prevurl)
327            self.body = "\n".join(self.report)   
328           
329if __name__ == "__main__" :
330    os.environ["LC_ALL"] = getLanguagePreference()
331    admin = PyKotaReportGUI(lang=os.environ["LC_ALL"])
332    admin.form = cgi.FieldStorage()
333    admin.guiAction()
334    admin.guiDisplay()
335    try :
336        admin.storage.close()
337    except (TypeError, NameError, AttributeError) :   
338        pass
339       
340    sys.exit(0)
Note: See TracBrowser for help on using the browser.