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

Revision 2879, 6.3 kB (checked in by jerome, 18 years ago)

Fixes a datetime encoding problem.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1048]1# PyKota
[1144]2# -*- coding: ISO-8859-15 -*-
[1048]3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
[2622]6# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
[1048]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
[2302]19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[1048]20#
21# $Id$
22#
[2064]23#
[1048]24
[1240]25from mx import DateTime
26
[1048]27class PyKotaReporterError(Exception):
28    """An exception for Reporter related stuff."""
29    def __init__(self, message = ""):
30        self.message = message
31        Exception.__init__(self, message)
32    def __repr__(self):
33        return self.message
34    __str__ = __repr__
35   
36class BaseReporter :   
37    """Base class for all reports."""
38    def __init__(self, tool, printers, ugnames, isgroup) :
39        """Initialize local datas."""
40        self.tool = tool
41        self.printers = printers
42        self.ugnames = ugnames
43        self.isgroup = isgroup
44       
[1235]45    def getPrinterTitle(self, printer) :     
[1582]46        return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description)
[1235]47       
48    def getPrinterGraceDelay(self, printer) :   
49        return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name)
50       
51    def getPrinterPrices(self, printer) :   
52        return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0))
53           
54    def getReportHeader(self) :       
55        if self.isgroup :
[2054]56            return _("Group          overcharge   used    soft    hard    balance grace         total       paid warn")
[1235]57        else :   
[2054]58            return _("User           overcharge   used    soft    hard    balance grace         total       paid warn")
[1235]59           
60    def getPrinterRealPageCounter(self, printer) :       
[1692]61        msg = _("unknown")
62        if printer.LastJob.Exists :
63            try :
64                msg = "%9i" % printer.LastJob.PrinterPageCounter
65            except TypeError :     
66                pass
[1235]67        return _("Real : %s") % msg
68               
69    def getTotals(self, total, totalmoney) :           
70        return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11]))
71           
72    def getQuota(self, entry, quota) :
73        """Prints the quota information."""
74        lifepagecounter = int(quota.LifePageCounter or 0)
75        pagecounter = int(quota.PageCounter or 0)
76        balance = float(entry.AccountBalance or 0.0)
77        lifetimepaid = float(entry.LifeTimePaid or 0.0)
[2054]78        if not hasattr(entry, "OverCharge") :
79            overcharge = _("N/A")       # Not available for groups
80        else :   
81            overcharge = float(entry.OverCharge or 0.0)
82        if not hasattr(quota, "WarnCount") :   
83            warncount = _("N/A")        # Not available for groups
84        else :   
85            warncount = int(quota.WarnCount or 0)
[1582]86       
[2452]87        if (not entry.LimitBy) or (entry.LimitBy.lower() == "quota") :
88            if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) :   
[1235]89                datelimit = "DENY"
[1418]90            elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) :
91                datelimit = "DENY"
92            elif quota.DateLimit is not None :
93                now = DateTime.now()
[2879]94                datelimit = DateTime.ISO.ParseDateTime(str(quota.DateLimit))
[1418]95                if now >= datelimit :
96                    datelimit = "DENY"
[2452]97            else :   
[1418]98                datelimit = ""
99            reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q"
[1235]100        else :
[2452]101            if entry.LimitBy.lower() == "balance" :
[2692]102                balancezero = self.tool.config.getBalanceZero()
103                if balance == balancezero :
[2452]104                    if entry.OverCharge > 0 :
105                        datelimit = "DENY"
106                        reached = "+B"
107                    else :   
108                        # overcharging by a negative or nul factor means user is always allowed to print
109                        # TODO : do something when printer prices are negative as well !
110                        datelimit = ""
111                        reached = "-B"
[2692]112                elif balance < balancezero :
[1235]113                    datelimit = "DENY"
[2452]114                    reached = "+B"
115                elif balance <= self.tool.config.getPoorMan() :
116                    datelimit = "WARNING"
117                    reached = "?B"
118                else :   
119                    datelimit = ""
120                    reached = "-B"
121            elif entry.LimitBy.lower() == "noquota" :
122                reached = "NQ"
[1235]123                datelimit = ""
[2452]124            elif entry.LimitBy.lower() == "nochange" :
125                reached = "NC"
126                datelimit = ""
127            else :
128                # noprint
129                reached = "NP"
130                datelimit = "DENY"
[1235]131           
132        strbalance = ("%5.2f" % balance)[:10]
133        strlifetimepaid = ("%6.2f" % lifetimepaid)[:10]
[2054]134        strovercharge = ("%5s" % overcharge)[:5]
135        strwarncount = ("%4s" % warncount)[:4]
136        return (lifepagecounter, lifetimepaid, entry.Name, reached, \
137                pagecounter, str(quota.SoftLimit), str(quota.HardLimit), \
138                strbalance, str(datelimit)[:10], lifepagecounter, \
139                strlifetimepaid, strovercharge, strwarncount)
[1235]140       
[1048]141def openReporter(tool, reporttype, printers, ugnames, isgroup) :
142    """Returns a reporter instance of the proper reporter."""
143    try :
[1219]144        exec "from pykota.reporters import %s as reporterbackend" % reporttype.lower()
[1048]145    except ImportError :
146        raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype
147    else :   
[1240]148        return reporterbackend.Reporter(tool, printers, ugnames, isgroup)
Note: See TracBrowser for help on using the browser.