#! /usr/bin/python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003, 2004, 2005 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 2 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # import sys import os import cgi import urllib from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError from pykota.reporter import PyKotaReporterError, openReporter from pykota.cgifuncs import getLanguagePreference, getCharsetPreference header = """Content-type: text/html %s

PyKota's Logo
PyKota v%s

%s

""" footer = """
""" class PyKotaReportGUI(PyKotaTool) : """PyKota Administrative GUI""" def guiDisplay(self) : """Displays the administrative interface.""" global header, footer print header % (self.getCharset(), _("PyKota Reports"), version.__version__, version.__version__, _("PyKota Reports"), _("Report")) print self.body print footer % _("Report") 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 htmlUGNamesInput(self, value="*") : """Input field for user/group names wildcard.""" return _("User / Group names mask") + (' : e.g. jo*' % (value or "*")) def htmlGroupsCheckbox(self, isgroup=0) : """Groups checkbox.""" if isgroup : return _("Groups report") + ' : ' else : return _("Groups report") + ' : ' def guiAction(self) : """Main function""" printers = ugmask = isgroup = None remuser = os.environ.get("REMOTE_USER", "root") # special hack to accomodate mod_auth_ldap Apache module try : remuser = remuser.split("=")[1].split(",")[0] except IndexError : pass 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 remuser == "root" : if self.form.has_key("ugmask") : ugmask = self.form["ugmask"].value else : ugmask = "*" else : if self.form.has_key("isgroup") : user = self.storage.getUser(remuser) if user.Exists : ugmask = " ".join([ g.Name for g in self.storage.getUserGroups(user) ]) else : ugmask = remuser # result will probably be empty, we don't care else : ugmask = remuser if self.form.has_key("isgroup") : isgroup = 1 else : isgroup = 0 self.body += self.htmlListPrinters(printers or []) self.body += "
" self.body += self.htmlUGNamesInput(ugmask) self.body += "
" self.body += self.htmlGroupsCheckbox(isgroup) if not self.form.has_key("history") : if printers and ugmask : self.reportingtool = openReporter(admin, "html", printers, ugmask.split(), isgroup) self.body += "%s" % self.reportingtool.generateReport() else : if remuser != "root" : username = remuser elif self.form.has_key("username") : username = self.form["username"].value else : username = None if username is not None : user = self.storage.getUser(username) else : user =None if self.form.has_key("printername") : printer = self.storage.getPrinter(self.form["printername"].value) else : printer = None if self.form.has_key("datelimit") : datelimit = self.form["datelimit"].value else : datelimit = None if self.form.has_key("hostname") : hostname = self.form["hostname"].value else : hostname = None self.report = ["

%s

" % _("History")] history = self.storage.retrieveHistory(user, printer, datelimit, hostname) if not history : self.report.append("

%s

" % _("Empty")) else : self.report.append('') headers = [_("Date"), _("Action"), _("User"), _("Printer"), \ _("Hostname"), _("JobId"), _("JobSize"), \ _("JobPrice"), _("Copies"), _("JobBytes"), \ _("PageCounter"), _("Title"), _("Filename"), \ _("Options"), _("MD5Sum"), _("BillingCode"), \ _("Pages")] self.report.append('%s' % "".join(["" % h for h in headers])) oddeven = 0 for job in history : oddeven += 1 if oddeven % 2 : oddevenclass = "odd" else : oddevenclass = "even" if job.JobAction == "DENY" : oddevenclass = "deny" elif job.JobAction == "WARN" : oddevenclass = "warn" username_url = '%s' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "username" : job.UserName}), job.UserName) printername_url = '%s' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "printername" : job.PrinterName}), job.PrinterName) if job.JobHostName : hostname_url = '%s' % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode({"history" : 1, "hostname" : job.JobHostName}), job.JobHostName) else : hostname_url = None self.report.append('%s' % (oddevenclass, "".join(["" % (h or " ") for h in (job.JobDate[:19], job.JobAction, username_url, printername_url, hostname_url, job.JobId, job.JobSize, job.JobPrice, job.JobCopies, job.JobSizeBytes, job.PrinterPageCounter, job.JobTitle, job.JobFileName, job.JobOptions, job.JobMD5Sum, job.JobBillingCode, job.JobPages)]))) self.report.append('
%s
%s
') dico = { "history" : 1, "datelimit" : job.JobDate, } if user and user.Exists : dico.update({ "username" : user.Name }) if printer and printer.Exists : dico.update({ "printername" : printer.Name }) if hostname : dico.update({ "hostname" : hostname }) prevurl = "%s?%s" % (os.environ.get("SCRIPT_NAME", ""), urllib.urlencode(dico)) self.report.append('%s' % (prevurl, _("Previous page"))) self.body = "\n".join(self.report) if __name__ == "__main__" : os.environ["LC_ALL"] = getLanguagePreference() admin = PyKotaReportGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) admin.form = cgi.FieldStorage() admin.guiAction() admin.guiDisplay() try : admin.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(0)