[2152] | 1 | #! /usr/bin/python |
---|
[3411] | 2 | # -*- coding: utf-8 -*-*- |
---|
[2152] | 3 | |
---|
| 4 | # PyKota Print Quotes generator |
---|
| 5 | # |
---|
[3259] | 6 | # PyKota - Print Quotas for CUPS |
---|
[2152] | 7 | # |
---|
[3275] | 8 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3259] | 9 | # This program is free software: you can redistribute it and/or modify |
---|
[2152] | 10 | # it under the terms of the GNU General Public License as published by |
---|
[3259] | 11 | # the Free Software Foundation, either version 3 of the License, or |
---|
[2152] | 12 | # (at your option) any later version. |
---|
[3259] | 13 | # |
---|
[2152] | 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 |
---|
[3259] | 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[2152] | 21 | # |
---|
| 22 | # $Id$ |
---|
| 23 | # |
---|
| 24 | # |
---|
| 25 | |
---|
| 26 | import sys |
---|
| 27 | import os |
---|
| 28 | import cgi |
---|
| 29 | import urllib |
---|
[2153] | 30 | import cStringIO |
---|
[3333] | 31 | from xml.sax import saxutils |
---|
[2152] | 32 | |
---|
[3295] | 33 | import pykota.appinit |
---|
| 34 | |
---|
| 35 | from pykota import version, utils |
---|
| 36 | from pykota.tool import PyKotaTool |
---|
| 37 | from pykota.errors import PyKotaToolError |
---|
| 38 | |
---|
[2909] | 39 | from pkpgpdls import analyzer, pdlparser |
---|
[2352] | 40 | |
---|
| 41 | |
---|
[3055] | 42 | header = """Content-type: text/html;charset=%s |
---|
[2152] | 43 | |
---|
| 44 | <html> |
---|
| 45 | <head> |
---|
| 46 | <title>%s</title> |
---|
| 47 | <link rel="stylesheet" type="text/css" href="/pykota.css" /> |
---|
| 48 | </head> |
---|
| 49 | <body> |
---|
[2265] | 50 | <p> |
---|
| 51 | <form action="pykotme.cgi" method="POST" enctype="multipart/form-data"> |
---|
| 52 | <table> |
---|
| 53 | <tr> |
---|
| 54 | <td> |
---|
| 55 | <p> |
---|
| 56 | <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a> |
---|
| 57 | <br /> |
---|
| 58 | <a href="%s">PyKota v%s</a> |
---|
| 59 | </p> |
---|
| 60 | </td> |
---|
| 61 | <td colspan="2"> |
---|
| 62 | <h1>%s</h1> |
---|
| 63 | </td> |
---|
| 64 | </tr> |
---|
| 65 | <tr> |
---|
| 66 | <td colspan="3" align="center"> |
---|
| 67 | <input type="submit" name="report" value="%s" /> |
---|
| 68 | </td> |
---|
| 69 | </tr> |
---|
| 70 | </table>""" |
---|
[2152] | 71 | |
---|
| 72 | footer = """ |
---|
[2265] | 73 | <table> |
---|
| 74 | <tr> |
---|
| 75 | <td colspan="3" align="center"> |
---|
| 76 | <input type="submit" name="report" value="%s" /> |
---|
| 77 | </td> |
---|
| 78 | </tr> |
---|
| 79 | </table> |
---|
| 80 | </form> |
---|
| 81 | </p> |
---|
| 82 | <hr width="25%%" /> |
---|
| 83 | <p> |
---|
| 84 | <font size="-2"> |
---|
[2909] | 85 | <a href="http://www.pykota.com/">%s</a> |
---|
[2267] | 86 | © %s %s |
---|
[2586] | 87 | <br /> |
---|
| 88 | <pre> |
---|
| 89 | %s |
---|
| 90 | </pre> |
---|
[2265] | 91 | </font> |
---|
| 92 | </p> |
---|
[2152] | 93 | </body> |
---|
| 94 | </html>""" |
---|
| 95 | |
---|
| 96 | class PyKotMeGUI(PyKotaTool) : |
---|
| 97 | """PyKota Quote's Generator GUI""" |
---|
| 98 | def guiDisplay(self) : |
---|
| 99 | """Displays the administrative interface.""" |
---|
| 100 | global header, footer |
---|
[3295] | 101 | content = [ header % (self.charset, _("PyKota Quotes"), \ |
---|
[2265] | 102 | self.config.getLogoLink(), \ |
---|
| 103 | self.config.getLogoURL(), version.__version__, \ |
---|
| 104 | self.config.getLogoLink(), \ |
---|
| 105 | version.__version__, _("PyKota Quotes"), \ |
---|
[3295] | 106 | _("Quote")) ] |
---|
| 107 | content.append(self.body) |
---|
[3333] | 108 | content.append(footer % (_("Quote"), |
---|
| 109 | version.__doc__, |
---|
| 110 | version.__years__, |
---|
| 111 | version.__author__, |
---|
| 112 | saxutils.escape(version.__gplblurb__))) |
---|
[3295] | 113 | for c in content : |
---|
| 114 | sys.stdout.write(c.encode(self.charset, "replace")) |
---|
| 115 | sys.stdout.flush() |
---|
[2152] | 116 | |
---|
| 117 | def error(self, message) : |
---|
| 118 | """Adds an error message to the GUI's body.""" |
---|
| 119 | if message : |
---|
| 120 | self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) |
---|
| 121 | |
---|
[2153] | 122 | def htmlListPrinters(self, selected=[], mask="*") : |
---|
| 123 | """Displays the printers multiple selection list.""" |
---|
| 124 | printers = self.storage.getMatchingPrinters(mask) |
---|
| 125 | selectednames = [p.Name for p in selected] |
---|
| 126 | message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer") |
---|
| 127 | for printer in printers : |
---|
| 128 | if printer.Name in selectednames : |
---|
| 129 | message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
| 130 | else : |
---|
| 131 | message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description) |
---|
| 132 | message += '</select></td></tr></table>' |
---|
| 133 | return message |
---|
[2152] | 134 | |
---|
| 135 | def guiAction(self) : |
---|
| 136 | """Main function""" |
---|
[2153] | 137 | printers = inputfile = None |
---|
[2152] | 138 | self.body = "<p>%s</p>\n" % _("Please click on the above button") |
---|
| 139 | if self.form.has_key("report") : |
---|
| 140 | if self.form.has_key("printers") : |
---|
| 141 | printersfield = self.form["printers"] |
---|
| 142 | if type(printersfield) != type([]) : |
---|
| 143 | printersfield = [ printersfield ] |
---|
| 144 | printers = [self.storage.getPrinter(p.value) for p in printersfield] |
---|
| 145 | else : |
---|
| 146 | printers = self.storage.getMatchingPrinters("*") |
---|
[2153] | 147 | if self.form.has_key("inputfile") : |
---|
| 148 | inputfile = self.form["inputfile"].value |
---|
[2152] | 149 | |
---|
[2381] | 150 | if os.environ.get("REMOTE_USER") is not None : |
---|
| 151 | self.body += self.htmlListPrinters(printers or []) |
---|
| 152 | self.body += "<br />" |
---|
[2153] | 153 | self.body += _("Filename") + " : " |
---|
| 154 | self.body += '<input type="file" size="64" name="inputfile" />' |
---|
| 155 | self.body += "<br />" |
---|
| 156 | if inputfile : |
---|
| 157 | try : |
---|
[2352] | 158 | parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile)) |
---|
[2153] | 159 | jobsize = parser.getJobSize() |
---|
[2352] | 160 | except pdlparser.PDLParserError, msg : |
---|
[2229] | 161 | self.body += '<p><font color="red">%s</font></p>' % msg |
---|
[2379] | 162 | jobsize = 0 # unknown file format ? |
---|
[2153] | 163 | else : |
---|
[2229] | 164 | self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize) |
---|
| 165 | |
---|
[2525] | 166 | remuser = os.environ.get("REMOTE_USER") |
---|
[2229] | 167 | # special hack to accomodate mod_auth_ldap Apache module |
---|
| 168 | try : |
---|
| 169 | remuser = remuser.split("=")[1].split(",")[0] |
---|
[2525] | 170 | except : |
---|
[2229] | 171 | pass |
---|
[2525] | 172 | if not remuser : |
---|
[2229] | 173 | 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.") |
---|
| 174 | else : |
---|
| 175 | try : |
---|
| 176 | user = self.storage.getUser(remuser) |
---|
[2232] | 177 | if user.Exists : |
---|
[2524] | 178 | if user.LimitBy == "noprint" : |
---|
| 179 | self.body += "<p>%s</p>" % _("Your account settings forbid you to print at this time.") |
---|
| 180 | else : |
---|
| 181 | for printer in printers : |
---|
| 182 | upquota = self.storage.getUserPQuota(user, printer) |
---|
| 183 | if upquota.Exists : |
---|
| 184 | if printer.MaxJobSize and (jobsize > printer.MaxJobSize) : |
---|
| 185 | msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name |
---|
| 186 | else : |
---|
| 187 | cost = upquota.computeJobPrice(jobsize) |
---|
| 188 | msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) |
---|
| 189 | if printer.PassThrough : |
---|
| 190 | msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) |
---|
| 191 | elif user.LimitBy == "nochange" : |
---|
| 192 | msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) |
---|
| 193 | self.body += "<p>%s</p>" % msg |
---|
[2229] | 194 | except : |
---|
| 195 | self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />") |
---|
[2152] | 196 | |
---|
| 197 | if __name__ == "__main__" : |
---|
[3295] | 198 | utils.reinitcgilocale() |
---|
| 199 | admin = PyKotMeGUI() |
---|
[2210] | 200 | admin.deferredInit() |
---|
[2152] | 201 | admin.form = cgi.FieldStorage() |
---|
| 202 | admin.guiAction() |
---|
| 203 | admin.guiDisplay() |
---|
| 204 | try : |
---|
| 205 | admin.storage.close() |
---|
| 206 | except (TypeError, NameError, AttributeError) : |
---|
| 207 | pass |
---|
| 208 | |
---|
| 209 | sys.exit(0) |
---|