root / pykota / trunk / cgi-bin / pykotme.cgi @ 2720

Revision 2622, 8.1 kB (checked in by jerome, 19 years ago)

Added 2006 to the copyright's years.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Rev
Line 
1#! /usr/bin/python
2# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quotes generator
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import cgi
30import urllib
31import cStringIO
32
33from pykota import version
34from pykota.tool import PyKotaTool, PyKotaToolError
35from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
36
37try :
38    from pkpgpdls import analyzer, pdlparser
39except ImportError : # TODO : Remove the try/except after release 1.24.
40    sys.stderr.write("ERROR: pkpgcounter is now distributed separately, please grab it from http://www.librelogiciel.com/software/pkpgcounter/action_Download\n")
41   
42
43header = """Content-type: text/html
44
45<?xml version="1.0" encoding="%s"?>
46<html>
47  <head>
48    <title>%s</title>
49    <link rel="stylesheet" type="text/css" href="/pykota.css" />
50  </head>
51  <body>
52    <p>
53      <form action="pykotme.cgi" method="POST" enctype="multipart/form-data">
54        <table>
55          <tr>
56            <td>
57              <p>
58                <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a>
59                <br />
60                <a href="%s">PyKota v%s</a>
61              </p>
62            </td>
63            <td colspan="2">
64              <h1>%s</h1>
65            </td>
66          </tr>
67          <tr>
68            <td colspan="3" align="center">
69              <input type="submit" name="report" value="%s" />
70            </td>
71          </tr>
72        </table>"""
73   
74footer = """
75        <table>
76          <tr>
77            <td colspan="3" align="center">
78              <input type="submit" name="report" value="%s" />
79            </td>
80          </tr>
81        </table> 
82      </form>
83    </p>
84    <hr width="25%%" />
85    <p>
86      <font size="-2">
87        <a href="http://www.librelogiciel.com/software/">%s</a>
88        &copy; %s %s
89        <br />
90        <pre>
91%s
92        </pre>
93      </font>
94    </p>
95  </body>
96</html>""" 
97
98class PyKotMeGUI(PyKotaTool) :
99    """PyKota Quote's Generator GUI"""
100    def guiDisplay(self) :
101        """Displays the administrative interface."""
102        global header, footer
103        print header % (self.getCharset(), _("PyKota Quotes"), \
104                        self.config.getLogoLink(), \
105                        self.config.getLogoURL(), version.__version__, \
106                        self.config.getLogoLink(), \
107                        version.__version__, _("PyKota Quotes"), \
108                        _("Quote"))
109        print self.body
110        print footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__)
111       
112    def error(self, message) :
113        """Adds an error message to the GUI's body."""
114        if message :
115            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
116       
117    def htmlListPrinters(self, selected=[], mask="*") :   
118        """Displays the printers multiple selection list."""
119        printers = self.storage.getMatchingPrinters(mask)
120        selectednames = [p.Name for p in selected]
121        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer")
122        for printer in printers :
123            if printer.Name in selectednames :
124                message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
125            else :
126                message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
127        message += '</select></td></tr></table>'
128        return message
129       
130    def guiAction(self) :
131        """Main function"""
132        printers = inputfile = None
133        self.body = "<p>%s</p>\n" % _("Please click on the above button")
134        if self.form.has_key("report") :
135            if self.form.has_key("printers") :
136                printersfield = self.form["printers"]
137                if type(printersfield) != type([]) :
138                    printersfield = [ printersfield ]
139                printers = [self.storage.getPrinter(p.value) for p in printersfield]
140            else :   
141                printers = self.storage.getMatchingPrinters("*")
142            if self.form.has_key("inputfile") :   
143                inputfile = self.form["inputfile"].value
144               
145        if os.environ.get("REMOTE_USER") is not None :       
146            self.body += self.htmlListPrinters(printers or [])           
147            self.body += "<br />"
148        self.body += _("Filename") + " : "
149        self.body += '<input type="file" size="64" name="inputfile" />'
150        self.body += "<br />"
151        if inputfile :
152            try :
153                parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile))
154                jobsize = parser.getJobSize()
155            except pdlparser.PDLParserError, msg :   
156                self.body += '<p><font color="red">%s</font></p>' % msg
157                jobsize = 0 # unknown file format ?
158            else :   
159                self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize)
160               
161            remuser = os.environ.get("REMOTE_USER")
162            # special hack to accomodate mod_auth_ldap Apache module
163            try :
164                remuser = remuser.split("=")[1].split(",")[0]
165            except :   
166                pass
167            if not remuser :   
168                self.body += "<p>%s</p>" % _("The exact cost of a print job can only be determined for a particular user. Please retry while logged-in.")
169            else :   
170                try :   
171                    user = self.storage.getUser(remuser)
172                    if user.Exists :
173                        if user.LimitBy == "noprint" :
174                            self.body += "<p>%s</p>" % _("Your account settings forbid you to print at this time.")
175                        else :   
176                            for printer in printers :
177                                upquota = self.storage.getUserPQuota(user, printer)
178                                if upquota.Exists :
179                                    if printer.MaxJobSize and (jobsize > printer.MaxJobSize) :
180                                        msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name
181                                    else :   
182                                        cost = upquota.computeJobPrice(jobsize)
183                                        msg = _("Cost on printer %s : %.2f") % (printer.Name, cost)
184                                        if printer.PassThrough :
185                                            msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode"))
186                                        elif user.LimitBy == "nochange" :   
187                                            msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable"))
188                                    self.body += "<p>%s</p>" % msg
189                except :
190                    self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />")
191           
192if __name__ == "__main__" :
193    os.environ["LC_ALL"] = getLanguagePreference()
194    admin = PyKotMeGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference())
195    admin.deferredInit()
196    admin.form = cgi.FieldStorage()
197    admin.guiAction()
198    admin.guiDisplay()
199    try :
200        admin.storage.close()
201    except (TypeError, NameError, AttributeError) :   
202        pass
203       
204    sys.exit(0)
Note: See TracBrowser for help on using the browser.