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

Revision 3133, 7.9 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Rev
RevLine 
[2152]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#
[3133]8# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[2152]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
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[2152]22#
23# $Id$
24#
25#
26
27import sys
28import os
29import cgi
30import urllib
[2153]31import cStringIO
[2152]32
33from pykota import version
34from pykota.tool import PyKotaTool, PyKotaToolError
35from pykota.cgifuncs import getLanguagePreference, getCharsetPreference
[2909]36from pkpgpdls import analyzer, pdlparser
[2352]37   
38
[3055]39header = """Content-type: text/html;charset=%s
[2152]40
41<html>
42  <head>
43    <title>%s</title>
44    <link rel="stylesheet" type="text/css" href="/pykota.css" />
45  </head>
46  <body>
[3055]47    <!-- %s %s -->
[2265]48    <p>
49      <form action="pykotme.cgi" method="POST" enctype="multipart/form-data">
50        <table>
51          <tr>
52            <td>
53              <p>
54                <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a>
55                <br />
56                <a href="%s">PyKota v%s</a>
57              </p>
58            </td>
59            <td colspan="2">
60              <h1>%s</h1>
61            </td>
62          </tr>
63          <tr>
64            <td colspan="3" align="center">
65              <input type="submit" name="report" value="%s" />
66            </td>
67          </tr>
68        </table>"""
[2152]69   
70footer = """
[2265]71        <table>
72          <tr>
73            <td colspan="3" align="center">
74              <input type="submit" name="report" value="%s" />
75            </td>
76          </tr>
77        </table> 
78      </form>
79    </p>
80    <hr width="25%%" />
81    <p>
82      <font size="-2">
[2909]83        <a href="http://www.pykota.com/">%s</a>
[2267]84        &copy; %s %s
[2586]85        <br />
86        <pre>
87%s
88        </pre>
[2265]89      </font>
90    </p>
[2152]91  </body>
92</html>""" 
93
94class PyKotMeGUI(PyKotaTool) :
95    """PyKota Quote's Generator GUI"""
96    def guiDisplay(self) :
97        """Displays the administrative interface."""
98        global header, footer
[3055]99        print header % (self.charset, _("PyKota Quotes"), \
100                        self.language, self.charset, \
[2265]101                        self.config.getLogoLink(), \
102                        self.config.getLogoURL(), version.__version__, \
103                        self.config.getLogoLink(), \
104                        version.__version__, _("PyKota Quotes"), \
105                        _("Quote"))
[2152]106        print self.body
[2586]107        print footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__)
[2152]108       
109    def error(self, message) :
110        """Adds an error message to the GUI's body."""
111        if message :
112            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
113       
[2153]114    def htmlListPrinters(self, selected=[], mask="*") :   
115        """Displays the printers multiple selection list."""
116        printers = self.storage.getMatchingPrinters(mask)
117        selectednames = [p.Name for p in selected]
118        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer")
119        for printer in printers :
120            if printer.Name in selectednames :
121                message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
122            else :
123                message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
124        message += '</select></td></tr></table>'
125        return message
[2152]126       
127    def guiAction(self) :
128        """Main function"""
[2153]129        printers = inputfile = None
[2152]130        self.body = "<p>%s</p>\n" % _("Please click on the above button")
131        if self.form.has_key("report") :
132            if self.form.has_key("printers") :
133                printersfield = self.form["printers"]
134                if type(printersfield) != type([]) :
135                    printersfield = [ printersfield ]
136                printers = [self.storage.getPrinter(p.value) for p in printersfield]
137            else :   
138                printers = self.storage.getMatchingPrinters("*")
[2153]139            if self.form.has_key("inputfile") :   
140                inputfile = self.form["inputfile"].value
[2152]141               
[2381]142        if os.environ.get("REMOTE_USER") is not None :       
143            self.body += self.htmlListPrinters(printers or [])           
144            self.body += "<br />"
[2153]145        self.body += _("Filename") + " : "
146        self.body += '<input type="file" size="64" name="inputfile" />'
147        self.body += "<br />"
148        if inputfile :
149            try :
[2352]150                parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile))
[2153]151                jobsize = parser.getJobSize()
[2352]152            except pdlparser.PDLParserError, msg :   
[2229]153                self.body += '<p><font color="red">%s</font></p>' % msg
[2379]154                jobsize = 0 # unknown file format ?
[2153]155            else :   
[2229]156                self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize)
157               
[2525]158            remuser = os.environ.get("REMOTE_USER")
[2229]159            # special hack to accomodate mod_auth_ldap Apache module
160            try :
161                remuser = remuser.split("=")[1].split(",")[0]
[2525]162            except :   
[2229]163                pass
[2525]164            if not remuser :   
[2229]165                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.")
166            else :   
167                try :   
168                    user = self.storage.getUser(remuser)
[2232]169                    if user.Exists :
[2524]170                        if user.LimitBy == "noprint" :
171                            self.body += "<p>%s</p>" % _("Your account settings forbid you to print at this time.")
172                        else :   
173                            for printer in printers :
174                                upquota = self.storage.getUserPQuota(user, printer)
175                                if upquota.Exists :
176                                    if printer.MaxJobSize and (jobsize > printer.MaxJobSize) :
177                                        msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name
178                                    else :   
179                                        cost = upquota.computeJobPrice(jobsize)
180                                        msg = _("Cost on printer %s : %.2f") % (printer.Name, cost)
181                                        if printer.PassThrough :
182                                            msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode"))
183                                        elif user.LimitBy == "nochange" :   
184                                            msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable"))
185                                    self.body += "<p>%s</p>" % msg
[2229]186                except :
187                    self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />")
[2152]188           
189if __name__ == "__main__" :
[3055]190    admin = PyKotMeGUI(lang=getLanguagePreference(), charset=getCharsetPreference())
[2210]191    admin.deferredInit()
[2152]192    admin.form = cgi.FieldStorage()
193    admin.guiAction()
194    admin.guiDisplay()
195    try :
196        admin.storage.close()
197    except (TypeError, NameError, AttributeError) :   
198        pass
199       
200    sys.exit(0)
Note: See TracBrowser for help on using the browser.