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