1 | #! /usr/bin/python |
---|
2 | |
---|
3 | # PyKota Print Quota Reports generator |
---|
4 | # |
---|
5 | # PyKota - Print Quotas for CUPS and LPRng |
---|
6 | # |
---|
7 | # (c) 2003-2004 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.29 2004/06/06 21:07:55 jalet |
---|
26 | # Improved CGI script to allow history on printers and hostnames. |
---|
27 | # Better (?) colors in stylesheet. |
---|
28 | # |
---|
29 | # Revision 1.28 2004/06/05 22:42:41 jalet |
---|
30 | # Improved web history reports |
---|
31 | # |
---|
32 | # Revision 1.27 2004/05/26 14:49:40 jalet |
---|
33 | # First try at saving the job-originating-hostname in the database |
---|
34 | # |
---|
35 | # Revision 1.26 2004/03/24 19:37:04 jalet |
---|
36 | # Doesn't retrieve users or printers objects to display the history, |
---|
37 | # this is not needed, and saves a lot of time (and database queries |
---|
38 | # if storagecahing is disabled) |
---|
39 | # |
---|
40 | # Revision 1.25 2004/01/12 15:28:45 jalet |
---|
41 | # Now can output the user's history on several printers at the same time. |
---|
42 | # |
---|
43 | # Revision 1.24 2004/01/12 14:52:03 jalet |
---|
44 | # Cuts the date string |
---|
45 | # |
---|
46 | # Revision 1.23 2004/01/12 14:35:01 jalet |
---|
47 | # Printing history added to CGI script. |
---|
48 | # |
---|
49 | # Revision 1.22 2004/01/09 07:58:53 jalet |
---|
50 | # Changed URL to PyKota's logo |
---|
51 | # |
---|
52 | # Revision 1.21 2004/01/08 14:10:32 jalet |
---|
53 | # Copyright year changed. |
---|
54 | # |
---|
55 | # Revision 1.20 2004/01/07 16:07:17 jalet |
---|
56 | # The stylesheet is again expected to be local, it was a bad idea to use |
---|
57 | # the one on my server. |
---|
58 | # |
---|
59 | # Revision 1.19 2004/01/06 16:05:45 jalet |
---|
60 | # Will now search the stylesheet on my own website. |
---|
61 | # |
---|
62 | # Revision 1.18 2003/12/27 16:49:25 uid67467 |
---|
63 | # Should be ok now. |
---|
64 | # |
---|
65 | # Revision 1.16 2003/12/02 14:40:20 jalet |
---|
66 | # Some code refactoring. |
---|
67 | # New HTML reporter added, which is now used in the CGI script for web based |
---|
68 | # print quota reports. It will need some de-uglyfication though... |
---|
69 | # |
---|
70 | # Revision 1.15 2003/10/24 22:06:42 jalet |
---|
71 | # Initial support for browser's language preference added. |
---|
72 | # |
---|
73 | # Revision 1.14 2003/10/10 19:48:07 jalet |
---|
74 | # Now displays version number |
---|
75 | # |
---|
76 | # Revision 1.13 2003/08/25 11:23:05 jalet |
---|
77 | # More powerful CGI script for quota reports |
---|
78 | # |
---|
79 | # Revision 1.12 2003/07/29 20:55:17 jalet |
---|
80 | # 1.14 is out ! |
---|
81 | # |
---|
82 | # Revision 1.11 2003/07/01 12:37:31 jalet |
---|
83 | # Nicer UI |
---|
84 | # |
---|
85 | # Revision 1.10 2003/07/01 07:30:32 jalet |
---|
86 | # Message changed. |
---|
87 | # |
---|
88 | # Revision 1.9 2003/06/30 13:47:26 jalet |
---|
89 | # Allows multiple user / group names masks in the input field |
---|
90 | # |
---|
91 | # Revision 1.8 2003/06/30 13:32:01 jalet |
---|
92 | # Much more powerful CGI script for quota reports |
---|
93 | # |
---|
94 | # Revision 1.7 2003/06/30 12:46:15 jalet |
---|
95 | # Extracted reporting code. |
---|
96 | # |
---|
97 | # Revision 1.6 2003/04/23 22:13:56 jalet |
---|
98 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
99 | # |
---|
100 | # Revision 1.5 2003/04/17 21:30:09 jalet |
---|
101 | # Now includes the logo |
---|
102 | # |
---|
103 | # Revision 1.4 2003/04/08 21:20:25 jalet |
---|
104 | # CGI Script now displays a link to PyKota's website. |
---|
105 | # |
---|
106 | # Revision 1.3 2003/03/29 13:45:27 jalet |
---|
107 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
108 | # Two README files were added. |
---|
109 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
110 | # |
---|
111 | # Revision 1.2 2003/02/12 11:31:51 jalet |
---|
112 | # doesn't use the jaxml module anymore |
---|
113 | # |
---|
114 | # Revision 1.1 2003/02/10 13:41:38 jalet |
---|
115 | # repykota cgi script added. |
---|
116 | # cleaner doc. |
---|
117 | # |
---|
118 | |
---|
119 | import sys |
---|
120 | import os |
---|
121 | import cgi |
---|
122 | import urllib |
---|
123 | |
---|
124 | from pykota import version |
---|
125 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
126 | from pykota.reporter import PyKotaReporterError, openReporter |
---|
127 | |
---|
128 | header = """Content-type: text/html |
---|
129 | |
---|
130 | <?xml version="1.0" encoding="iso-8859-1"?> |
---|
131 | <html> |
---|
132 | <head> |
---|
133 | <title>PyKota Reports</title> |
---|
134 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
135 | </head> |
---|
136 | <body> |
---|
137 | <form action="printquota.cgi" method="POST"> |
---|
138 | <table> |
---|
139 | <tr> |
---|
140 | <td> |
---|
141 | <p> |
---|
142 | <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png" alt="PyKota's Logo" /></a> |
---|
143 | <br /> |
---|
144 | <a href="http://www.librelogiciel.com/software/">PyKota version %s</a> |
---|
145 | </p> |
---|
146 | </td> |
---|
147 | <td colspan="2"> |
---|
148 | <h1>PyKota Reports</h1> |
---|
149 | </td> |
---|
150 | </tr> |
---|
151 | <tr> |
---|
152 | <td colspan="3" align="center"> |
---|
153 | <input type="submit" name="action" value="Report" /> |
---|
154 | </td> |
---|
155 | </tr> |
---|
156 | </table>""" |
---|
157 | |
---|
158 | footer = """ |
---|
159 | </form> |
---|
160 | </body> |
---|
161 | </html>""" |
---|
162 | |
---|
163 | def getLanguagePreference() : |
---|
164 | """Returns the preferred language.""" |
---|
165 | languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "") |
---|
166 | langs = [l.strip().split(';')[0] for l in languages.split(",")] |
---|
167 | return "%s_%s" % (langs[0], langs[0].upper()) |
---|
168 | |
---|
169 | class PyKotaReportGUI(PyKotaTool) : |
---|
170 | """PyKota Administrative GUI""" |
---|
171 | |
---|
172 | def guiDisplay(self) : |
---|
173 | """Displays the administrative interface.""" |
---|
174 | global header, footer |
---|
175 | print header % version.__version__ |
---|
176 | print self.body |
---|
177 | print footer |
---|
178 | |
---|
179 | def error(self, message) : |
---|
180 | """Adds an error message to the GUI's body.""" |
---|
181 | if message : |
---|
182 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
183 | |
---|
184 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
185 | """Displays the printers multiple selection list.""" |
---|
186 | printers = self.storage.getMatchingPrinters(mask) |
---|
187 | selectednames = [p.Name for p in selected] |
---|
188 | message = '<table><tr><td valign="top">Printer :</td><td valign="top"><select name="printers" multiple="multiple">' |
---|
189 | for printer in printers : |
---|
190 | if printer.Name in selectednames : |
---|
191 | message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name) |
---|
192 | else : |
---|
193 | message += '<option value="%s">%s</option>' % (printer.Name, printer.Name) |
---|
194 | message += '</select></td></tr></table>' |
---|
195 | return message |
---|
196 | |
---|
197 | def htmlUGNamesInput(self, value="*") : |
---|
198 | """Input field for user/group names wildcard.""" |
---|
199 | return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*") |
---|
200 | |
---|
201 | def htmlGroupsCheckbox(self, isgroup=0) : |
---|
202 | """Groups checkbox.""" |
---|
203 | if isgroup : |
---|
204 | return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />' |
---|
205 | else : |
---|
206 | return 'Groups report : <input type="checkbox" name="isgroup" />' |
---|
207 | |
---|
208 | def guiAction(self) : |
---|
209 | """Main function""" |
---|
210 | printers = ugmask = isgroup = None |
---|
211 | self.body = "<p>Please click on the button above</p>\n" |
---|
212 | if self.form.has_key("action") : |
---|
213 | action = self.form["action"].value |
---|
214 | if action == "Report" : |
---|
215 | if self.form.has_key("printers") : |
---|
216 | printersfield = self.form["printers"] |
---|
217 | if type(printersfield) != type([]) : |
---|
218 | printersfield = [ printersfield ] |
---|
219 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
220 | else : |
---|
221 | printers = self.storage.getMatchingPrinters("*") |
---|
222 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
223 | if remuser == "root" : |
---|
224 | if self.form.has_key("ugmask") : |
---|
225 | ugmask = self.form["ugmask"].value |
---|
226 | else : |
---|
227 | ugmask = "*" |
---|
228 | else : |
---|
229 | if self.form.has_key("isgroup") : |
---|
230 | user = self.storage.getUser(remuser) |
---|
231 | if user.Exists : |
---|
232 | ugmask = " ".join([ g.Name for g in self.storage.getUserGroups(user) ]) |
---|
233 | else : |
---|
234 | ugmask = remuser # result will probably be empty, we don't care |
---|
235 | else : |
---|
236 | ugmask = remuser |
---|
237 | if self.form.has_key("isgroup") : |
---|
238 | isgroup = 1 |
---|
239 | else : |
---|
240 | isgroup = 0 |
---|
241 | else : |
---|
242 | self.error(body, "Invalid action [%s]" % action) |
---|
243 | self.body += self.htmlListPrinters(printers or []) |
---|
244 | self.body += "<br />" |
---|
245 | self.body += self.htmlUGNamesInput(ugmask) |
---|
246 | self.body += "<br />" |
---|
247 | self.body += self.htmlGroupsCheckbox(isgroup) |
---|
248 | if not self.form.has_key("history") : |
---|
249 | if printers and ugmask : |
---|
250 | self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup) |
---|
251 | self.body += "%s" % self.reportingtool.generateReport() |
---|
252 | else : |
---|
253 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
254 | if remuser != "root" : |
---|
255 | username = remuser |
---|
256 | elif self.form.has_key("username") : |
---|
257 | username = self.form["username"].value |
---|
258 | else : |
---|
259 | username = None |
---|
260 | if username is not None : |
---|
261 | user = self.storage.getUser(username) |
---|
262 | else : |
---|
263 | user =None |
---|
264 | if self.form.has_key("printername") : |
---|
265 | printer = self.storage.getPrinter(self.form["printername"].value) |
---|
266 | else : |
---|
267 | printer = None |
---|
268 | if self.form.has_key("datelimit") : |
---|
269 | datelimit = self.form["datelimit"].value |
---|
270 | else : |
---|
271 | datelimit = None |
---|
272 | if self.form.has_key("hostname") : |
---|
273 | hostname = self.form["hostname"].value |
---|
274 | else : |
---|
275 | hostname = None |
---|
276 | self.report = ["<h2>History</h2>"] |
---|
277 | history = self.storage.retrieveHistory(user, printer, datelimit, hostname) |
---|
278 | if not history : |
---|
279 | self.report.append("<h3>Empty</h3>") |
---|
280 | else : |
---|
281 | self.report.append('<table class="pykotatable" border="1">') |
---|
282 | headers = ["Date", "Action", "User", "Printer", "Hostname", "JobId", "JobSize", "JobPrice", "Copies", "JobBytes", "PageCounter", "Title", "Filename", "Options"] |
---|
283 | self.report.append('<tr class="pykotacolsheader">%s</tr>' % "".join(["<th>%s</th>" % h for h in headers])) |
---|
284 | oddeven = 0 |
---|
285 | for job in history : |
---|
286 | oddeven += 1 |
---|
287 | if oddeven % 2 : |
---|
288 | oddevenclass = "odd" |
---|
289 | else : |
---|
290 | oddevenclass = "even" |
---|
291 | if job.JobAction == "DENY" : |
---|
292 | oddevenclass = "deny" |
---|
293 | elif job.JobAction == "WARN" : |
---|
294 | oddevenclass = "warn" |
---|
295 | username_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "username" : job.UserName}), job.UserName) |
---|
296 | printername_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "printername" : job.PrinterName}), job.PrinterName) |
---|
297 | if job.JobHostName : |
---|
298 | hostname_url = '<a href="%s?%s">%s</a>' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "hostname" : job.JobHostName}), job.JobHostName) |
---|
299 | else : |
---|
300 | hostname_url = None |
---|
301 | self.report.append('<tr class="%s">%s</tr>' % (oddevenclass, "".join(["<td>%s</td>" % (h or " ") for h in (job.JobDate[:19], job.JobAction, username_url, printername_url, hostname_url, job.JobId, job.JobSize, job.JobPrice, job.JobCopies, job.JobSizeBytes, job.PrinterPageCounter, job.JobTitle, job.JobFileName, job.JobOptions)]))) |
---|
302 | self.report.append('</table>') |
---|
303 | dico = { "history" : 1, |
---|
304 | "datelimit" : job.JobDate, |
---|
305 | } |
---|
306 | if user and user.Exists : |
---|
307 | dico.update({ "username" : user.Name }) |
---|
308 | if printer and printer.Exists : |
---|
309 | dico.update({ "printername" : printer.Name }) |
---|
310 | if hostname : |
---|
311 | dico.update({ "hostname" : hostname }) |
---|
312 | prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico)) |
---|
313 | self.report.append('<a href="%s">Previous page</a>' % prevurl) |
---|
314 | self.body = "\n".join(self.report) |
---|
315 | |
---|
316 | if __name__ == "__main__" : |
---|
317 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
318 | admin = PyKotaReportGUI(lang=os.environ["LC_ALL"]) |
---|
319 | admin.form = cgi.FieldStorage() |
---|
320 | admin.guiAction() |
---|
321 | admin.guiDisplay() |
---|
322 | try : |
---|
323 | admin.storage.close() |
---|
324 | except (TypeError, NameError, AttributeError) : |
---|
325 | pass |
---|
326 | |
---|
327 | sys.exit(0) |
---|