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