1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
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 | # |
---|
23 | # $Log$ |
---|
24 | # Revision 1.10 2004/09/02 10:09:30 jalet |
---|
25 | # Fixed bug in LDAP user deletion code which didn't correctly delete the user's |
---|
26 | # pykotaLastJob entries. |
---|
27 | # |
---|
28 | # Revision 1.9 2004/07/01 17:45:49 jalet |
---|
29 | # Added code to handle the description field for printers |
---|
30 | # |
---|
31 | # Revision 1.8 2004/03/24 15:15:24 jalet |
---|
32 | # Began integration of Henrik Janhagen's work on quota-then-balance |
---|
33 | # and balance-then-quota |
---|
34 | # |
---|
35 | # Revision 1.7 2004/01/08 14:10:32 jalet |
---|
36 | # Copyright year changed. |
---|
37 | # |
---|
38 | # Revision 1.6 2003/12/27 16:49:25 uid67467 |
---|
39 | # Should be ok now. |
---|
40 | # |
---|
41 | # Revision 1.4 2003/12/02 14:40:21 jalet |
---|
42 | # Some code refactoring. |
---|
43 | # New HTML reporter added, which is now used in the CGI script for web based |
---|
44 | # print quota reports. It will need some de-uglyfication though... |
---|
45 | # |
---|
46 | # Revision 1.3 2003/11/25 23:46:40 jalet |
---|
47 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
48 | # |
---|
49 | # Revision 1.2 2003/10/07 09:07:28 jalet |
---|
50 | # Character encoding added to please latest version of Python |
---|
51 | # |
---|
52 | # Revision 1.1 2003/06/30 12:46:15 jalet |
---|
53 | # Extracted reporting code. |
---|
54 | # |
---|
55 | # |
---|
56 | # |
---|
57 | |
---|
58 | from mx import DateTime |
---|
59 | |
---|
60 | class PyKotaReporterError(Exception): |
---|
61 | """An exception for Reporter related stuff.""" |
---|
62 | def __init__(self, message = ""): |
---|
63 | self.message = message |
---|
64 | Exception.__init__(self, message) |
---|
65 | def __repr__(self): |
---|
66 | return self.message |
---|
67 | __str__ = __repr__ |
---|
68 | |
---|
69 | class BaseReporter : |
---|
70 | """Base class for all reports.""" |
---|
71 | def __init__(self, tool, printers, ugnames, isgroup) : |
---|
72 | """Initialize local datas.""" |
---|
73 | self.tool = tool |
---|
74 | self.printers = printers |
---|
75 | self.ugnames = ugnames |
---|
76 | self.isgroup = isgroup |
---|
77 | |
---|
78 | def getPrinterTitle(self, printer) : |
---|
79 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
80 | |
---|
81 | def getPrinterGraceDelay(self, printer) : |
---|
82 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
83 | |
---|
84 | def getPrinterPrices(self, printer) : |
---|
85 | return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0)) |
---|
86 | |
---|
87 | def getReportHeader(self) : |
---|
88 | if self.isgroup : |
---|
89 | return _("Group used soft hard balance grace total paid") |
---|
90 | else : |
---|
91 | return _("User used soft hard balance grace total paid") |
---|
92 | |
---|
93 | def getPrinterRealPageCounter(self, printer) : |
---|
94 | msg = _("unknown") |
---|
95 | if printer.LastJob.Exists : |
---|
96 | try : |
---|
97 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
98 | except TypeError : |
---|
99 | pass |
---|
100 | return _("Real : %s") % msg |
---|
101 | |
---|
102 | def getTotals(self, total, totalmoney) : |
---|
103 | return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11])) |
---|
104 | |
---|
105 | def getQuota(self, entry, quota) : |
---|
106 | """Prints the quota information.""" |
---|
107 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
108 | pagecounter = int(quota.PageCounter or 0) |
---|
109 | balance = float(entry.AccountBalance or 0.0) |
---|
110 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
111 | |
---|
112 | #balance |
---|
113 | if entry.LimitBy and (entry.LimitBy.lower() == "balance") : |
---|
114 | if balance <= 0 : |
---|
115 | datelimit = "DENY" |
---|
116 | reached = "+B" |
---|
117 | elif balance <= self.tool.config.getPoorMan() : |
---|
118 | datelimit = "WARNING" |
---|
119 | reached = "?B" |
---|
120 | else : |
---|
121 | datelimit = "" |
---|
122 | reached = "-B" |
---|
123 | |
---|
124 | #balance-then-quota |
---|
125 | elif entry.LimitBy and (entry.LimitBy.lower() == "balance-then-quota") : |
---|
126 | if balance <= 0 : |
---|
127 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
128 | datelimit = "DENY" |
---|
129 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
130 | datelimit = "DENY" |
---|
131 | elif quota.DateLimit is not None : |
---|
132 | now = DateTime.now() |
---|
133 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
134 | if now >= datelimit : |
---|
135 | datelimit = "QUOTA_DENY" |
---|
136 | else : |
---|
137 | datelimit = "" |
---|
138 | reached = ( ((datelimit == "DENY" ) and "+B") or "-Q") |
---|
139 | datelimit = ( ((datelimit == "QUOTA_DENY") and "DENY") or datelimit) |
---|
140 | elif balance <= self.tool.config.getPoorMan() : |
---|
141 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
142 | datelimit = "WARNING" |
---|
143 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
144 | datelimit = "WARNING" |
---|
145 | elif quota.DateLimit is not None : |
---|
146 | now = DateTime.now() |
---|
147 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
148 | if now >= datelimit : |
---|
149 | datelimit = "QUOTA_DENY" |
---|
150 | else : |
---|
151 | datelimit = "" |
---|
152 | reached = ( ((datelimit == "WARNING" ) and "?B") or "+Q") |
---|
153 | datelimit = ( ((datelimit == "QUOTA_DENY") and "WARNING") or datelimit) |
---|
154 | else : |
---|
155 | datelimit = "" |
---|
156 | reached = "-B" |
---|
157 | |
---|
158 | #Quota-then-balance |
---|
159 | elif entry.LimitBy and (entry.LimitBy.lower() == "quota-then-balance") : |
---|
160 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
161 | datelimit = "DENY" |
---|
162 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
163 | datelimit = "DENY" |
---|
164 | elif quota.DateLimit is not None : |
---|
165 | now = DateTime.now() |
---|
166 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
167 | if now >= datelimit : |
---|
168 | datelimit = "DENY" |
---|
169 | else : |
---|
170 | datelimit = "" |
---|
171 | |
---|
172 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
173 | |
---|
174 | if (datelimit == "DENY") and (reached == "-Q") and (balance > self.tool.config.getPoorMan()) : |
---|
175 | datelimit = "" |
---|
176 | reached = "-B" |
---|
177 | else : |
---|
178 | reached = (((datelimit == "DENY") and (self.tool.config.getPoorMan() < balance ) and "-B") or reached) |
---|
179 | if (datelimit == "DENY") and (self.tool.config.getPoorMan() < balance) : |
---|
180 | datelimit = "" |
---|
181 | reached = (((datelimit == "DENY") and (0.0 < balance <= self.tool.config.getPoorMan()) and "?B") or reached) |
---|
182 | datelimit = (((datelimit == "DENY") and (0.0 < balance <= self.tool.config.getPoorMan()) and "WARNING") or datelimit) |
---|
183 | |
---|
184 | #Quota |
---|
185 | else : |
---|
186 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
187 | datelimit = "DENY" |
---|
188 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
189 | datelimit = "DENY" |
---|
190 | elif quota.DateLimit is not None : |
---|
191 | now = DateTime.now() |
---|
192 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
193 | if now >= datelimit : |
---|
194 | datelimit = "DENY" |
---|
195 | else : |
---|
196 | datelimit = "" |
---|
197 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
198 | |
---|
199 | strbalance = ("%5.2f" % balance)[:10] |
---|
200 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
201 | return (lifepagecounter, lifetimepaid, entry.Name, reached, pagecounter, str(quota.SoftLimit), str(quota.HardLimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid) |
---|
202 | |
---|
203 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
204 | """Returns a reporter instance of the proper reporter.""" |
---|
205 | try : |
---|
206 | exec "from pykota.reporters import %s as reporterbackend" % reporttype.lower() |
---|
207 | except ImportError : |
---|
208 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
209 | else : |
---|
210 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|