[1240] | 1 | #! /usr/bin/python |
---|
[1635] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[794] | 3 | |
---|
| 4 | # PyKota Print Quota Reports generator |
---|
| 5 | # |
---|
[952] | 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[794] | 7 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[794] | 13 | # |
---|
[873] | 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. |
---|
[794] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2056] | 25 | # |
---|
[794] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import cgi |
---|
[1274] | 30 | import urllib |
---|
[794] | 31 | |
---|
[1048] | 32 | from pykota import version |
---|
| 33 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
[1049] | 34 | from pykota.reporter import PyKotaReporterError, openReporter |
---|
[2018] | 35 | from pykota.cgifuncs import getLanguagePreference, getCharsetPreference |
---|
[1048] | 36 | |
---|
[799] | 37 | header = """Content-type: text/html |
---|
[794] | 38 | |
---|
[1763] | 39 | <?xml version="1.0" encoding="%s"?> |
---|
[799] | 40 | <html> |
---|
| 41 | <head> |
---|
[1614] | 42 | <title>%s</title> |
---|
[1274] | 43 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
[799] | 44 | </head> |
---|
| 45 | <body> |
---|
[1049] | 46 | <form action="printquota.cgi" method="POST"> |
---|
| 47 | <table> |
---|
| 48 | <tr> |
---|
| 49 | <td> |
---|
| 50 | <p> |
---|
[2174] | 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> |
---|
[1049] | 52 | <br /> |
---|
[1614] | 53 | <a href="http://www.librelogiciel.com/software/">PyKota v%s</a> |
---|
[1049] | 54 | </p> |
---|
| 55 | </td> |
---|
| 56 | <td colspan="2"> |
---|
[1614] | 57 | <h1>%s</h1> |
---|
[1049] | 58 | </td> |
---|
| 59 | </tr> |
---|
| 60 | <tr> |
---|
| 61 | <td colspan="3" align="center"> |
---|
[1763] | 62 | <input type="submit" name="report" value="%s" /> |
---|
[1049] | 63 | </td> |
---|
| 64 | </tr> |
---|
| 65 | </table>""" |
---|
| 66 | |
---|
[799] | 67 | footer = """ |
---|
[1614] | 68 | <table> |
---|
| 69 | <tr> |
---|
| 70 | <td colspan="3" align="center"> |
---|
[1763] | 71 | <input type="submit" name="report" value="%s" /> |
---|
[1614] | 72 | </td> |
---|
| 73 | </tr> |
---|
| 74 | </table> |
---|
[1049] | 75 | </form> |
---|
[799] | 76 | </body> |
---|
| 77 | </html>""" |
---|
[794] | 78 | |
---|
[1049] | 79 | class PyKotaReportGUI(PyKotaTool) : |
---|
| 80 | """PyKota Administrative GUI""" |
---|
| 81 | def guiDisplay(self) : |
---|
| 82 | """Displays the administrative interface.""" |
---|
| 83 | global header, footer |
---|
[2174] | 84 | print header % (self.getCharset(), _("PyKota Reports"), version.__version__, version.__version__, _("PyKota Reports"), _("Report")) |
---|
[1049] | 85 | print self.body |
---|
[1614] | 86 | print footer % _("Report") |
---|
[1049] | 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] |
---|
[1817] | 97 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer") |
---|
[1049] | 98 | for printer in printers : |
---|
| 99 | if printer.Name in selectednames : |
---|
[1582] | 100 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
[1049] | 101 | else : |
---|
[1582] | 102 | message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
[1054] | 103 | message += '</select></td></tr></table>' |
---|
[1049] | 104 | return message |
---|
| 105 | |
---|
| 106 | def htmlUGNamesInput(self, value="*") : |
---|
| 107 | """Input field for user/group names wildcard.""" |
---|
[1614] | 108 | return _("User / Group names mask") + (' : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*")) |
---|
[1049] | 109 | |
---|
| 110 | def htmlGroupsCheckbox(self, isgroup=0) : |
---|
| 111 | """Groups checkbox.""" |
---|
| 112 | if isgroup : |
---|
[1614] | 113 | return _("Groups report") + ' : <input type="checkbox" checked="checked" name="isgroup" />' |
---|
[1049] | 114 | else : |
---|
[1614] | 115 | return _("Groups report") + ' : <input type="checkbox" name="isgroup" />' |
---|
[1049] | 116 | |
---|
| 117 | def guiAction(self) : |
---|
| 118 | """Main function""" |
---|
| 119 | printers = ugmask = isgroup = None |
---|
[2032] | 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 |
---|
[1817] | 126 | self.body = "<p>%s</p>\n" % _("Please click on the above button") |
---|
[1763] | 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) ]) |
---|
[1121] | 145 | else : |
---|
[1763] | 146 | ugmask = remuser # result will probably be empty, we don't care |
---|
[1049] | 147 | else : |
---|
[1763] | 148 | ugmask = remuser |
---|
| 149 | if self.form.has_key("isgroup") : |
---|
| 150 | isgroup = 1 |
---|
| 151 | else : |
---|
| 152 | isgroup = 0 |
---|
[1049] | 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) |
---|
[2229] | 158 | try : |
---|
| 159 | if not self.form.has_key("history") : |
---|
| 160 | if printers and ugmask : |
---|
| 161 | self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup) |
---|
| 162 | self.body += "%s" % self.reportingtool.generateReport() |
---|
| 163 | else : |
---|
| 164 | if remuser != "root" : |
---|
| 165 | username = remuser |
---|
| 166 | elif self.form.has_key("username") : |
---|
| 167 | username = self.form["username"].value |
---|
| 168 | else : |
---|
| 169 | username = None |
---|
| 170 | if username is not None : |
---|
| 171 | user = self.storage.getUser(username) |
---|
| 172 | else : |
---|
| 173 | user =None |
---|
| 174 | if self.form.has_key("printername") : |
---|
| 175 | printer = self.storage.getPrinter(self.form["printername"].value) |
---|
| 176 | else : |
---|
| 177 | printer = None |
---|
| 178 | if self.form.has_key("datelimit") : |
---|
| 179 | datelimit = self.form["datelimit"].value |
---|
| 180 | else : |
---|
| 181 | datelimit = None |
---|
| 182 | if self.form.has_key("hostname") : |
---|
| 183 | hostname = self.form["hostname"].value |
---|
| 184 | else : |
---|
| 185 | hostname = None |
---|
| 186 | if self.form.has_key("billingcode") : |
---|
| 187 | billingcode = self.form["billingcode"].value |
---|
| 188 | else : |
---|
| 189 | billingcode = None |
---|
| 190 | self.report = ["<h2>%s</h2>" % _("History")] |
---|
| 191 | history = self.storage.retrieveHistory(user, printer, datelimit, hostname, billingcode) |
---|
| 192 | if not history : |
---|
| 193 | self.report.append("<h3>%s</h3>" % _("Empty")) |
---|
| 194 | else : |
---|
| 195 | self.report.append('<table class="pykotatable" border="1">') |
---|
| 196 | headers = [_("Date"), _("Action"), _("User"), _("Printer"), \ |
---|
| 197 | _("Hostname"), _("JobId"), _("JobSize"), \ |
---|
| 198 | _("JobPrice"), _("Copies"), _("JobBytes"), \ |
---|
| 199 | _("PageCounter"), _("Title"), _("Filename"), \ |
---|
| 200 | _("Options"), _("MD5Sum"), _("BillingCode"), \ |
---|
| 201 | _("Pages")] |
---|
| 202 | self.report.append('<tr class="pykotacolsheader">%s</tr>' % "".join(["<th>%s</th>" % h for h in headers])) |
---|
| 203 | oddeven = 0 |
---|
| 204 | for job in history : |
---|
| 205 | oddeven += 1 |
---|
| 206 | if oddeven % 2 : |
---|
| 207 | oddevenclass = "odd" |
---|
| 208 | else : |
---|
| 209 | oddevenclass = "even" |
---|
| 210 | if job.JobAction == "DENY" : |
---|
| 211 | oddevenclass = "deny" |
---|
| 212 | elif job.JobAction == "WARN" : |
---|
| 213 | oddevenclass = "warn" |
---|
| 214 | username_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "username" : job.UserName}), job.UserName) |
---|
| 215 | printername_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "printername" : job.PrinterName}), job.PrinterName) |
---|
| 216 | if job.JobHostName : |
---|
| 217 | hostname_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "hostname" : job.JobHostName}), job.JobHostName) |
---|
| 218 | else : |
---|
| 219 | hostname_url = None |
---|
| 220 | if job.JobBillingCode : |
---|
| 221 | billingcode_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "billingcode" : job.JobBillingCode}), job.JobBillingCode) |
---|
| 222 | else : |
---|
| 223 | billingcode_url = None |
---|
| 224 | self.report.append('<tr class="%s">%s</tr>' % \ |
---|
| 225 | (oddevenclass, \ |
---|
| 226 | "".join(["<td>%s</td>" % (h or " ") \ |
---|
| 227 | for h in (job.JobDate[:19], \ |
---|
| 228 | job.JobAction, \ |
---|
| 229 | username_url, \ |
---|
| 230 | printername_url, \ |
---|
| 231 | hostname_url, \ |
---|
| 232 | job.JobId, \ |
---|
| 233 | job.JobSize, \ |
---|
| 234 | job.JobPrice, \ |
---|
| 235 | job.JobCopies, \ |
---|
| 236 | job.JobSizeBytes, \ |
---|
| 237 | job.PrinterPageCounter, \ |
---|
| 238 | job.JobTitle, \ |
---|
| 239 | job.JobFileName, \ |
---|
| 240 | job.JobOptions, \ |
---|
| 241 | job.JobMD5Sum, \ |
---|
| 242 | billingcode_url, \ |
---|
| 243 | job.JobPages)]))) |
---|
| 244 | self.report.append('</table>') |
---|
| 245 | dico = { "history" : 1, |
---|
| 246 | "datelimit" : job.JobDate, |
---|
| 247 | } |
---|
| 248 | if user and user.Exists : |
---|
| 249 | dico.update({ "username" : user.Name }) |
---|
| 250 | if printer and printer.Exists : |
---|
| 251 | dico.update({ "printername" : printer.Name }) |
---|
| 252 | if hostname : |
---|
| 253 | dico.update({ "hostname" : hostname }) |
---|
| 254 | prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico)) |
---|
| 255 | self.report.append('<a href="%s">%s</a>' % (prevurl, _("Previous page"))) |
---|
| 256 | self.body = "\n".join(self.report) |
---|
| 257 | except : |
---|
| 258 | self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />") |
---|
[1172] | 259 | |
---|
[1049] | 260 | if __name__ == "__main__" : |
---|
[2018] | 261 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
| 262 | admin = PyKotaReportGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) |
---|
[2210] | 263 | admin.deferredInit() |
---|
[1049] | 264 | admin.form = cgi.FieldStorage() |
---|
| 265 | admin.guiAction() |
---|
| 266 | admin.guiDisplay() |
---|
[1113] | 267 | try : |
---|
| 268 | admin.storage.close() |
---|
| 269 | except (TypeError, NameError, AttributeError) : |
---|
| 270 | pass |
---|
| 271 | |
---|
| 272 | sys.exit(0) |
---|