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

Revision 1235, 5.1 kB (checked in by jalet, 20 years ago)

Some code refactoring.
New HTML reporter added, which is now used in the CGI script for web based
print quota reports. It will need some de-uglyfication though...

  • 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 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.4  2003/12/02 14:40:21  jalet
25# Some code refactoring.
26# New HTML reporter added, which is now used in the CGI script for web based
27# print quota reports. It will need some de-uglyfication though...
28#
29# Revision 1.3  2003/11/25 23:46:40  jalet
30# Don't try to verify if module name is valid, Python does this better than us.
31#
32# Revision 1.2  2003/10/07 09:07:28  jalet
33# Character encoding added to please latest version of Python
34#
35# Revision 1.1  2003/06/30 12:46:15  jalet
36# Extracted reporting code.
37#
38#
39#
40
41class PyKotaReporterError(Exception):
42    """An exception for Reporter related stuff."""
43    def __init__(self, message = ""):
44        self.message = message
45        Exception.__init__(self, message)
46    def __repr__(self):
47        return self.message
48    __str__ = __repr__
49   
50class BaseReporter :   
51    """Base class for all reports."""
52    def __init__(self, tool, printers, ugnames, isgroup) :
53        """Initialize local datas."""
54        self.tool = tool
55        self.printers = printers
56        self.ugnames = ugnames
57        self.isgroup = isgroup
58       
59    def getPrinterTitle(self, printer) :     
60        return _("*** Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)
61       
62    def getPrinterGraceDelay(self, printer) :   
63        return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name)
64       
65    def getPrinterPrices(self, printer) :   
66        return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0))
67           
68    def getReportHeader(self) :       
69        if self.isgroup :
70            return _("Group           used    soft    hard    balance grace         total       paid")
71        else :   
72            return _("User            used    soft    hard    balance grace         total       paid")
73           
74    def getPrinterRealPageCounter(self, printer) :       
75        try :
76            msg = "%9i" % printer.LastJob.PrinterPageCounter
77        except TypeError :     
78            msg = _("unknown")
79        return _("Real : %s") % msg
80               
81    def getTotals(self, total, totalmoney) :           
82        return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11]))
83           
84    def getQuota(self, entry, quota) :
85        """Prints the quota information."""
86        lifepagecounter = int(quota.LifePageCounter or 0)
87        pagecounter = int(quota.PageCounter or 0)
88        balance = float(entry.AccountBalance or 0.0)
89        lifetimepaid = float(entry.LifeTimePaid or 0.0)
90       
91        if entry.LimitBy and (entry.LimitBy.lower() == "balance") :   
92            if balance <= 0 :
93                datelimit = "DENY"
94                reached = "+B"
95            else :   
96                datelimit = ""
97                reached = "-B"
98        else :
99            if quota.DateLimit is not None :
100                now = DateTime.now()
101                datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit)
102                if now >= datelimit :
103                    datelimit = "DENY"
104            elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) :   
105                datelimit = "DENY"
106            elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) :
107                datelimit = "DENY"
108            else :   
109                datelimit = ""
110            reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q"
111           
112        strbalance = ("%5.2f" % balance)[:10]
113        strlifetimepaid = ("%6.2f" % lifetimepaid)[:10]
114        return (lifepagecounter, lifetimepaid, entry.Name, reached, pagecounter, str(quota.SoftLimit), str(quota.HardLimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid)
115       
116def openReporter(tool, reporttype, printers, ugnames, isgroup) :
117    """Returns a reporter instance of the proper reporter."""
118    try :
119        exec "from pykota.reporters import %s as reporterbackend" % reporttype.lower()
120    except ImportError :
121        raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype
122    else :   
123        return getattr(reporterbackend, "Reporter")(tool, printers, ugnames, isgroup)
Note: See TracBrowser for help on using the browser.