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