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

Revision 1582, 8.9 kB (checked in by jalet, 20 years ago)

Added code to handle the description field for printers

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