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