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 | <p> |
---|
48 | <form action="pykotme.cgi" method="POST" enctype="multipart/form-data"> |
---|
49 | <table> |
---|
50 | <tr> |
---|
51 | <td> |
---|
52 | <p> |
---|
53 | <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a> |
---|
54 | <br /> |
---|
55 | <a href="%s">PyKota v%s</a> |
---|
56 | </p> |
---|
57 | </td> |
---|
58 | <td colspan="2"> |
---|
59 | <h1>%s</h1> |
---|
60 | </td> |
---|
61 | </tr> |
---|
62 | <tr> |
---|
63 | <td colspan="3" align="center"> |
---|
64 | <input type="submit" name="report" value="%s" /> |
---|
65 | </td> |
---|
66 | </tr> |
---|
67 | </table>""" |
---|
68 | |
---|
69 | footer = """ |
---|
70 | <table> |
---|
71 | <tr> |
---|
72 | <td colspan="3" align="center"> |
---|
73 | <input type="submit" name="report" value="%s" /> |
---|
74 | </td> |
---|
75 | </tr> |
---|
76 | </table> |
---|
77 | </form> |
---|
78 | </p> |
---|
79 | <hr width="25%%" /> |
---|
80 | <p> |
---|
81 | <font size="-2"> |
---|
82 | <a href="http://www.librelogiciel.com/software/">%s</a> |
---|
83 | © 2003, 2004, 2005 %s |
---|
84 | </font> |
---|
85 | </p> |
---|
86 | </body> |
---|
87 | </html>""" |
---|
88 | |
---|
89 | class PyKotMeGUI(PyKotaTool) : |
---|
90 | """PyKota Quote's Generator GUI""" |
---|
91 | def guiDisplay(self) : |
---|
92 | """Displays the administrative interface.""" |
---|
93 | global header, footer |
---|
94 | print header % (self.getCharset(), _("PyKota Quotes"), \ |
---|
95 | self.config.getLogoLink(), \ |
---|
96 | self.config.getLogoURL(), version.__version__, \ |
---|
97 | self.config.getLogoLink(), \ |
---|
98 | version.__version__, _("PyKota Quotes"), \ |
---|
99 | _("Quote")) |
---|
100 | print self.body |
---|
101 | print footer % (_("Quote"), version.__doc__, version.__author__) |
---|
102 | |
---|
103 | def error(self, message) : |
---|
104 | """Adds an error message to the GUI's body.""" |
---|
105 | if message : |
---|
106 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
107 | |
---|
108 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
109 | """Displays the printers multiple selection list.""" |
---|
110 | printers = self.storage.getMatchingPrinters(mask) |
---|
111 | selectednames = [p.Name for p in selected] |
---|
112 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer") |
---|
113 | for printer in printers : |
---|
114 | if printer.Name in selectednames : |
---|
115 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
116 | else : |
---|
117 | message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
118 | message += '</select></td></tr></table>' |
---|
119 | return message |
---|
120 | |
---|
121 | def guiAction(self) : |
---|
122 | """Main function""" |
---|
123 | printers = inputfile = None |
---|
124 | self.body = "<p>%s</p>\n" % _("Please click on the above button") |
---|
125 | if self.form.has_key("report") : |
---|
126 | if self.form.has_key("printers") : |
---|
127 | printersfield = self.form["printers"] |
---|
128 | if type(printersfield) != type([]) : |
---|
129 | printersfield = [ printersfield ] |
---|
130 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
131 | else : |
---|
132 | printers = self.storage.getMatchingPrinters("*") |
---|
133 | if self.form.has_key("inputfile") : |
---|
134 | inputfile = self.form["inputfile"].value |
---|
135 | |
---|
136 | self.body += self.htmlListPrinters(printers or []) |
---|
137 | self.body += "<br />" |
---|
138 | self.body += _("Filename") + " : " |
---|
139 | self.body += '<input type="file" size="64" name="inputfile" />' |
---|
140 | self.body += "<br />" |
---|
141 | if inputfile : |
---|
142 | try : |
---|
143 | parser = PDLAnalyzer(cStringIO.StringIO(inputfile)) |
---|
144 | jobsize = parser.getJobSize() |
---|
145 | except PDLAnalyzerError, msg : |
---|
146 | self.body += '<p><font color="red">%s</font></p>' % msg |
---|
147 | else : |
---|
148 | self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize) |
---|
149 | |
---|
150 | remuser = os.environ.get("REMOTE_USER", "root") |
---|
151 | # special hack to accomodate mod_auth_ldap Apache module |
---|
152 | try : |
---|
153 | remuser = remuser.split("=")[1].split(",")[0] |
---|
154 | except IndexError : |
---|
155 | pass |
---|
156 | if remuser == "root" : |
---|
157 | 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.") |
---|
158 | else : |
---|
159 | try : |
---|
160 | user = self.storage.getUser(remuser) |
---|
161 | if user.Exists : |
---|
162 | for printer in printers : |
---|
163 | upquota = self.storage.getUserPQuota(user, printer) |
---|
164 | if upquota.Exists : |
---|
165 | cost = upquota.computeJobPrice(jobsize) |
---|
166 | self.body += "<p>%s</p>" % (_("Cost on printer %s : %.2f") % (printer.Name, cost)) |
---|
167 | except : |
---|
168 | self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />") |
---|
169 | |
---|
170 | if __name__ == "__main__" : |
---|
171 | os.environ["LC_ALL"] = getLanguagePreference() |
---|
172 | admin = PyKotMeGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) |
---|
173 | admin.deferredInit() |
---|
174 | admin.form = cgi.FieldStorage() |
---|
175 | admin.guiAction() |
---|
176 | admin.guiDisplay() |
---|
177 | try : |
---|
178 | admin.storage.close() |
---|
179 | except (TypeError, NameError, AttributeError) : |
---|
180 | pass |
---|
181 | |
---|
182 | sys.exit(0) |
---|