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

Revision 1056, 5.2 kB (checked in by jalet, 21 years ago)

Bug fixed when wanting a report and an user/group was limited by account balance

  • 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.2  2003/07/02 09:29:12  jalet
24# Bug fixed when wanting a report and an user/group was limited by account balance
25#
26# Revision 1.1  2003/06/30 12:46:15  jalet
27# Extracted reporting code.
28#
29#
30#
31
32import sys
33
34from mx import DateTime
35
36from pykota.reporter import BaseReporter, PyKotaReporterError
37   
38class Reporter(BaseReporter) :   
39    """Base class for all PyKota command line tools."""
40    def generateReport(self) :
41        """Produces a simple text report."""
42        self.report = []
43        for printer in self.printers :
44            self.report.append(_("*** Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name))
45            self.report.append(_("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name))
46            if printer.PricePerJob is not None :
47                self.report.append(_("Price per job: %.3f") % printer.PricePerJob)
48            if printer.PricePerPage is not None :   
49                self.report.append(_("Price per page: %.3f") % printer.PricePerPage)
50            total = 0
51            totalmoney = 0.0
52            if self.isgroup :
53                self.report.append(_("Group           used    soft    hard    balance grace         total       paid"))
54                self.report.append("------------------------------------------------------------------------------")
55                for (group, grouppquota) in self.tool.storage.getPrinterGroupsAndQuotas(printer, self.ugnames) :
56                    (pages, money) = self.printQuota(group, grouppquota)
57                    total += pages
58                    totalmoney += money
59            else :
60                # default is user quota report
61                self.report.append(_("User            used    soft    hard    balance grace         total       paid"))
62                self.report.append("------------------------------------------------------------------------------")
63                for (user, userpquota) in self.tool.storage.getPrinterUsersAndQuotas(printer, self.ugnames) :
64                    (pages, money) = self.printQuota(user, userpquota)
65                    total += pages
66                    totalmoney += money
67            if total or totalmoney :       
68                self.report.append((" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11]))
69            try :
70                msg = "%9i" % printer.LastJob.PrinterPageCounter
71            except TypeError :     
72                msg = _("unknown")
73            self.report.append((" " * 51) + (_("Real : %s") % msg))
74            self.report.append("")       
75        if self.isgroup :   
76            self.report.append(_("Totals may be inaccurate if some users are members of several groups."))
77        return "\n".join(self.report)   
78                       
79    def printQuota(self, entry, quota) :
80        """Prints the quota information."""
81        lifepagecounter = int(quota.LifePageCounter or 0)
82        pagecounter = int(quota.PageCounter or 0)
83        balance = float(entry.AccountBalance or 0.0)
84        lifetimepaid = float(entry.LifeTimePaid or 0.0)
85       
86        if entry.LimitBy.lower() == "balance" :   
87            if balance <= 0 :
88                datelimit = "DENY"
89                reached = "+B"
90            else :   
91                datelimit = ""
92                reached = "-B"
93        else :
94            if quota.DateLimit is not None :
95                now = DateTime.now()
96                datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit)
97                if now >= datelimit :
98                    datelimit = "DENY"
99            elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) :   
100                datelimit = "DENY"
101            elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) :
102                datelimit = "DENY"
103            else :   
104                datelimit = ""
105            reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q"
106           
107        strbalance = ("%5.2f" % balance)[:10]
108        strlifetimepaid = ("%6.2f" % lifetimepaid)[:10]
109        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))
110        return (lifepagecounter, lifetimepaid)
111   
Note: See TracBrowser for help on using the browser.