[3411] | 1 | # -*- coding: utf-8 -*-*- |
---|
[1048] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[1048] | 4 | # |
---|
[3275] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[1048] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1048] | 9 | # (at your option) any later version. |
---|
[3260] | 10 | # |
---|
[1048] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1048] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[2064] | 21 | # |
---|
[1048] | 22 | |
---|
[3184] | 23 | """This module defines bases classes used by all reporters.""" |
---|
| 24 | |
---|
[2945] | 25 | import os |
---|
| 26 | import imp |
---|
[1240] | 27 | from mx import DateTime |
---|
| 28 | |
---|
[3288] | 29 | from pykota.errors import PyKotaReporterError |
---|
[1048] | 30 | |
---|
| 31 | class BaseReporter : |
---|
| 32 | """Base class for all reports.""" |
---|
| 33 | def __init__(self, tool, printers, ugnames, isgroup) : |
---|
| 34 | """Initialize local datas.""" |
---|
| 35 | self.tool = tool |
---|
| 36 | self.printers = printers |
---|
| 37 | self.ugnames = ugnames |
---|
| 38 | self.isgroup = isgroup |
---|
| 39 | |
---|
[1235] | 40 | def getPrinterTitle(self, printer) : |
---|
[3184] | 41 | """Returns the formatted title for a given printer.""" |
---|
[1582] | 42 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
[1235] | 43 | |
---|
| 44 | def getPrinterGraceDelay(self, printer) : |
---|
[3184] | 45 | """Returns the formatted grace delay for a given printer.""" |
---|
[1235] | 46 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
| 47 | |
---|
| 48 | def getPrinterPrices(self, printer) : |
---|
[3184] | 49 | """Returns the formatted prices for a given printer.""" |
---|
[1235] | 50 | return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0)) |
---|
| 51 | |
---|
| 52 | def getReportHeader(self) : |
---|
[3184] | 53 | """Returns the correct header depending on users vs users groups.""" |
---|
[1235] | 54 | if self.isgroup : |
---|
[2054] | 55 | return _("Group overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 56 | else : |
---|
[2054] | 57 | return _("User overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 58 | |
---|
| 59 | def getPrinterRealPageCounter(self, printer) : |
---|
[3184] | 60 | """Returns the formatted real page counter for a given printer.""" |
---|
[1692] | 61 | msg = _("unknown") |
---|
| 62 | if printer.LastJob.Exists : |
---|
| 63 | try : |
---|
| 64 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
| 65 | except TypeError : |
---|
| 66 | pass |
---|
[1235] | 67 | return _("Real : %s") % msg |
---|
| 68 | |
---|
| 69 | def getTotals(self, total, totalmoney) : |
---|
[3184] | 70 | """Returns the formatted totals.""" |
---|
[1235] | 71 | return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11])) |
---|
| 72 | |
---|
| 73 | def getQuota(self, entry, quota) : |
---|
| 74 | """Prints the quota information.""" |
---|
| 75 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
| 76 | pagecounter = int(quota.PageCounter or 0) |
---|
| 77 | balance = float(entry.AccountBalance or 0.0) |
---|
| 78 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
[2054] | 79 | if not hasattr(entry, "OverCharge") : |
---|
| 80 | overcharge = _("N/A") # Not available for groups |
---|
| 81 | else : |
---|
| 82 | overcharge = float(entry.OverCharge or 0.0) |
---|
| 83 | if not hasattr(quota, "WarnCount") : |
---|
| 84 | warncount = _("N/A") # Not available for groups |
---|
| 85 | else : |
---|
| 86 | warncount = int(quota.WarnCount or 0) |
---|
[1582] | 87 | |
---|
[2452] | 88 | if (not entry.LimitBy) or (entry.LimitBy.lower() == "quota") : |
---|
| 89 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
[1235] | 90 | datelimit = "DENY" |
---|
[1418] | 91 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 92 | datelimit = "DENY" |
---|
| 93 | elif quota.DateLimit is not None : |
---|
| 94 | now = DateTime.now() |
---|
[3050] | 95 | datelimit = DateTime.ISO.ParseDateTime(str(quota.DateLimit)[:19]) |
---|
[1418] | 96 | if now >= datelimit : |
---|
| 97 | datelimit = "DENY" |
---|
[2452] | 98 | else : |
---|
[1418] | 99 | datelimit = "" |
---|
| 100 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
[1235] | 101 | else : |
---|
[2452] | 102 | if entry.LimitBy.lower() == "balance" : |
---|
[2692] | 103 | balancezero = self.tool.config.getBalanceZero() |
---|
| 104 | if balance == balancezero : |
---|
[2452] | 105 | if entry.OverCharge > 0 : |
---|
| 106 | datelimit = "DENY" |
---|
| 107 | reached = "+B" |
---|
| 108 | else : |
---|
| 109 | # overcharging by a negative or nul factor means user is always allowed to print |
---|
| 110 | # TODO : do something when printer prices are negative as well ! |
---|
| 111 | datelimit = "" |
---|
| 112 | reached = "-B" |
---|
[2692] | 113 | elif balance < balancezero : |
---|
[1235] | 114 | datelimit = "DENY" |
---|
[2452] | 115 | reached = "+B" |
---|
| 116 | elif balance <= self.tool.config.getPoorMan() : |
---|
| 117 | datelimit = "WARNING" |
---|
| 118 | reached = "?B" |
---|
| 119 | else : |
---|
| 120 | datelimit = "" |
---|
| 121 | reached = "-B" |
---|
| 122 | elif entry.LimitBy.lower() == "noquota" : |
---|
| 123 | reached = "NQ" |
---|
[1235] | 124 | datelimit = "" |
---|
[2452] | 125 | elif entry.LimitBy.lower() == "nochange" : |
---|
| 126 | reached = "NC" |
---|
| 127 | datelimit = "" |
---|
| 128 | else : |
---|
| 129 | # noprint |
---|
| 130 | reached = "NP" |
---|
| 131 | datelimit = "DENY" |
---|
[1235] | 132 | |
---|
| 133 | strbalance = ("%5.2f" % balance)[:10] |
---|
| 134 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
[2054] | 135 | strovercharge = ("%5s" % overcharge)[:5] |
---|
| 136 | strwarncount = ("%4s" % warncount)[:4] |
---|
| 137 | return (lifepagecounter, lifetimepaid, entry.Name, reached, \ |
---|
| 138 | pagecounter, str(quota.SoftLimit), str(quota.HardLimit), \ |
---|
| 139 | strbalance, str(datelimit)[:10], lifepagecounter, \ |
---|
| 140 | strlifetimepaid, strovercharge, strwarncount) |
---|
[1235] | 141 | |
---|
[1048] | 142 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
| 143 | """Returns a reporter instance of the proper reporter.""" |
---|
| 144 | try : |
---|
[2945] | 145 | reporterbackend = imp.load_source("reporterbackend", |
---|
| 146 | os.path.join(os.path.dirname(__file__), |
---|
| 147 | "reporters", |
---|
| 148 | "%s.py" % reporttype.lower())) |
---|
[1048] | 149 | except ImportError : |
---|
| 150 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
| 151 | else : |
---|
[1240] | 152 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|