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

Revision 1257, 5.3 kB (checked in by jalet, 20 years ago)

Copyright year changed.

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