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