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

Revision 2174, 10.5 kB (checked in by jerome, 20 years ago)

Now CGI scripts report PyKota's version number back home : this will allow
me to gather some statistics in the future.

  • 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# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Reports generator
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import cgi
30import urllib
31
32from pykota import version
33from pykota.tool import PyKotaTool, PyKotaToolError
34from pykota.reporter import PyKotaReporterError, openReporter
35from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
36
37header = """Content-type: text/html
38
39<?xml version="1.0" encoding="%s"?>
40<html>
41  <head>
42    <title>%s</title>
43    <link rel="stylesheet" type="text/css" href="/pykota.css" />
44  </head>
45  <body>
46    <form action="printquota.cgi" method="POST">
47      <table>
48        <tr>
49          <td>
50            <p>
51              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png?version=%s" alt="PyKota's Logo" /></a>
52              <br />
53              <a href="http://www.librelogiciel.com/software/">PyKota v%s</a>
54            </p>
55          </td>
56          <td colspan="2">
57            <h1>%s</h1>
58          </td>
59        </tr>
60        <tr>
61          <td colspan="3" align="center">
62            <input type="submit" name="report" value="%s" />
63          </td>
64        </tr>
65      </table>"""
66   
67footer = """
68      <table>
69        <tr>
70          <td colspan="3" align="center">
71            <input type="submit" name="report" value="%s" />
72          </td>
73        </tr>
74      </table> 
75    </form>
76  </body>
77</html>""" 
78
79class PyKotaReportGUI(PyKotaTool) :
80    """PyKota Administrative GUI"""
81    def guiDisplay(self) :
82        """Displays the administrative interface."""
83        global header, footer
84        print header % (self.getCharset(), _("PyKota Reports"), version.__version__, version.__version__, _("PyKota Reports"), _("Report"))
85        print self.body
86        print footer % _("Report")
87       
88    def error(self, message) :
89        """Adds an error message to the GUI's body."""
90        if message :
91            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
92       
93    def htmlListPrinters(self, selected=[], mask="*") :   
94        """Displays the printers multiple selection list."""
95        printers = self.storage.getMatchingPrinters(mask)
96        selectednames = [p.Name for p in selected]
97        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer")
98        for printer in printers :
99            if printer.Name in selectednames :
100                message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
101            else :
102                message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
103        message += '</select></td></tr></table>'
104        return message
105       
106    def htmlUGNamesInput(self, value="*") :   
107        """Input field for user/group names wildcard."""
108        return _("User / Group names mask") + (' : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*"))
109       
110    def htmlGroupsCheckbox(self, isgroup=0) :
111        """Groups checkbox."""
112        if isgroup :
113            return _("Groups report") + ' : <input type="checkbox" checked="checked" name="isgroup" />'
114        else :   
115            return _("Groups report") + ' : <input type="checkbox" name="isgroup" />'
116           
117    def guiAction(self) :
118        """Main function"""
119        printers = ugmask = isgroup = None
120        remuser = os.environ.get("REMOTE_USER", "root")   
121        # special hack to accomodate mod_auth_ldap Apache module
122        try :
123            remuser = remuser.split("=")[1].split(",")[0]
124        except IndexError :   
125            pass
126        self.body = "<p>%s</p>\n" % _("Please click on the above button")
127        if self.form.has_key("report") :
128            if self.form.has_key("printers") :
129                printersfield = self.form["printers"]
130                if type(printersfield) != type([]) :
131                    printersfield = [ printersfield ]
132                printers = [self.storage.getPrinter(p.value) for p in printersfield]
133            else :   
134                printers = self.storage.getMatchingPrinters("*")
135            if remuser == "root" :
136                if self.form.has_key("ugmask") :     
137                    ugmask = self.form["ugmask"].value
138                else :     
139                    ugmask = "*"
140            else :       
141                if self.form.has_key("isgroup") :   
142                    user = self.storage.getUser(remuser)
143                    if user.Exists :
144                        ugmask = " ".join([ g.Name for g in self.storage.getUserGroups(user) ])
145                    else :   
146                        ugmask = remuser # result will probably be empty, we don't care
147                else :   
148                    ugmask = remuser
149            if self.form.has_key("isgroup") :   
150                isgroup = 1
151            else :   
152                isgroup = 0
153        self.body += self.htmlListPrinters(printers or [])           
154        self.body += "<br />"
155        self.body += self.htmlUGNamesInput(ugmask)
156        self.body += "<br />"
157        self.body += self.htmlGroupsCheckbox(isgroup)
158        if not self.form.has_key("history") :
159            if printers and ugmask :
160                self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup)
161                self.body += "%s" % self.reportingtool.generateReport()
162        else :       
163            if remuser != "root" :
164                username = remuser
165            elif self.form.has_key("username") :   
166                username = self.form["username"].value
167            else :   
168                username = None
169            if username is not None :   
170                user = self.storage.getUser(username)
171            else :   
172                user =None
173            if self.form.has_key("printername") :
174                printer = self.storage.getPrinter(self.form["printername"].value)
175            else :   
176                printer = None
177            if self.form.has_key("datelimit") :   
178                datelimit = self.form["datelimit"].value
179            else :   
180                datelimit = None
181            if self.form.has_key("hostname") :   
182                hostname = self.form["hostname"].value
183            else :   
184                hostname = None
185            self.report = ["<h2>%s</h2>" % _("History")]   
186            history = self.storage.retrieveHistory(user, printer, datelimit, hostname)
187            if not history :
188                self.report.append("<h3>%s</h3>" % _("Empty"))
189            else :
190                self.report.append('<table class="pykotatable" border="1">')
191                headers = [_("Date"), _("Action"), _("User"), _("Printer"), \
192                           _("Hostname"), _("JobId"), _("JobSize"), \
193                           _("JobPrice"), _("Copies"), _("JobBytes"), \
194                           _("PageCounter"), _("Title"), _("Filename"), \
195                           _("Options"), _("MD5Sum"), _("BillingCode"), \
196                           _("Pages")]
197                self.report.append('<tr class="pykotacolsheader">%s</tr>' % "".join(["<th>%s</th>" % h for h in headers]))
198                oddeven = 0
199                for job in history :
200                    oddeven += 1
201                    if oddeven % 2 :
202                        oddevenclass = "odd"
203                    else :   
204                        oddevenclass = "even"
205                    if job.JobAction == "DENY" :
206                        oddevenclass = "deny"
207                    elif job.JobAction == "WARN" :   
208                        oddevenclass = "warn"
209                    username_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "username" : job.UserName}), job.UserName)
210                    printername_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "printername" : job.PrinterName}), job.PrinterName)
211                    if job.JobHostName :
212                        hostname_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "hostname" : job.JobHostName}), job.JobHostName)
213                    else :   
214                        hostname_url = None
215                    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, job.JobMD5Sum, job.JobBillingCode, job.JobPages)])))
216                self.report.append('</table>')
217                dico = { "history" : 1,
218                         "datelimit" : job.JobDate,
219                       }
220                if user and user.Exists :
221                    dico.update({ "username" : user.Name })
222                if printer and printer.Exists :
223                    dico.update({ "printername" : printer.Name })
224                if hostname :   
225                    dico.update({ "hostname" : hostname })
226                prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico))
227                self.report.append('<a href="%s">%s</a>' % (prevurl, _("Previous page")))
228            self.body = "\n".join(self.report)   
229           
230if __name__ == "__main__" :
231    os.environ["LC_ALL"] = getLanguagePreference()
232    admin = PyKotaReportGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
233    admin.form = cgi.FieldStorage()
234    admin.guiAction()
235    admin.guiDisplay()
236    try :
237        admin.storage.close()
238    except (TypeError, NameError, AttributeError) :   
239        pass
240       
241    sys.exit(0)
Note: See TracBrowser for help on using the browser.