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