root / pykota / trunk / pykota / reporters / text.py @ 1062

Revision 1062, 5.3 kB (checked in by jalet, 21 years ago)

The previous bug fix was incomplete.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
22# $Log$
23# Revision 1.3  2003/07/05 07:46:50  jalet
24# The previous bug fix was incomplete.
25#
26# Revision 1.2  2003/07/02 09:29:12  jalet
27# Bug fixed when wanting a report and an user/group was limited by account balance
28#
29# Revision 1.1  2003/06/30 12:46:15  jalet
30# Extracted reporting code.
31#
32#
33#
34
35import sys
36
37from mx import DateTime
38
39from pykota.reporter import BaseReporter, PyKotaReporterError
40   
41class Reporter(BaseReporter) :   
42    """Base class for all PyKota command line tools."""
43    def generateReport(self) :
44        """Produces a simple text report."""
45        self.report = []
46        for printer in self.printers :
47            self.report.append(_("*** Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name))
48            self.report.append(_("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name))
49            if printer.PricePerJob is not None :
50                self.report.append(_("Price per job: %.3f") % printer.PricePerJob)
51            if printer.PricePerPage is not None :   
52                self.report.append(_("Price per page: %.3f") % printer.PricePerPage)
53            total = 0
54            totalmoney = 0.0
55            if self.isgroup :
56                self.report.append(_("Group           used    soft    hard    balance grace         total       paid"))
57                self.report.append("------------------------------------------------------------------------------")
58                for (group, grouppquota) in self.tool.storage.getPrinterGroupsAndQuotas(printer, self.ugnames) :
59                    (pages, money) = self.printQuota(group, grouppquota)
60                    total += pages
61                    totalmoney += money
62            else :
63                # default is user quota report
64                self.report.append(_("User            used    soft    hard    balance grace         total       paid"))
65                self.report.append("------------------------------------------------------------------------------")
66                for (user, userpquota) in self.tool.storage.getPrinterUsersAndQuotas(printer, self.ugnames) :
67                    (pages, money) = self.printQuota(user, userpquota)
68                    total += pages
69                    totalmoney += money
70            if total or totalmoney :       
71                self.report.append((" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11]))
72            try :
73                msg = "%9i" % printer.LastJob.PrinterPageCounter
74            except TypeError :     
75                msg = _("unknown")
76            self.report.append((" " * 51) + (_("Real : %s") % msg))
77            self.report.append("")       
78        if self.isgroup :   
79            self.report.append(_("Totals may be inaccurate if some users are members of several groups."))
80        return "\n".join(self.report)   
81                       
82    def printQuota(self, entry, quota) :
83        """Prints the quota information."""
84        lifepagecounter = int(quota.LifePageCounter or 0)
85        pagecounter = int(quota.PageCounter or 0)
86        balance = float(entry.AccountBalance or 0.0)
87        lifetimepaid = float(entry.LifeTimePaid or 0.0)
88       
89        if entry.LimitBy and (entry.LimitBy.lower() == "balance") :   
90            if balance <= 0 :
91                datelimit = "DENY"
92                reached = "+B"
93            else :   
94                datelimit = ""
95                reached = "-B"
96        else :
97            if quota.DateLimit is not None :
98                now = DateTime.now()
99                datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit)
100                if now >= datelimit :
101                    datelimit = "DENY"
102            elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) :   
103                datelimit = "DENY"
104            elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) :
105                datelimit = "DENY"
106            else :   
107                datelimit = ""
108            reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q"
109           
110        strbalance = ("%5.2f" % balance)[:10]
111        strlifetimepaid = ("%6.2f" % lifetimepaid)[:10]
112        self.report.append("%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (entry.Name, reached, pagecounter, str(quota.SoftLimit), str(quota.HardLimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid))
113        return (lifepagecounter, lifetimepaid)
114   
Note: See TracBrowser for help on using the browser.