[794] | 1 | #! /usr/bin/python |
---|
| 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$ |
---|
[1053] | 25 | # Revision 1.10 2003/07/01 07:30:32 jalet |
---|
| 26 | # Message changed. |
---|
| 27 | # |
---|
[1050] | 28 | # Revision 1.9 2003/06/30 13:47:26 jalet |
---|
| 29 | # Allows multiple user / group names masks in the input field |
---|
| 30 | # |
---|
[1049] | 31 | # Revision 1.8 2003/06/30 13:32:01 jalet |
---|
| 32 | # Much more powerful CGI script for quota reports |
---|
| 33 | # |
---|
[1048] | 34 | # Revision 1.7 2003/06/30 12:46:15 jalet |
---|
| 35 | # Extracted reporting code. |
---|
| 36 | # |
---|
[952] | 37 | # Revision 1.6 2003/04/23 22:13:56 jalet |
---|
| 38 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 39 | # |
---|
[941] | 40 | # Revision 1.5 2003/04/17 21:30:09 jalet |
---|
| 41 | # Now includes the logo |
---|
| 42 | # |
---|
[893] | 43 | # Revision 1.4 2003/04/08 21:20:25 jalet |
---|
| 44 | # CGI Script now displays a link to PyKota's website. |
---|
| 45 | # |
---|
[873] | 46 | # Revision 1.3 2003/03/29 13:45:27 jalet |
---|
| 47 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 48 | # Two README files were added. |
---|
| 49 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 50 | # |
---|
[799] | 51 | # Revision 1.2 2003/02/12 11:31:51 jalet |
---|
| 52 | # doesn't use the jaxml module anymore |
---|
| 53 | # |
---|
[794] | 54 | # Revision 1.1 2003/02/10 13:41:38 jalet |
---|
| 55 | # repykota cgi script added. |
---|
| 56 | # cleaner doc. |
---|
| 57 | # |
---|
| 58 | |
---|
| 59 | import sys |
---|
| 60 | import os |
---|
| 61 | import cgi |
---|
| 62 | |
---|
[1048] | 63 | from pykota import version |
---|
| 64 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
[1049] | 65 | from pykota.reporter import PyKotaReporterError, openReporter |
---|
[1048] | 66 | |
---|
[799] | 67 | header = """Content-type: text/html |
---|
[794] | 68 | |
---|
[799] | 69 | <?xml version="1.0" encoding="iso-8859-1"?> |
---|
| 70 | <html> |
---|
| 71 | <head> |
---|
[1049] | 72 | <title>PyKota Reports</title> |
---|
[799] | 73 | </head> |
---|
| 74 | <body> |
---|
[1049] | 75 | <form action="printquota.cgi" method="POST"> |
---|
| 76 | <table> |
---|
| 77 | <tr> |
---|
| 78 | <td> |
---|
| 79 | <p> |
---|
| 80 | <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" alt="PyKota's Logo" /></a> |
---|
| 81 | <br /> |
---|
| 82 | <a href="http://www.librelogiciel.com/software/">PyKota</a> |
---|
| 83 | </p> |
---|
| 84 | </td> |
---|
| 85 | <td colspan="2"> |
---|
| 86 | <h2>PyKota Reports</h2> |
---|
| 87 | </td> |
---|
| 88 | </tr> |
---|
| 89 | <tr> |
---|
| 90 | <td colspan="3" align="center"> |
---|
| 91 | <input type="submit" name="action" value="Report" /> |
---|
| 92 | </td> |
---|
| 93 | </tr> |
---|
| 94 | </table>""" |
---|
| 95 | |
---|
[799] | 96 | footer = """ |
---|
[1049] | 97 | </form> |
---|
[799] | 98 | </body> |
---|
| 99 | </html>""" |
---|
[794] | 100 | |
---|
| 101 | |
---|
[1049] | 102 | class PyKotaReportGUI(PyKotaTool) : |
---|
| 103 | """PyKota Administrative GUI""" |
---|
| 104 | def guiDisplay(self) : |
---|
| 105 | """Displays the administrative interface.""" |
---|
| 106 | global header, footer |
---|
| 107 | print header |
---|
| 108 | print self.body |
---|
| 109 | print footer |
---|
| 110 | |
---|
| 111 | def error(self, message) : |
---|
| 112 | """Adds an error message to the GUI's body.""" |
---|
| 113 | if message : |
---|
| 114 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
| 115 | |
---|
| 116 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
| 117 | """Displays the printers multiple selection list.""" |
---|
| 118 | printers = self.storage.getMatchingPrinters(mask) |
---|
| 119 | selectednames = [p.Name for p in selected] |
---|
| 120 | message = 'Printer : <select name="printers" multiple="multiple">' |
---|
| 121 | for printer in printers : |
---|
| 122 | if printer.Name in selectednames : |
---|
| 123 | message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name) |
---|
| 124 | else : |
---|
| 125 | message += '<option value="%s">%s</option>' % (printer.Name, printer.Name) |
---|
| 126 | message += '</select>' |
---|
| 127 | return message |
---|
| 128 | |
---|
| 129 | def htmlUGNamesInput(self, value="*") : |
---|
| 130 | """Input field for user/group names wildcard.""" |
---|
| 131 | return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*") |
---|
| 132 | |
---|
| 133 | def htmlGroupsCheckbox(self, isgroup=0) : |
---|
| 134 | """Groups checkbox.""" |
---|
| 135 | if isgroup : |
---|
| 136 | return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />' |
---|
| 137 | else : |
---|
| 138 | return 'Groups report : <input type="checkbox" name="isgroup" />' |
---|
| 139 | |
---|
| 140 | def guiAction(self) : |
---|
| 141 | """Main function""" |
---|
| 142 | printers = ugmask = isgroup = None |
---|
[1053] | 143 | self.body = "<p>Please click on the button above</p>\n" |
---|
[1049] | 144 | if self.form.has_key("action") : |
---|
| 145 | action = self.form["action"].value |
---|
| 146 | if action == "Report" : |
---|
| 147 | if self.form.has_key("printers") : |
---|
| 148 | printersfield = self.form["printers"] |
---|
| 149 | if type(printersfield) != type([]) : |
---|
| 150 | printersfield = [ printersfield ] |
---|
| 151 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
| 152 | else : |
---|
| 153 | printers = self.storage.getMatchingPrinters("*") |
---|
| 154 | if self.form.has_key("ugmask") : |
---|
| 155 | ugmask = self.form["ugmask"].value |
---|
| 156 | else : |
---|
| 157 | ugmask = "*" |
---|
| 158 | if self.form.has_key("isgroup") : |
---|
| 159 | isgroup = 1 |
---|
| 160 | else : |
---|
| 161 | isgroup = 0 |
---|
| 162 | else : |
---|
| 163 | self.error(body, "Invalid action [%s]" % action) |
---|
| 164 | self.body += self.htmlListPrinters(printers or []) |
---|
| 165 | self.body += "<br />" |
---|
| 166 | self.body += self.htmlUGNamesInput(ugmask) |
---|
| 167 | self.body += "<br />" |
---|
| 168 | self.body += self.htmlGroupsCheckbox(isgroup) |
---|
| 169 | if printers and ugmask : |
---|
[1050] | 170 | self.reportingtool = openReporter(admin, "text", printers, ugmask.split(), isgroup) |
---|
[1049] | 171 | self.body += "<pre>%s</pre>" % self.reportingtool.generateReport() |
---|
| 172 | |
---|
| 173 | if __name__ == "__main__" : |
---|
| 174 | admin = PyKotaReportGUI() |
---|
| 175 | admin.form = cgi.FieldStorage() |
---|
| 176 | admin.guiAction() |
---|
| 177 | admin.guiDisplay() |
---|