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