#! /usr/bin/python # -*- coding: utf-8 -*- # PyKota Print Quotes generator # # PyKota - Print Quotas for CUPS # # (c) 2003-2013 Jerome Alet # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # $Id$ # # import sys import os import cgi import urllib import cStringIO from xml.sax import saxutils import pykota.appinit from pykota import version, utils from pykota.tool import PyKotaTool from pykota.errors import PyKotaToolError from pkpgpdls import analyzer, pdlparser header = """Content-type: text/html;charset=%s %s

PyKota's Logo
PyKota v%s

%s

""" footer = """


%s © %s %s

%s
        

""" class PyKotMeGUI(PyKotaTool) : """PyKota Quote's Generator GUI""" def guiDisplay(self) : """Displays the administrative interface.""" global header, footer content = [ header % (self.charset, _("PyKota Quotes"), \ self.config.getLogoLink(), \ self.config.getLogoURL(), version.__version__, \ self.config.getLogoLink(), \ version.__version__, _("PyKota Quotes"), \ _("Quote")) ] content.append(self.body) content.append(footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, saxutils.escape(version.__gplblurb__))) for c in content : sys.stdout.write(c.encode(self.charset, "replace")) sys.stdout.flush() def error(self, message) : """Adds an error message to the GUI's body.""" if message : self.body = '

%s

\n%s' % (message, self.body) def htmlListPrinters(self, selected=[], mask="*") : """Displays the printers multiple selection list.""" printers = self.storage.getMatchingPrinters(mask) selectednames = [p.Name for p in selected] message = '
%s :
' return message def guiAction(self) : """Main function""" printers = inputfile = None self.body = "

%s

\n" % _("Please click on the above button") if self.form.has_key("report") : if self.form.has_key("printers") : printersfield = self.form["printers"] if type(printersfield) != type([]) : printersfield = [ printersfield ] printers = [self.storage.getPrinter(p.value) for p in printersfield] else : printers = self.storage.getMatchingPrinters("*") if self.form.has_key("inputfile") : inputfile = self.form["inputfile"].value if os.environ.get("REMOTE_USER") is not None : self.body += self.htmlListPrinters(printers or []) self.body += "
" self.body += _("Filename") + " : " self.body += '' self.body += "
" if inputfile : try : parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile)) jobsize = parser.getJobSize() except pdlparser.PDLParserError, msg : self.body += '

%s

' % msg jobsize = 0 # unknown file format ? else : self.body += "

%s

" % (_("Job size : %i pages") % jobsize) remuser = os.environ.get("REMOTE_USER") # special hack to accomodate mod_auth_ldap Apache module try : remuser = remuser.split("=")[1].split(",")[0] except : pass if not remuser : self.body += "

%s

" % _("The exact cost of a print job can only be determined for a particular user. Please retry while logged-in.") else : try : user = self.storage.getUser(remuser) if user.Exists : if user.LimitBy == "noprint" : self.body += "

%s

" % _("Your account settings forbid you to print at this time.") else : for printer in printers : upquota = self.storage.getUserPQuota(user, printer) if upquota.Exists : if (printer.MaxJobSize and (jobsize > printer.MaxJobSize)) \ or (upquota.MaxJobSize and (jobsize > upquota.MaxJobSize)) : msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name else : cost = upquota.computeJobPrice(jobsize) msg = _("Cost on printer %s : %.2f") % (printer.Name, cost) if printer.PassThrough : msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode")) elif user.LimitBy == "nochange" : msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable")) self.body += "

%s

" % msg except : self.body += '

%s

' % self.crashed("CGI Error").replace("\n", "
") if __name__ == "__main__" : utils.reinitcgilocale() admin = PyKotMeGUI() admin.deferredInit() admin.form = cgi.FieldStorage() admin.guiAction() admin.guiDisplay() try : admin.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(0)