[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> |
---|
[1262] | 51 | <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" 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 |
---|
[1763] | 84 | print header % (self.getCharset(), _("PyKota Reports"), 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) |
---|
[1274] | 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 |
---|
[1525] | 181 | if self.form.has_key("hostname") : |
---|
| 182 | hostname = self.form["hostname"].value |
---|
| 183 | else : |
---|
| 184 | hostname = None |
---|
[1614] | 185 | self.report = ["<h2>%s</h2>" % _("History")] |
---|
[1525] | 186 | history = self.storage.retrieveHistory(user, printer, datelimit, hostname) |
---|
[1274] | 187 | if not history : |
---|
[1614] | 188 | self.report.append("<h3>%s</h3>" % _("Empty")) |
---|
[1274] | 189 | else : |
---|
| 190 | self.report.append('<table class="pykotatable" border="1">') |
---|
[2056] | 191 | headers = [_("Date"), _("Action"), _("User"), _("Printer"), \ |
---|
| 192 | _("Hostname"), _("JobId"), _("JobSize"), \ |
---|
| 193 | _("JobPrice"), _("Copies"), _("JobBytes"), \ |
---|
| 194 | _("PageCounter"), _("Title"), _("Filename"), \ |
---|
| 195 | _("Options"), _("MD5Sum"), _("BillingCode"), \ |
---|
| 196 | _("Pages")] |
---|
[1274] | 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" |
---|
[1525] | 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 |
---|
[2056] | 215 | self.report.append('<tr class="%s">%s</tr>' % (oddevenclass, "".join(["<td>%s</td>" % (h or " ") 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)]))) |
---|
[1274] | 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 }) |
---|
[1525] | 224 | if hostname : |
---|
| 225 | dico.update({ "hostname" : hostname }) |
---|
[1274] | 226 | prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico)) |
---|
[1621] | 227 | self.report.append('<a href="%s">%s</a>' % (prevurl, _("Previous page"))) |
---|
[1274] | 228 | self.body = "\n".join(self.report) |
---|
[1172] | 229 | |
---|
[1049] | 230 | if __name__ == "__main__" : |
---|
[2018] | 231 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
| 232 | admin = PyKotaReportGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) |
---|
[1049] | 233 | admin.form = cgi.FieldStorage() |
---|
| 234 | admin.guiAction() |
---|
| 235 | admin.guiDisplay() |
---|
[1113] | 236 | try : |
---|
| 237 | admin.storage.close() |
---|
| 238 | except (TypeError, NameError, AttributeError) : |
---|
| 239 | pass |
---|
| 240 | |
---|
| 241 | sys.exit(0) |
---|