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

Revision 1239, 5.1 kB (checked in by uid67467, 20 years ago)

Savannah is back online...

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