Show
Ignore:
Timestamp:
06/30/03 15:32:01 (21 years ago)
Author:
jalet
Message:

Much more powerful CGI script for quota reports

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/cgi-bin/printquota.cgi

    r1048 r1049  
    2323# 
    2424# $Log$ 
     25# Revision 1.8  2003/06/30 13:32:01  jalet 
     26# Much more powerful CGI script for quota reports 
     27# 
    2528# Revision 1.7  2003/06/30 12:46:15  jalet 
    2629# Extracted reporting code. 
     
    5457from pykota import version 
    5558from pykota.tool import PyKotaTool, PyKotaToolError 
    56 from pykota.config import PyKotaConfigError 
    57 from pykota.storage import PyKotaStorageError 
     59from pykota.reporter import PyKotaReporterError, openReporter 
    5860 
    5961header = """Content-type: text/html 
     
    6264<html> 
    6365  <head> 
    64     <title>Print Quota Report</title> 
     66    <title>PyKota Reports</title> 
    6567  </head> 
    6668  <body> 
    67     <h2>Print Quota Report</h2> 
    68     <p> 
    69       <pre>""" 
    70  
     69    <form action="printquota.cgi" method="POST"> 
     70      <table> 
     71        <tr> 
     72          <td> 
     73            <p> 
     74              <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" alt="PyKota's Logo" /></a> 
     75              <br /> 
     76              <a href="http://www.librelogiciel.com/software/">PyKota</a> 
     77            </p> 
     78          </td> 
     79          <td colspan="2"> 
     80            <h2>PyKota Reports</h2> 
     81          </td> 
     82        </tr> 
     83        <tr> 
     84          <td colspan="3" align="center"> 
     85            <input type="submit" name="action" value="Report" /> 
     86          </td> 
     87        </tr> 
     88      </table>""" 
     89     
    7190footer = """ 
    72       </pre> 
    73     </p>   
    74     <p> 
    75       <a href="http://www.librelogiciel.com/software/"><img src="http://www.librelogiciel.com/software/PyKota/calllogo" /></a> 
    76       <br /> 
    77       Report generated with <a href="http://www.librelogiciel.com/software/">PyKota</a> 
    78     </p> 
     91    </form> 
    7992  </body> 
    8093</html>"""   
    8194 
    82 print header 
    8395 
    84 out = os.popen("repykota") 
    85 print out.read().strip() 
    86 out.close() 
    87  
    88 print footer 
     96class PyKotaReportGUI(PyKotaTool) : 
     97    """PyKota Administrative GUI""" 
     98    def guiDisplay(self) : 
     99        """Displays the administrative interface.""" 
     100        global header, footer 
     101        print header 
     102        print self.body 
     103        print footer 
     104         
     105    def error(self, message) : 
     106        """Adds an error message to the GUI's body.""" 
     107        if message : 
     108            self.body = '<p><font color="red">%s</font></p>\n%s' % (message, self.body) 
     109         
     110    def htmlListPrinters(self, selected=[], mask="*") :     
     111        """Displays the printers multiple selection list.""" 
     112        printers = self.storage.getMatchingPrinters(mask) 
     113        selectednames = [p.Name for p in selected] 
     114        message = 'Printer : <select name="printers" multiple="multiple">' 
     115        for printer in printers : 
     116            if printer.Name in selectednames : 
     117                message += '<option value="%s" selected="selected">%s</option>' % (printer.Name, printer.Name) 
     118            else : 
     119                message += '<option value="%s">%s</option>' % (printer.Name, printer.Name) 
     120        message += '</select>' 
     121        return message 
     122         
     123    def htmlUGNamesInput(self, value="*") :     
     124        """Input field for user/group names wildcard.""" 
     125        return 'User / Group names mask : <input type="text" name="ugmask" size="20" value="%s" /> <em>e.g. <strong>jo*</strong></em>' % (value or "*") 
     126         
     127    def htmlGroupsCheckbox(self, isgroup=0) : 
     128        """Groups checkbox.""" 
     129        if isgroup : 
     130            return 'Groups report : <input type="checkbox" checked="checked" name="isgroup" />' 
     131        else :     
     132            return 'Groups report : <input type="checkbox" name="isgroup" />' 
     133             
     134    def guiAction(self) : 
     135        """Main function""" 
     136        printers = ugmask = isgroup = None 
     137        self.body = "<p>Please click on the menu above</p>\n" 
     138        if self.form.has_key("action") : 
     139            action = self.form["action"].value 
     140            if action == "Report" : 
     141                if self.form.has_key("printers") : 
     142                    printersfield = self.form["printers"] 
     143                    if type(printersfield) != type([]) : 
     144                        printersfield = [ printersfield ] 
     145                    printers = [self.storage.getPrinter(p.value) for p in printersfield] 
     146                else :     
     147                    printers = self.storage.getMatchingPrinters("*") 
     148                if self.form.has_key("ugmask") :      
     149                    ugmask = self.form["ugmask"].value 
     150                else :      
     151                    ugmask = "*" 
     152                if self.form.has_key("isgroup") :     
     153                    isgroup = 1 
     154                else :     
     155                    isgroup = 0 
     156            else : 
     157                self.error(body, "Invalid action [%s]" % action) 
     158        self.body += self.htmlListPrinters(printers or [])             
     159        self.body += "<br />" 
     160        self.body += self.htmlUGNamesInput(ugmask) 
     161        self.body += "<br />" 
     162        self.body += self.htmlGroupsCheckbox(isgroup) 
     163        if printers and ugmask : 
     164            self.reportingtool = openReporter(admin, "text", printers, [ ugmask ], isgroup) 
     165            self.body += "<pre>%s</pre>" % self.reportingtool.generateReport() 
     166     
     167if __name__ == "__main__" : 
     168    admin = PyKotaReportGUI() 
     169    admin.form = cgi.FieldStorage() 
     170    admin.guiAction() 
     171    admin.guiDisplay()