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