[1048] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1048] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[1048] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2064] | 23 | # |
---|
[1048] | 24 | |
---|
[1240] | 25 | from mx import DateTime |
---|
| 26 | |
---|
[1048] | 27 | class PyKotaReporterError(Exception): |
---|
| 28 | """An exception for Reporter related stuff.""" |
---|
| 29 | def __init__(self, message = ""): |
---|
| 30 | self.message = message |
---|
| 31 | Exception.__init__(self, message) |
---|
| 32 | def __repr__(self): |
---|
| 33 | return self.message |
---|
| 34 | __str__ = __repr__ |
---|
| 35 | |
---|
| 36 | class BaseReporter : |
---|
| 37 | """Base class for all reports.""" |
---|
| 38 | def __init__(self, tool, printers, ugnames, isgroup) : |
---|
| 39 | """Initialize local datas.""" |
---|
| 40 | self.tool = tool |
---|
| 41 | self.printers = printers |
---|
| 42 | self.ugnames = ugnames |
---|
| 43 | self.isgroup = isgroup |
---|
| 44 | |
---|
[1235] | 45 | def getPrinterTitle(self, printer) : |
---|
[1582] | 46 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
[1235] | 47 | |
---|
| 48 | def getPrinterGraceDelay(self, printer) : |
---|
| 49 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
| 50 | |
---|
| 51 | def getPrinterPrices(self, printer) : |
---|
| 52 | return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0)) |
---|
| 53 | |
---|
| 54 | def getReportHeader(self) : |
---|
| 55 | if self.isgroup : |
---|
[2054] | 56 | return _("Group overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 57 | else : |
---|
[2054] | 58 | return _("User overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 59 | |
---|
| 60 | def getPrinterRealPageCounter(self, 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) : |
---|
| 70 | return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11])) |
---|
| 71 | |
---|
| 72 | def getQuota(self, entry, quota) : |
---|
| 73 | """Prints the quota information.""" |
---|
| 74 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
| 75 | pagecounter = int(quota.PageCounter or 0) |
---|
| 76 | balance = float(entry.AccountBalance or 0.0) |
---|
| 77 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
[2054] | 78 | if not hasattr(entry, "OverCharge") : |
---|
| 79 | overcharge = _("N/A") # Not available for groups |
---|
| 80 | else : |
---|
| 81 | overcharge = float(entry.OverCharge or 0.0) |
---|
| 82 | if not hasattr(quota, "WarnCount") : |
---|
| 83 | warncount = _("N/A") # Not available for groups |
---|
| 84 | else : |
---|
| 85 | warncount = int(quota.WarnCount or 0) |
---|
[1582] | 86 | |
---|
[1418] | 87 | #balance |
---|
[1235] | 88 | if entry.LimitBy and (entry.LimitBy.lower() == "balance") : |
---|
[2064] | 89 | if balance == 0.0 : |
---|
| 90 | if entry.OverCharge > 0 : |
---|
| 91 | datelimit = "DENY" |
---|
| 92 | reached = "+B" |
---|
| 93 | else : |
---|
| 94 | # overcharging by a negative or nul factor means user is always allowed to print |
---|
| 95 | # TODO : do something when printer prices are negative as well ! |
---|
| 96 | datelimit = "" |
---|
| 97 | reached = "-B" |
---|
| 98 | elif balance < 0 : |
---|
[1235] | 99 | datelimit = "DENY" |
---|
| 100 | reached = "+B" |
---|
[1240] | 101 | elif balance <= self.tool.config.getPoorMan() : |
---|
| 102 | datelimit = "WARNING" |
---|
| 103 | reached = "?B" |
---|
[1235] | 104 | else : |
---|
| 105 | datelimit = "" |
---|
| 106 | reached = "-B" |
---|
[1418] | 107 | |
---|
| 108 | #balance-then-quota |
---|
| 109 | elif entry.LimitBy and (entry.LimitBy.lower() == "balance-then-quota") : |
---|
| 110 | if balance <= 0 : |
---|
| 111 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
| 112 | datelimit = "DENY" |
---|
| 113 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 114 | datelimit = "DENY" |
---|
| 115 | elif quota.DateLimit is not None : |
---|
| 116 | now = DateTime.now() |
---|
| 117 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
| 118 | if now >= datelimit : |
---|
| 119 | datelimit = "QUOTA_DENY" |
---|
| 120 | else : |
---|
| 121 | datelimit = "" |
---|
| 122 | reached = ( ((datelimit == "DENY" ) and "+B") or "-Q") |
---|
| 123 | datelimit = ( ((datelimit == "QUOTA_DENY") and "DENY") or datelimit) |
---|
| 124 | elif balance <= self.tool.config.getPoorMan() : |
---|
| 125 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
| 126 | datelimit = "WARNING" |
---|
| 127 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 128 | datelimit = "WARNING" |
---|
| 129 | elif quota.DateLimit is not None : |
---|
| 130 | now = DateTime.now() |
---|
| 131 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
| 132 | if now >= datelimit : |
---|
| 133 | datelimit = "QUOTA_DENY" |
---|
| 134 | else : |
---|
| 135 | datelimit = "" |
---|
| 136 | reached = ( ((datelimit == "WARNING" ) and "?B") or "+Q") |
---|
| 137 | datelimit = ( ((datelimit == "QUOTA_DENY") and "WARNING") or datelimit) |
---|
| 138 | else : |
---|
| 139 | datelimit = "" |
---|
| 140 | reached = "-B" |
---|
| 141 | |
---|
| 142 | #Quota-then-balance |
---|
| 143 | elif entry.LimitBy and (entry.LimitBy.lower() == "quota-then-balance") : |
---|
| 144 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
| 145 | datelimit = "DENY" |
---|
| 146 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 147 | datelimit = "DENY" |
---|
| 148 | elif quota.DateLimit is not None : |
---|
| 149 | now = DateTime.now() |
---|
| 150 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
| 151 | if now >= datelimit : |
---|
| 152 | datelimit = "DENY" |
---|
| 153 | else : |
---|
| 154 | datelimit = "" |
---|
| 155 | |
---|
| 156 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
| 157 | |
---|
| 158 | if (datelimit == "DENY") and (reached == "-Q") and (balance > self.tool.config.getPoorMan()) : |
---|
| 159 | datelimit = "" |
---|
| 160 | reached = "-B" |
---|
| 161 | else : |
---|
| 162 | reached = (((datelimit == "DENY") and (self.tool.config.getPoorMan() < balance ) and "-B") or reached) |
---|
| 163 | if (datelimit == "DENY") and (self.tool.config.getPoorMan() < balance) : |
---|
| 164 | datelimit = "" |
---|
| 165 | reached = (((datelimit == "DENY") and (0.0 < balance <= self.tool.config.getPoorMan()) and "?B") or reached) |
---|
| 166 | datelimit = (((datelimit == "DENY") and (0.0 < balance <= self.tool.config.getPoorMan()) and "WARNING") or datelimit) |
---|
| 167 | |
---|
| 168 | #Quota |
---|
[1235] | 169 | else : |
---|
[1240] | 170 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
| 171 | datelimit = "DENY" |
---|
| 172 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 173 | datelimit = "DENY" |
---|
| 174 | elif quota.DateLimit is not None : |
---|
[1235] | 175 | now = DateTime.now() |
---|
| 176 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
| 177 | if now >= datelimit : |
---|
| 178 | datelimit = "DENY" |
---|
| 179 | else : |
---|
| 180 | datelimit = "" |
---|
| 181 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
| 182 | |
---|
| 183 | strbalance = ("%5.2f" % balance)[:10] |
---|
| 184 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
[2054] | 185 | strovercharge = ("%5s" % overcharge)[:5] |
---|
| 186 | strwarncount = ("%4s" % warncount)[:4] |
---|
| 187 | return (lifepagecounter, lifetimepaid, entry.Name, reached, \ |
---|
| 188 | pagecounter, str(quota.SoftLimit), str(quota.HardLimit), \ |
---|
| 189 | strbalance, str(datelimit)[:10], lifepagecounter, \ |
---|
| 190 | strlifetimepaid, strovercharge, strwarncount) |
---|
[1235] | 191 | |
---|
[1048] | 192 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
| 193 | """Returns a reporter instance of the proper reporter.""" |
---|
| 194 | try : |
---|
[1219] | 195 | exec "from pykota.reporters import %s as reporterbackend" % reporttype.lower() |
---|
[1048] | 196 | except ImportError : |
---|
| 197 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
| 198 | else : |
---|
[1240] | 199 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|