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

Revision 3561, 7.9 kB (checked in by jerome, 11 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
[3489]2# -*- coding: utf-8 -*-
[2152]3
4# PyKota Print Quotes generator
5#
[3259]6# PyKota - Print Quotas for CUPS
[2152]7#
[3561]8# (c) 2003-2013 Jerome Alet <alet@librelogiciel.com>
[3259]9# This program is free software: you can redistribute it and/or modify
[2152]10# it under the terms of the GNU General Public License as published by
[3259]11# the Free Software Foundation, either version 3 of the License, or
[2152]12# (at your option) any later version.
[3413]13#
[2152]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.
[3413]18#
[2152]19# You should have received a copy of the GNU General Public License
[3259]20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[2152]21#
22# $Id$
23#
24#
25
26import sys
27import os
28import cgi
29import urllib
[2153]30import cStringIO
[3333]31from xml.sax import saxutils
[2152]32
[3295]33import pykota.appinit
34
35from pykota import version, utils
36from pykota.tool import PyKotaTool
37from pykota.errors import PyKotaToolError
38
[2909]39from pkpgpdls import analyzer, pdlparser
[2352]40
[3413]41
[3055]42header = """Content-type: text/html;charset=%s
[2152]43
44<html>
45  <head>
46    <title>%s</title>
47    <link rel="stylesheet" type="text/css" href="/pykota.css" />
48  </head>
49  <body>
[2265]50    <p>
51      <form action="pykotme.cgi" method="POST" enctype="multipart/form-data">
52        <table>
53          <tr>
54            <td>
55              <p>
56                <a href="%s"><img src="%s?version=%s" alt="PyKota's Logo" /></a>
57                <br />
58                <a href="%s">PyKota v%s</a>
59              </p>
60            </td>
61            <td colspan="2">
62              <h1>%s</h1>
63            </td>
64          </tr>
65          <tr>
66            <td colspan="3" align="center">
67              <input type="submit" name="report" value="%s" />
68            </td>
69          </tr>
70        </table>"""
[3413]71
[2152]72footer = """
[2265]73        <table>
74          <tr>
75            <td colspan="3" align="center">
76              <input type="submit" name="report" value="%s" />
77            </td>
78          </tr>
[3413]79        </table>
[2265]80      </form>
81    </p>
82    <hr width="25%%" />
83    <p>
84      <font size="-2">
[2909]85        <a href="http://www.pykota.com/">%s</a>
[3413]86        &copy; %s %s
[2586]87        <br />
88        <pre>
89%s
90        </pre>
[2265]91      </font>
92    </p>
[2152]93  </body>
[3413]94</html>"""
[2152]95
96class PyKotMeGUI(PyKotaTool) :
97    """PyKota Quote's Generator GUI"""
98    def guiDisplay(self) :
99        """Displays the administrative interface."""
100        global header, footer
[3295]101        content = [ header % (self.charset, _("PyKota Quotes"), \
[2265]102                        self.config.getLogoLink(), \
103                        self.config.getLogoURL(), version.__version__, \
104                        self.config.getLogoLink(), \
105                        version.__version__, _("PyKota Quotes"), \
[3295]106                        _("Quote")) ]
107        content.append(self.body)
[3413]108        content.append(footer % (_("Quote"),
109                                 version.__doc__,
110                                 version.__years__,
111                                 version.__author__,
[3333]112                                 saxutils.escape(version.__gplblurb__)))
[3295]113        for c in content :
114            sys.stdout.write(c.encode(self.charset, "replace"))
115        sys.stdout.flush()
[3413]116
[2152]117    def error(self, message) :
118        """Adds an error message to the GUI's body."""
119        if message :
120            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body)
[3413]121
122    def htmlListPrinters(self, selected=[], mask="*") :
[2153]123        """Displays the printers multiple selection list."""
124        printers = self.storage.getMatchingPrinters(mask)
125        selectednames = [p.Name for p in selected]
126        message = '<table><tr><td valign="top">%s :</td><td valign="top"><select name="printers" multiple="multiple">' % _("Printer")
127        for printer in printers :
128            if printer.Name in selectednames :
129                message += '<option value="%s" selected="selected">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
130            else :
131                message += '<option value="%s">%s (%s)</option>' % (printer.Name, printer.Name, printer.Description)
132        message += '</select></td></tr></table>'
133        return message
[3413]134
[2152]135    def guiAction(self) :
136        """Main function"""
[2153]137        printers = inputfile = None
[2152]138        self.body = "<p>%s</p>\n" % _("Please click on the above button")
139        if self.form.has_key("report") :
140            if self.form.has_key("printers") :
141                printersfield = self.form["printers"]
142                if type(printersfield) != type([]) :
143                    printersfield = [ printersfield ]
144                printers = [self.storage.getPrinter(p.value) for p in printersfield]
[3413]145            else :
[2152]146                printers = self.storage.getMatchingPrinters("*")
[3413]147            if self.form.has_key("inputfile") :
[2153]148                inputfile = self.form["inputfile"].value
[3413]149
150        if os.environ.get("REMOTE_USER") is not None :
151            self.body += self.htmlListPrinters(printers or [])
[2381]152            self.body += "<br />"
[2153]153        self.body += _("Filename") + " : "
154        self.body += '<input type="file" size="64" name="inputfile" />'
155        self.body += "<br />"
156        if inputfile :
157            try :
[2352]158                parser = analyzer.PDLAnalyzer(cStringIO.StringIO(inputfile))
[2153]159                jobsize = parser.getJobSize()
[3413]160            except pdlparser.PDLParserError, msg :
[2229]161                self.body += '<p><font color="red">%s</font></p>' % msg
[2379]162                jobsize = 0 # unknown file format ?
[3413]163            else :
[2229]164                self.body += "<p>%s</p>" % (_("Job size : %i pages") % jobsize)
[3413]165
[2525]166            remuser = os.environ.get("REMOTE_USER")
[2229]167            # special hack to accomodate mod_auth_ldap Apache module
168            try :
169                remuser = remuser.split("=")[1].split(",")[0]
[3413]170            except :
[2229]171                pass
[3413]172            if not remuser :
[2229]173                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.")
[3413]174            else :
175                try :
[2229]176                    user = self.storage.getUser(remuser)
[2232]177                    if user.Exists :
[2524]178                        if user.LimitBy == "noprint" :
179                            self.body += "<p>%s</p>" % _("Your account settings forbid you to print at this time.")
[3413]180                        else :
[2524]181                            for printer in printers :
182                                upquota = self.storage.getUserPQuota(user, printer)
183                                if upquota.Exists :
[3549]184                                    if (printer.MaxJobSize and (jobsize > printer.MaxJobSize)) \
185                                            or (upquota.MaxJobSize and (jobsize > upquota.MaxJobSize)) :
[2524]186                                        msg = _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name
[3413]187                                    else :
[2524]188                                        cost = upquota.computeJobPrice(jobsize)
189                                        msg = _("Cost on printer %s : %.2f") % (printer.Name, cost)
190                                        if printer.PassThrough :
191                                            msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode"))
[3413]192                                        elif user.LimitBy == "nochange" :
[2524]193                                            msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable"))
194                                    self.body += "<p>%s</p>" % msg
[2229]195                except :
196                    self.body += '<p><font color="red">%s</font></p>' % self.crashed("CGI Error").replace("\n", "<br />")
[3413]197
[2152]198if __name__ == "__main__" :
[3295]199    utils.reinitcgilocale()
200    admin = PyKotMeGUI()
[2210]201    admin.deferredInit()
[2152]202    admin.form = cgi.FieldStorage()
203    admin.guiAction()
204    admin.guiDisplay()
205    try :
206        admin.storage.close()
[3413]207    except (TypeError, NameError, AttributeError) :
[2152]208        pass
[3413]209
[2152]210    sys.exit(0)
Note: See TracBrowser for help on using the browser.