1 | #! /usr/bin/python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quotes generator |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS |
---|
7 | # |
---|
8 | # (c) 2003, 2004, 2005, 2006, 2007 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 3 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, see <http://www.gnu.org/licenses/>. |
---|
21 | # |
---|
22 | # $Id$ |
---|
23 | # |
---|
24 | # |
---|
25 | |
---|
26 | import sys |
---|
27 | import os |
---|
28 | import cgi |
---|
29 | import urllib |
---|
30 | import cStringIO |
---|
31 | |
---|
32 | from pykota import version |
---|
33 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
34 | from pykota.cgifuncs import getLanguagePreference, getCharsetPreference |
---|
35 | from pkpgpdls import analyzer, pdlparser |
---|
36 | |
---|
37 | |
---|
38 | header = """Content-type: text/html;charset=%s |
---|
39 | |
---|
40 | <html> |
---|
41 | <head> |
---|
42 | <title>%s</title> |
---|
43 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
44 | </head> |
---|
45 | <body> |
---|
46 | <!-- %s %s --> |
---|
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.pykota.com/">%s</a> |
---|
83 | © %s %s |
---|
84 | <br /> |
---|
85 | <pre> |
---|
86 | %s |
---|
87 | </pre> |
---|
88 | </font> |
---|
89 | </p> |
---|
90 | </body> |
---|
91 | </html>""" |
---|
92 | |
---|
93 | class PyKotMeGUI(PyKotaTool) : |
---|
94 | """PyKota Quote's Generator GUI""" |
---|
95 | def guiDisplay(self) : |
---|
96 | """Displays the administrative interface.""" |
---|
97 | global header, footer |
---|
98 | print header % (self.charset, _("PyKota Quotes"), \ |
---|
99 | self.language, self.charset, \ |
---|
100 | self.config.getLogoLink(), \ |
---|
101 | self.config.getLogoURL(), version.__version__, \ |
---|
102 | self.config.getLogoLink(), \ |
---|
103 | version.__version__, _("PyKota Quotes"), \ |
---|
104 | _("Quote")) |
---|
105 | print self.body |
---|
106 | print footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__) |
---|
107 | |
---|
108 | def error(self, message) : |
---|
109 | """Adds an error message to the GUI's body.""" |
---|
110 | if message : |
---|
111 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
112 | |
---|
113 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
114 | """Displays the printers multiple selection list.""" |
---|
115 | printers = self.storage.getMatchingPrinters(mask) |
---|
116 | selectednames = [p.Name for p in selected] |
---|
117 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer") |
---|
118 | for printer in printers : |
---|
119 | if printer.Name in selectednames : |
---|
120 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
121 | else : |
---|
122 | message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
123 | message += '</select></td></tr></table>' |
---|
124 | return message |
---|
125 | |
---|
126 | def guiAction(self) : |
---|
127 | """Main function""" |
---|
128 | printers = inputfile = None |
---|
129 | self.body = "<p>%s</p>\n" % _("Please click on the above button") |
---|
130 | if self.form.has_key("report") : |
---|
131 | if self.form.has_key("printers") : |
---|
132 | printersfield = self.form["printers"] |
---|
133 | if type(printersfield) != type([]) : |
---|
134 | printersfield = [ printersfield ] |
---|
135 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
136 | else : |
---|
137 | printers = self.storage.getMatchingPrinters("*") |
---|
138 | if self.form.has_key("inputfile") : |
---|
139 | inputfile = self.form["inputfile"].value |
---|
140 | |
---|
141 | if os.environ.get("REMOTE_USER") is not None : |
---|
142 | self.body += self.htmlListPrinters(printers or []) |
---|
143 | self.body += "<br />" |
---|
144 | self.body += _("Filename") + " : " |
---|
145 | self.body += '<input type="file" size="64" name="inputfile" />' |
---|
146 | self.body += "<br />" |
---|
147 | if inputfile : |
---|
148 | try : |
---|
149 | parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile)) |
---|
150 | jobsize = parser.getJobSize() |
---|
151 | except pdlparser.PDLParserError, msg : |
---|
152 | self.body += '<p><font color="red">%s</font></p>' % msg |
---|
153 | jobsize = 0 # unknown file format ? |
---|
154 | else : |
---|
155 | self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize) |
---|
156 | |
---|
157 | remuser = os.environ.get("REMOTE_USER") |
---|
158 | # special hack to accomodate mod_auth_ldap Apache module |
---|
159 | try : |
---|
160 | remuser = remuser.split("=")[1].split(",")[0] |
---|
161 | except : |
---|
162 | pass |
---|
163 | if not remuser : |
---|
164 | 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.") |
---|
165 | else : |
---|
166 | try : |
---|
167 | user = self.storage.getUser(remuser) |
---|
168 | if user.Exists : |
---|
169 | if user.LimitBy == "noprint" : |
---|
170 | self.body += "<p>%s</p>" % _("Your account settings forbid you to print at this time.") |
---|
171 | else : |
---|
172 | for printer in printers : |
---|
173 | upquota = self.storage.getUserPQuota(user, printer) |
---|
174 | if upquota.Exists : |
---|
175 | if printer.MaxJobSize and (jobsize > printer.MaxJobSize) : |
---|
176 | msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name |
---|
177 | else : |
---|
178 | cost = upquota.computeJobPrice(jobsize) |
---|
179 | msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
180 | if printer.PassThrough : |
---|
181 | msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) |
---|
182 | elif user.LimitBy == "nochange" : |
---|
183 | msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) |
---|
184 | self.body += "<p>%s</p>" % msg |
---|
185 | except : |
---|
186 | self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />") |
---|
187 | |
---|
188 | if __name__ == "__main__" : |
---|
189 | admin = PyKotMeGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) |
---|
190 | admin.deferredInit() |
---|
191 | admin.form = cgi.FieldStorage() |
---|
192 | admin.guiAction() |
---|
193 | admin.guiDisplay() |
---|
194 | try : |
---|
195 | admin.storage.close() |
---|
196 | except (TypeError, NameError, AttributeError) : |
---|
197 | pass |
---|
198 | |
---|
199 | sys.exit(0) |
---|