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

Revision 2147, 9.0 kB (checked in by jerome, 19 years ago)

Removed all references to $Log$

  • 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#
24
25from mx import DateTime
26
27class 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   
36class 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       
45    def getPrinterTitle(self, printer) :     
46        return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description)
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 :
56            return _("Group          overcharge   used    soft    hard    balance grace         total       paid warn")
57        else :   
58            return _("User           overcharge   used    soft    hard    balance grace         total       paid warn")
59           
60    def getPrinterRealPageCounter(self, printer) :       
61        msg = _("unknown")
62        if printer.LastJob.Exists :
63            try :
64                msg = "%9i" % printer.LastJob.PrinterPageCounter
65            except TypeError :     
66                pass
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)
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)
86       
87        #balance
88        if entry.LimitBy and (entry.LimitBy.lower() == "balance") :   
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 :
99                datelimit = "DENY"
100                reached = "+B"
101            elif balance <= self.tool.config.getPoorMan() :
102                datelimit = "WARNING"
103                reached = "?B"
104            else :   
105                datelimit = ""
106                reached = "-B"
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
169        else :
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 :
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]
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)
191       
192def openReporter(tool, reporttype, printers, ugnames, isgroup) :
193    """Returns a reporter instance of the proper reporter."""
194    try :
195        exec "from pykota.reporters import %s as reporterbackend" % reporttype.lower()
196    except ImportError :
197        raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype
198    else :   
199        return reporterbackend.Reporter(tool, printers, ugnames, isgroup)
Note: See TracBrowser for help on using the browser.