1 | #! /usr/bin/python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quotes generator |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
7 | # |
---|
8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # |
---|
26 | |
---|
27 | import sys |
---|
28 | import os |
---|
29 | import cgi |
---|
30 | import urllib |
---|
31 | import cStringIO |
---|
32 | |
---|
33 | from pykota import version |
---|
34 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
35 | from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError |
---|
36 | from pykota.cgifuncs import getLanguagePreference, getCharsetPreference |
---|
37 | |
---|
38 | header = """Content-type: text/html |
---|
39 | |
---|
40 | <?xml version="1.0" encoding="%s"?> |
---|
41 | <html> |
---|
42 | <head> |
---|
43 | <title>%s</title> |
---|
44 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
45 | </head> |
---|
46 | <body> |
---|
47 | <form action="pykotme.cgi" method="POST" enctype="multipart/form-data"> |
---|
48 | <table> |
---|
49 | <tr> |
---|
50 | <td> |
---|
51 | <p> |
---|
52 | <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/pykota.png?version=%s" alt="PyKota's Logo" /></a> |
---|
53 | <br /> |
---|
54 | <a href="http://www.librelogiciel.com/software/">PyKota v%s</a> |
---|
55 | </p> |
---|
56 | </td> |
---|
57 | <td colspan="2"> |
---|
58 | <h1>%s</h1> |
---|
59 | </td> |
---|
60 | </tr> |
---|
61 | <tr> |
---|
62 | <td colspan="3" align="center"> |
---|
63 | <input type="submit" name="report" value="%s" /> |
---|
64 | </td> |
---|
65 | </tr> |
---|
66 | </table>""" |
---|
67 | |
---|
68 | footer = """ |
---|
69 | <table> |
---|
70 | <tr> |
---|
71 | <td colspan="3" align="center"> |
---|
72 | <input type="submit" name="report" value="%s" /> |
---|
73 | </td> |
---|
74 | </tr> |
---|
75 | </table> |
---|
76 | </form> |
---|
77 | </body> |
---|
78 | </html>""" |
---|
79 | |
---|
80 | class PyKotMeGUI(PyKotaTool) : |
---|
81 | """PyKota Quote's Generator GUI""" |
---|
82 | def guiDisplay(self) : |
---|
83 | """Displays the administrative interface.""" |
---|
84 | global header, footer |
---|
85 | print header % (self.getCharset(), _("PyKota Reports"), version.__version__, version.__version__, _("PyKota Quotes"), _("Quote")) |
---|
86 | print self.body |
---|
87 | print footer % _("Quote") |
---|
88 | |
---|
89 | def error(self, message) : |
---|
90 | """Adds an error message to the GUI's body.""" |
---|
91 | if message : |
---|
92 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
93 | |
---|
94 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
95 | """Displays the printers multiple selection list.""" |
---|
96 | printers = self.storage.getMatchingPrinters(mask) |
---|
97 | selectednames = [p.Name for p in selected] |
---|
98 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer") |
---|
99 | for printer in printers : |
---|
100 | if printer.Name in selectednames : |
---|
101 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
102 | else : |
---|
103 | message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
104 | message += '</select></td></tr></table>' |
---|
105 | return message |
---|
106 | |
---|
107 | def guiAction(self) : |
---|
108 | """Main function""" |
---|
109 | printers = inputfile = None |
---|
110 | self.body = "<p>%s</p>\n" % _("Please click on the above button") |
---|
111 | if self.form.has_key("report") : |
---|
112 | if self.form.has_key("printers") : |
---|
113 | printersfield = self.form["printers"] |
---|
114 | if type(printersfield) != type([]) : |
---|
115 | printersfield = [ printersfield ] |
---|
116 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
117 | else : |
---|
118 | printers = self.storage.getMatchingPrinters("*") |
---|
119 | if self.form.has_key("inputfile") : |
---|
120 | inputfile = self.form["inputfile"].value |
---|
121 | |
---|
122 | self.body += self.htmlListPrinters(printers or []) |
---|
123 | self.body += "<br />" |
---|
124 | self.body += _("Filename") + " : " |
---|
125 | self.body += '<input type="file" size="64" name="inputfile" />' |
---|
126 | self.body += "<br />" |
---|
127 | if inputfile : |
---|
128 | try : |
---|
129 | parser = PDLAnalyzer(cStringIO.StringIO(inputfile)) |
---|
130 | jobsize = parser.getJobSize() |
---|
131 | except PDLAnalyzerError, msg : |
---|
132 | self.body += '<p><font color="red">%s</font></p>' % msg |
---|
133 | else : |
---|
134 | self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize) |
---|
135 | |
---|
136 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
137 | # special hack to accomodate mod_auth_ldap Apache module |
---|
138 | try : |
---|
139 | remuser = remuser.split("=")[1].split(",")[0] |
---|
140 | except IndexError : |
---|
141 | pass |
---|
142 | if remuser == "root" : |
---|
143 | self.body += "<p>%s</p>" % _("The exact cost of a print job can only be determined for a particular user. Please retry while logged-in.") |
---|
144 | else : |
---|
145 | try : |
---|
146 | user = self.storage.getUser(remuser) |
---|
147 | if user.Exists : |
---|
148 | for printer in printers : |
---|
149 | upquota = self.storage.getUserPQuota(user, printer) |
---|
150 | if upquota.Exists : |
---|
151 | cost = upquota.computeJobPrice(jobsize) |
---|
152 | self.body += "<p>%s</p>" % (_("Cost on printer %s : %.2f") % (printer.Name, cost)) |
---|
153 | except : |
---|
154 | self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />") |
---|
155 | |
---|
156 | if __name__ == "__main__" : |
---|
157 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
158 | admin = PyKotMeGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) |
---|
159 | admin.deferredInit() |
---|
160 | admin.form = cgi.FieldStorage() |
---|
161 | admin.guiAction() |
---|
162 | admin.guiDisplay() |
---|
163 | try : |
---|
164 | admin.storage.close() |
---|
165 | except (TypeError, NameError, AttributeError) : |
---|
166 | pass |
---|
167 | |
---|
168 | sys.exit(0) |
---|