root / pykota / trunk / pykota / reporter.py @ 1418

Revision 1418, 8.7 kB (checked in by jalet, 20 years ago)

Began integration of Henrik Janhagen's work on quota-then-balance
and balance-then-quota

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