Changeset 900

Show
Ignore:
Timestamp:
04/10/03 23:47:20 (22 years ago)
Author:
jalet
Message:

Job history added. Upgrade script neutralized for now !

Location:
pykota/trunk
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r895 r900  
    2323# 
    2424# $Log$ 
     25# Revision 1.33  2003/04/10 21:47:20  jalet 
     26# Job history added. Upgrade script neutralized for now ! 
     27# 
    2528# Revision 1.32  2003/04/08 21:31:39  jalet 
    2629# (anything or 0) = anything !!! Go back to school Jerome ! 
     
    232235    def main(self, names, options) : 
    233236        """Edit user or group quotas.""" 
    234         printernames = self.storage.getMatchingPrinters(options["printer"]) 
    235         if not printernames : 
     237        printers = self.storage.getMatchingPrinters(options["printer"]) 
     238        if not printers : 
    236239            pname = options["printer"] 
    237240            if options["add"] and pname : 
    238241                if self.isValidName(pname) : 
    239                     self.storage.addPrinter(pname) 
    240                     printernames = [ (pname, 0) ] 
     242                    printerid = self.storage.addPrinter(pname) 
     243                    printers = [ (printerid, pname) ] 
    241244                else :     
    242245                    raise PyKotaToolError, _("Invalid printer name %s") % pname 
     
    263266            else :     
    264267                names = [ "*" ] # all users 
    265         for (printer, printerpagecounter) in printernames : 
     268        for (printerid, printer) in printers : 
    266269            if options["prototype"] : 
    267270                if options["groups"] : 
    268                     prototype = self.storage.getGroupPQuota(options["prototype"], printer) 
     271                    prototype = self.storage.getGroupPQuota(self.storage.getGroupId(options["prototype"]), printerid) 
    269272                else :      
    270273                    # default is user quota edition 
    271                     prototype = self.storage.getUserPQuota(options["prototype"], printer) 
     274                    prototype = self.storage.getUserPQuota(self.storage.getUserId(options["prototype"]), printerid) 
    272275                if prototype is None : 
    273276                    self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer)) 
     
    283286                if softlimit is not None : 
    284287                    self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer)) 
    285             if (not options["reset"] and not options["noquota"]) and ((hardlimit is None) or (softlimit is None)) : 
     288            if (not options["reset"] and not options["noquota"] and not options["prototype"]) and ((hardlimit is None) or (softlimit is None)) : 
    286289                raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.") 
    287290            if options["add"] :     
    288                 allnames = names 
     291                allidnames = [(None, n) for n in names] 
    289292            else :    
    290293                if options["groups"] :     
    291                     allnames = self.storage.getPrinterGroups(printer) 
     294                    allidnames = self.storage.getPrinterGroups(printerid) 
    292295                else :     
    293                     allnames = self.storage.getPrinterUsers(printer) 
    294             for name in [n for n in allnames if self.matchString(n, names)]: 
     296                    allidnames = self.storage.getPrinterUsers(printerid) 
     297            for (ident, name) in [(i, n) for (i, n) in allidnames if self.matchString(n, names)]: 
    295298                if options["groups"] : 
    296                     quota = self.storage.getGroupPQuota(name, printer) 
     299                    quota = self.storage.getGroupPQuota(ident, printerid) 
    297300                else : 
    298                     quota = self.storage.getUserPQuota(name, printer) 
     301                    quota = self.storage.getUserPQuota(ident, printerid) 
    299302                if quota is None : 
    300303                    # not found 
     
    306309                        if options["groups"] : 
    307310                            if self.isValidName(name) : 
    308                                 self.storage.addGroupPQuota(name, printer) 
    309                                 quota = self.storage.getGroupPQuota(name, printer) 
     311                                (ident, printerid) = self.storage.addGroupPQuota(name, printerid) 
     312                                quota = self.storage.getGroupPQuota(ident, printerid) 
    310313                            else :     
    311314                                self.logger.log_message(_("Invalid group name %s") % name) 
    312315                        else : 
    313316                            if self.isValidName(name) : 
    314                                 self.storage.addUserPQuota(name, printer) 
    315                                 quota = self.storage.getUserPQuota(name, printer) 
     317                                (ident, printerid) = self.storage.addUserPQuota(name, printerid) 
     318                                quota = self.storage.getUserPQuota(ident, printerid) 
    316319                            else :     
    317320                                self.logger.log_message(_("Invalid user name %s") % name) 
     
    320323                else :     
    321324                    if options["groups"] : 
    322                         if options["noquota"] or ((softlimit is not None) and (hardlimit is not None)) : 
    323                             self.storage.setGroupPQuota(name, printer, softlimit, hardlimit) 
     325                        if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : 
     326                            self.storage.setGroupPQuota(ident, printerid, softlimit, hardlimit) 
    324327                        if options["reset"] : 
    325                             self.storage.resetGroupPQuota(name, printer) 
     328                            self.storage.resetGroupPQuota(ident, printerid) 
     329                        self.warnGroupPQuota(name, printer)     
    326330                    else : 
    327                         if options["noquota"] or ((softlimit is not None) and (hardlimit is not None)) : 
    328                             self.storage.setUserPQuota(name, printer, softlimit, hardlimit) 
     331                        if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : 
     332                            self.storage.setUserPQuota(ident, printerid, softlimit, hardlimit) 
    329333                        if options["reset"] : 
    330                             self.storage.resetUserPQuota(name, printer) 
     334                            self.storage.resetUserPQuota(ident, printerid) 
     335                        self.warnUserPQuota(name, printer)     
    331336                      
    332337if __name__ == "__main__" :  
  • pykota/trunk/bin/pykota

    r887 r900  
    2323# 
    2424# $Log$ 
     25# Revision 1.17  2003/04/10 21:47:20  jalet 
     26# Job history added. Upgrade script neutralized for now ! 
     27# 
    2528# Revision 1.16  2003/04/08 20:38:08  jalet 
    2629# The last job Id is saved now for each printer, this will probably 
     
    151154        # can't get actual page counter, assume printer is off, but warns in log 
    152155        kotafilter.logger.log_message("%s" % msg, "warn") 
     156        counterbeforejob = None 
    153157        printerIsOff = 1 
    154158    else :     
     
    156160         
    157161    # Get the last page counter and last username from the Quota Storage backend 
    158     pgc = kotafilter.storage.getPrinterPageCounter(kotafilter.printername)     
    159     if pgc is None : 
     162    printerid = kotafilter.storage.getPrinterId(kotafilter.printername) 
     163    if printerid is None : 
    160164        # The printer is unknown from the Quota Storage perspective 
    161165        # we let the job pass through, but log a warning message 
    162166        kotafilter.logger.log_message(_("Printer %s not registered in the PyKota system") % kotafilter.printername, "warn") 
    163167    else :     
    164         # get last values from Quota Storage 
    165         (lastpagecounter, lastjobid, lastusername) = (pgc["pagecounter"], pgc["lastjobid"], pgc["lastusername"]) 
    166          
    167         # if printer is off then we assume the correct counter value is the last one 
    168         if printerIsOff : 
    169             counterbeforejob = lastpagecounter 
     168        userid = kotafilter.storage.getUserId(username) 
     169        if userid is None : 
     170            # The user is unknown from the Quota Storage perspective 
     171            # we let the job pass through, but log a warning message 
     172            kotafilter.logger.log_message(_("User %s not registered in the PyKota system") % username, "warn") 
     173        else : 
     174            pgc = kotafilter.storage.getPrinterPageCounter(printerid)     
     175            if pgc is None : 
     176                # The printer hasn't been used yet, from PyKota's point of view 
     177                lasthistoryid = None 
     178                lastjobid = jobid 
     179                lastuserid = userid 
     180                lastusername = username 
     181                lastpagecounter = counterbeforejob 
     182            else :     
     183                # get last values from Quota Storage 
     184                (lasthistoryid, lastjobid, lastuserid, lastusername, lastpagecounter) = (pgc["id"], pgc["jobid"], pgc["userid"], pgc["username"], pgc["pagecounter"]) 
     185                 
     186            # if printer is off then we assume the correct counter value is the last one 
     187            if printerIsOff : 
     188                counterbeforejob = lastpagecounter 
     189                 
     190            # Update the quota for the previous user on this printer according  
     191            # to the job size (difference between actual counter and latest one from storage) 
     192            jobsize = (counterbeforejob - lastpagecounter)     
     193            if jobsize >= 0 : 
     194                kotafilter.storage.updateUserPQuota(lastuserid, printerid, jobsize) 
     195                kotafilter.storage.updateJobSizeInHistory(lasthistoryid, jobsize) 
     196                kotafilter.warnUserPQuota(lastusername) 
     197            else :     
     198                kotafilter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, lastusername, kotafilter.printername), "error") 
     199                 
     200            # Is the current user allowed to print at all ? 
     201            action = kotafilter.warnUserPQuota(username) 
    170202             
    171         # Update the last page counter and last username in the Quota Storage backend 
    172         # set them to current user and  
    173         kotafilter.storage.updatePrinterPageCounter(kotafilter.printername, username, counterbeforejob, jobid) 
    174          
    175         # Was the printer ever used ? 
    176         if (lastpagecounter is None) or (lastusername is None) : 
    177             lastusername = username 
    178             lastpagecounter = counterbeforejob 
     203            # adds the current job to history     
     204            kotafilter.storage.addJobToHistory(jobid, kotafilter.storage.getUserId(username), printerid, counterbeforejob, action) 
    179205             
    180         # Update the quota for the previous user on this printer according  
    181         # to the job size (difference between actual counter and latest one from storage) 
    182         jobsize = (counterbeforejob - lastpagecounter)     
    183         if jobsize >= 0 : 
    184             kotafilter.storage.updateUserPQuota(lastusername, kotafilter.printername, jobsize) 
    185             kotafilter.warnUserPQuota(lastusername) 
    186         else :     
    187             kotafilter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, kotafilter.printername, lastusername), "error") 
    188              
    189         # Is the current user allowed to print at all ? 
    190         # if no then die, else proceed. 
    191         action = kotafilter.warnUserPQuota(username) 
    192         if action == "DENY" : 
    193             # No, just die cleanly 
    194             return 1 
     206            # if not allowed to print then die, else proceed. 
     207            if action == "DENY" : 
     208                # No, just die cleanly 
     209                return 1 
    195210         
    196211    # pass the job untouched to the underlying layer 
  • pykota/trunk/bin/repykota

    r895 r900  
    2323# 
    2424# $Log$ 
     25# Revision 1.21  2003/04/10 21:47:20  jalet 
     26# Job history added. Upgrade script neutralized for now ! 
     27# 
    2528# Revision 1.20  2003/04/08 21:31:39  jalet 
    2629# (anything or 0) = anything !!! Go back to school Jerome ! 
     
    149152    def main(self, options) : 
    150153        """Print Quota reports generator.""" 
    151         printernames = self.storage.getMatchingPrinters(options["printer"]) 
    152         if not printernames : 
     154        printers = self.storage.getMatchingPrinters(options["printer"]) 
     155        if not printers : 
    153156            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 
    154         for (printer, printerpagecounter) in printernames : 
     157        for (printerid, printer) in printers : 
    155158            print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer) 
    156159            print _("Pages grace time: %idays") % self.config.getGraceDelay(printer) 
     
    159162                print _("Group            used     soft     hard   grace        total") 
    160163                print "------------------------------------------------------------" 
    161                 for name in self.storage.getPrinterGroups(printer) : 
    162                     quota = self.storage.getGroupPQuota(name, printer)  
     164                for name in self.storage.getPrinterGroups(printerid) : 
     165                    quota = self.storage.getGroupPQuota(ident, printerid)  
    163166                    total += self.printQuota(name, quota) 
    164167            else : 
     
    166169                print _("User             used     soft     hard   grace        total") 
    167170                print "------------------------------------------------------------" 
    168                 for name in self.storage.getPrinterUsers(printer) : 
    169                     quota = self.storage.getUserPQuota(name, printer) 
     171                for (ident, name) in self.storage.getPrinterUsers(printerid) : 
     172                    quota = self.storage.getUserPQuota(ident, printerid) 
    170173                    total += self.printQuota(name, quota) 
    171174            if total :         
    172175                print (" " * 43) + (_("Total : %9i") % total) 
     176            printerpagecounter = self.storage.getPrinterPageCounter(printerid) 
    173177            if printerpagecounter is None :     
    174178                msg = _("unknown") 
    175179            else : 
    176                 msg = "%9i" % printerpagecounter 
     180                msg = "%9i" % printerpagecounter["pagecounter"] 
    177181            print (" " * 44) + (_("Real : %s") % msg) 
    178182            print         
  • pykota/trunk/bin/warnpykota

    r895 r900  
    2323# 
    2424# $Log$ 
     25# Revision 1.15  2003/04/10 21:47:20  jalet 
     26# Job history added. Upgrade script neutralized for now ! 
     27# 
    2528# Revision 1.14  2003/04/08 21:31:39  jalet 
    2629# (anything or 0) = anything !!! Go back to school Jerome ! 
     
    134137    def main(self, options) : 
    135138        """Warn users or groups over print quota.""" 
    136         printernames = self.storage.getMatchingPrinters(options["printer"]) 
    137         if not printernames : 
     139        printers = self.storage.getMatchingPrinters(options["printer"]) 
     140        if not printers : 
    138141            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 
    139         for (printer, printerpagecounter) in printernames : 
     142        for (printerid, printer) in printers : 
    140143            if options["groups"] : 
    141                 for name in self.storage.getPrinterGroups(printer) : 
     144                for (ident, name) in self.storage.getPrinterGroups(printerid) : 
    142145                    self.warnGroupPQuota(name, printer) 
    143146            else : 
    144                 for name in self.storage.getPrinterUsers(printer) : 
     147                for (ident, name) in self.storage.getPrinterUsers(printerid) : 
    145148                    self.warnUserPQuota(name, printer) 
    146149                      
  • pykota/trunk/initscripts/pykota-postgresql.sql

    r897 r900  
    2020-- 
    2121-- $Log$ 
     22-- Revision 1.10  2003/04/10 21:47:20  jalet 
     23-- Job history added. Upgrade script neutralized for now ! 
     24-- 
    2225-- Revision 1.9  2003/04/09 20:11:29  jalet 
    2326-- Added a field to save the action taken for this job (Allow, Deny) 
     
    9396-- 
    9497CREATE TABLE printers(id SERIAL PRIMARY KEY NOT NULL, 
    95                       printername TEXT UNIQUE NOT NULL, 
    96                       lastjobid TEXT, 
    97                       lastusername TEXT, 
    98                       pagecounter INT4 DEFAULT 0); 
     98                      printername TEXT UNIQUE NOT NULL); 
    9999                     
    100100-- 
     
    111111                         
    112112-- 
    113 -- Create the job history table for u 
     113-- Create the job history table 
    114114-- 
    115115CREATE TABLE jobhistory(id SERIAL PRIMARY KEY NOT NULL, 
     
    117117                        userid INT4 REFERENCES users(id), 
    118118                        printerid INT4 REFERENCES printers(id), 
     119                        pagecounter INT4 DEFAULT 0, 
     120                        jobsize INT4, 
    119121                        action TEXT, 
    120                         jobsize INT4, 
    121122                        jobdate TIMESTAMP DEFAULT now()); 
    122123                         
     
    145146GRANT SELECT, UPDATE ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq TO pykotaadmin; 
    146147GRANT SELECT, UPDATE ON printers, userpquota, grouppquota TO pykotauser; 
    147 GRANT SELECT ON users, groups, groupsmembers, jobhistory TO pykotauser; 
     148GRANT SELECT ON users, groups, groupsmembers TO pykotauser; 
     149GRANT SELECT, INSERT, UPDATE ON jobhistory TO pykotauser; 
     150GRANT SELECT, UPDATE ON jobhistory_id_seq TO pykotauser; 
    148151 
  • pykota/trunk/initscripts/README.postgresql

    r887 r900  
    9393  1.03, YOU HAVE TO RUN the upgrade-1.01-to-1.03.sh shell script. 
    9494  NOT LAUNCHING THIS SCRIPT WILL CAUSE PYKOTA TO STOP WORKING ! 
    95   Please don't run this script if your version of PyKota is already 
    96   1.03 or higher. 
     95  Please don't run this script if your existing version of PyKota is  
     96  already 1.03 or higher. 
  • pykota/trunk/initscripts/upgrade-1.01-to-1.03.sh

    r888 r900  
    55echo WARNING : If unsure press Ctrl+C NOW TO STOP, else press ENTER TO PROCEED 
    66read 
    7 echo -n Update begins... 
    8 pg_dump -D -N -U postgres -f pykotadb-full.dump pykota >update.messages 2>update.errors 
    9 pg_dump -a -D -N -U postgres -f pykotadb.dump pykota >>update.messages 2>>update.errors 
    10 dropdb pykota >>update.messages 2>>update.errors 
    11 dropuser pykotaadmin >>update.messages 2>>update.errors 
    12 dropuser pykotauser >>update.messages 2>>update.errors 
    13 psql -U postgres template1 -f pykota-postgresql.sql >>update.messages 2>>update.errors 
    14 psql -U postgres pykota -f pykotadb.dump >>update.messages 2>>update.errors 
    15 echo 
    16 echo Done ! 
    17 echo Check the files update.messages and update.errors to see if all is OK. 
    18 echo NOTICE messages are normal and expected. In case other message types 
    19 echo are found in update.errors, please file a bug report and restore 
    20 echo your database from pykotadb-full.dump 
     7 
     8echo This script does not work at all yet ! 
     9echo Please be patient so that I can write a safe upgrade script 
     10echo of completely drop your existing database to start anew 
     11echo with this version of PyKota. 
     12 
     13#echo -n Update begins... 
     14#pg_dump -D -N -U postgres -f pykotadb-full.dump pykota >upgrade.messages 2>upgrade.errors 
     15#pg_dump -a -D -N -U postgres -f pykotadb.dump pykota >>upgrade.messages 2>>upgrade.errors 
     16#dropdb pykota >>upgrade.messages 2>>upgrade.errors 
     17#dropuser pykotaadmin >>upgrade.messages 2>>upgrade.errors 
     18#dropuser pykotauser >>upgrade.messages 2>>upgrade.errors 
     19#psql -U postgres template1 -f pykota-postgresql.sql >>upgrade.messages 2>>upgrade.errors 
     20#psql -U postgres pykota -f pykotadb.dump >>upgrade.messages 2>>upgrade.errors 
     21#echo 
     22#echo Done ! 
     23#echo Check the files upgrade.messages and upgrade.errors to see if all is OK. 
     24#echo NOTICE messages are normal and expected. In case other message types 
     25#echo are found in upgrade.errors, please file a bug report and restore 
     26#echo your database from pykotadb-full.dump 
  • pykota/trunk/NEWS

    r899 r900  
    2424    - 1.03alpha : 
    2525     
    26         - IMPORTANT : DataBase schema has changed, YOU HAVE TO 
    27           UPGRADE using the upgrade-1.01-to-1.03.sh script. 
    28           Please read initscripts/README first and then  
    29           read initscripts/README.postgresql 
     26        - IMPORTANT : DataBase schema has changed. 
     27          DON'T TRY TO UPGRADE AN EXISTING PYKOTA INSTALLATION ! 
     28          The upgrade script will be finalized ASAP, be patient, 
     29          or drop your old quota database to start anew. 
    3030           
    3131        - The installation script now allows to install the sample 
     
    3838        - The job history is now kept, this will allow per-period 
    3939          reports in the future. 
    40          
     40           
     41        - After having modified the quota for an user with edpykota, 
     42          a quota check is done to eventually warn the user/admin about 
     43          a quota which is too low to print. 
     44           
    4145    - 1.02 : 
    4246     
  • pykota/trunk/pykota/storage.py

    r873 r900  
    2121# 
    2222# $Log$ 
     23# Revision 1.11  2003/04/10 21:47:20  jalet 
     24# Job history added. Upgrade script neutralized for now ! 
     25# 
    2326# Revision 1.10  2003/03/29 13:45:27  jalet 
    2427# GPL paragraphs were incorrectly (from memory) copied into the sources. 
     
    7174        return self.message 
    7275    __str__ = __repr__ 
    73      
    74 class BaseStorage :     
    75     """Base class for all storages.""" 
    76     def getMatchingPrinters(self, printerpattern) : 
    77         """Returns the list of all printers tuples (name, pagecounter) which match a certain pattern for the printer name.""" 
    78         pass 
    79              
    80     def addPrinter(self, printername) :         
    81         """Adds a printer to the quota storage.""" 
    82         pass 
    83          
    84     def getPrinterUsers(self, printername) :         
    85         """Returns the list of usernames which uses a given printer.""" 
    86         pass 
    87          
    88     def getPrinterGroups(self, printername) :         
    89         """Returns the list of groups which uses a given printer.""" 
    90         pass 
    91          
    92     def getPrinterPageCounter(self, printername) : 
    93         """Returns the last page counter value for a printer given its name.""" 
    94         pass 
    95          
    96     def updatePrinterPageCounter(self, printername, username, pagecount) : 
    97         """Updates the last page counter information for a printer given its name, last username and pagecount.""" 
    98         pass 
    99          
    100     def addUserPQuota(self, username, printername) : 
    101         """Initializes a user print quota on a printer, adds the printer and the user to the quota storage if needed.""" 
    102         pass 
    103          
    104     def getUPIds(self, username, printername) :     
    105         """Returns a tuple (userid, printerid) given a username and a printername.""" 
    106         pass 
    107          
    108     def getUserPQuota(self, username, printername) : 
    109         """Returns the Print Quota information for a given (username, printername).""" 
    110         pass 
    111          
    112     def setUserPQuota(self, username, printername, softlimit, hardlimit) : 
    113         """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" 
    114         pass 
    115          
    116     def resetUserPQuota(self, username, printername) :     
    117         """Resets the page counter to zero. Life time page counter is kept unchanged.""" 
    118         pass 
    119          
    120     def setDateLimit(self, username, printername, datelimit) : 
    121         """Sets the limit date for a soft limit to become an hard one given (username, printername).""" 
    122         pass 
    123          
    124     def updateUserPQuota(self, username, printername, pagecount) : 
    125         """Updates the used user Quota information given (username, printername) and a job size in pages.""" 
    126         pass 
    127          
    128     def buyUserPQuota(self, username, printername, pagebought) : 
    129         """Buys pages for a given (username, printername).""" 
    130         pass 
    13176         
    13277def openConnection(config, asadmin=0) : 
  • pykota/trunk/pykota/storages/sql.py

    r887 r900  
    2121# 
    2222# $Log$ 
     23# Revision 1.22  2003/04/10 21:47:20  jalet 
     24# Job history added. Upgrade script neutralized for now ! 
     25# 
    2326# Revision 1.21  2003/04/08 20:38:08  jalet 
    2427# The last job Id is saved now for each printer, this will probably 
     
    99102class SQLStorage :     
    100103    def getMatchingPrinters(self, printerpattern) : 
    101         """Returns the list of all printers tuples (name, pagecounter) which match a certain pattern for the printer name.""" 
     104        """Returns the list of all printers as tuples (id, name) for printer names which match a certain pattern.""" 
    102105        printerslist = [] 
    103106        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... 
    104107        # but we don't because other storages semantics may be different, so every 
    105108        # storage should use fnmatch to match patterns and be storage agnostic 
    106         result = self.doQuery("SELECT printername, pagecounter FROM printers;") 
     109        result = self.doQuery("SELECT id, printername FROM printers;") 
    107110        result = self.doParseResult(result) 
    108111        if result is not None : 
    109112            for printer in result : 
    110113                if fnmatch.fnmatchcase(printer["printername"], printerpattern) : 
    111                     printerslist.append((printer["printername"], printer["pagecounter"])) 
     114                    printerslist.append((printer["id"], printer["printername"])) 
    112115        return printerslist         
    113116             
    114     def addPrinter(self, printername) :         
    115         """Adds a printer to the quota storage.""" 
    116         self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) 
    117          
    118     def getPrinterUsers(self, printername) :         
     117    def getPrinterId(self, printername) :         
     118        """Returns a printerid given a printername.""" 
     119        result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
     120        try : 
     121            return self.doParseResult(result)[0]["id"] 
     122        except TypeError :      # Not found     
     123            return 
     124             
     125    def getUserId(self, username) : 
     126        """Returns a userid given a username.""" 
     127        result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) 
     128        try : 
     129            return self.doParseResult(result)[0]["id"] 
     130        except TypeError :      # Not found 
     131            return 
     132             
     133    def getGroupId(self, groupname) : 
     134        """Returns a groupid given a grupname.""" 
     135        result = self.doQuery("SELECT id FROM groups WHERE groupname=%s;" % self.doQuote(groupname)) 
     136        try : 
     137            return self.doParseResult(result)[0]["id"] 
     138        except TypeError :      # Not found 
     139            return 
     140             
     141    def getJobHistoryId(self, jobid, userid, printerid) :         
     142        """Returns the history line's id given a (jobid, userid, printerid).""" 
     143        result = self.doQuery("SELECT id FROM jobhistory WHERE jobid=%s AND userid=%s AND printerid=%s;" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid))) 
     144        try : 
     145            return self.doParseResult(result)[0]["id"] 
     146        except TypeError :      # Not found     
     147            return 
     148             
     149    def getPrinterUsers(self, printerid) :         
    119150        """Returns the list of usernames which uses a given printer.""" 
    120         result = self.doQuery("SELECT DISTINCT username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid IN (SELECT printerid FROM printers WHERE printername=%s)) ORDER BY username;" % self.doQuote(printername)) 
     151        result = self.doQuery("SELECT DISTINCT id, username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid=%s) ORDER BY username;" % self.doQuote(printerid)) 
    121152        result = self.doParseResult(result) 
    122153        if result is None : 
    123154            return [] 
    124155        else :     
    125             return [record["username"] for record in result] 
    126          
    127     def getPrinterGroups(self, printername) :         
     156            return [(record["id"], record["username"]) for record in result] 
     157         
     158    def getPrinterGroups(self, printerid) :         
    128159        """Returns the list of groups which uses a given printer.""" 
    129         result = self.doQuery("SELECT DISTINCT groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid IN (SELECT printerid FROM printers WHERE printername=%s));" % self.doQuote(printername)) 
     160        result = self.doQuery("SELECT DISTINCT id, groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid=%s);" % self.doQuote(printerid)) 
    130161        result = self.doParseResult(result) 
    131162        if result is None : 
    132163            return [] 
    133164        else :     
    134             return [record["groupname"] for record in result] 
    135          
    136     def getUserId(self, username) : 
    137         """Returns a userid given a username.""" 
    138         result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) 
    139         try : 
    140             return self.doParseResult(result)[0]["id"] 
    141         except TypeError :      # Not found 
    142             return 
    143              
    144     def getPrinterId(self, printername) :         
    145         """Returns a printerid given a printername.""" 
    146         result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
    147         try : 
    148             return self.doParseResult(result)[0]["id"] 
     165            return [(record["id"], record["groupname"]) for record in result] 
     166         
     167    def addPrinter(self, printername) :         
     168        """Adds a printer to the quota storage, returns its id.""" 
     169        self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) 
     170        return self.getPrinterId(printername) 
     171         
     172    def addUser(self, username) :         
     173        """Adds a user to the quota storage, returns its id.""" 
     174        self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) 
     175        return self.getUserId(username) 
     176         
     177    def addGroup(self, groupname) :         
     178        """Adds a group to the quota storage, returns its id.""" 
     179        self.doQuery("INSERT INTO groups (groupname) VALUES (%s);" % self.doQuote(groupname)) 
     180        return self.getGroupId(groupname) 
     181         
     182    def addUserPQuota(self, username, printerid) : 
     183        """Initializes a user print quota on a printer, adds the user to the quota storage if needed.""" 
     184        userid = self.getUserId(username)      
     185        if userid is None :     
     186            userid = self.addUser(username) 
     187        self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) 
     188        return (userid, printerid) 
     189         
     190    def addGroupPQuota(self, groupname, printerid) : 
     191        """Initializes a group print quota on a printer, adds the group to the quota storage if needed.""" 
     192        groupid = self.getGroupId(groupname)      
     193        if groupid is None :     
     194            groupid = self.addUser(groupname) 
     195        self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s);" % (self.doQuote(groupid), self.doQuote(printerid))) 
     196        return (groupid, printerid) 
     197         
     198    def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : 
     199        """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid).""" 
     200        self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) 
     201         
     202    def resetUserPQuota(self, userid, printerid) :     
     203        """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged.""" 
     204        self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
     205         
     206    def updateUserPQuota(self, userid, printerid, pagecount) : 
     207        """Updates the used user Quota information given (userid, printerid) and a job size in pages.""" 
     208        self.doQuery("UPDATE userpquota SET lifepagecounter=lifepagecounter+(%s), pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) 
     209         
     210    def getUserPQuota(self, userid, printerid) : 
     211        """Returns the Print Quota information for a given (userid, printerid).""" 
     212        result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
     213        try : 
     214            return self.doParseResult(result)[0] 
    149215        except TypeError :      # Not found     
    150216            return 
    151              
    152     def getPrinterPageCounter(self, printername) : 
    153         """Returns the last page counter value for a printer given its name.""" 
    154         result = self.doQuery("SELECT pagecounter, lastjobid, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) 
     217         
     218    def setUserDateLimit(self, userid, printerid, datelimit) : 
     219        """Sets the limit date for a soft limit to become an hard one given (userid, printerid).""" 
     220        self.doQuery("UPDATE userpquota SET datelimit=%s::TIMESTAMP WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(userid), self.doQuote(printerid))) 
     221         
     222    def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) : 
     223        """Adds a job to the history: (jobid, userid, printerid, last page counter taken from requester).""" 
     224        self.doQuery("INSERT INTO jobhistory (jobid, userid, printerid, pagecounter, action) VALUES (%s, %s, %s, %s, %s);" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid), self.doQuote(pagecounter), self.doQuote(action))) 
     225        return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient 
     226     
     227    def updateJobSizeInHistory(self, historyid, jobsize) : 
     228        """Updates a job size in the history given the history line's id.""" 
     229        self.doQuery("UPDATE jobhistory SET jobsize=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(historyid))) 
     230     
     231    def getPrinterPageCounter(self, printerid) : 
     232        """Returns the last page counter value for a printer given its id, also returns last username, last jobid and history line id.""" 
     233        result = self.doQuery("SELECT jobhistory.id, jobid, userid, username, pagecounter FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1;" % self.doQuote(printerid)) 
    155234        try : 
    156235            return self.doParseResult(result)[0] 
     
    158237            return 
    159238         
    160     def updatePrinterPageCounter(self, printername, username, pagecount, jobid) : 
    161         """Updates the last page counter information for a printer given its name, last username, pagecount and jobid.""" 
    162         return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s, lastjobid=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(jobid), self.doQuote(printername))) 
    163          
    164     def addUserPQuota(self, username, printername) : 
    165         """Initializes a user print quota on a printer, adds the printer and the user to the quota storage if needed.""" 
    166         (userid, printerid) = self.getUPIds(username, printername) 
    167         if printerid is None :     
    168             self.addPrinter(printername)        # should we still add it ? 
    169         if userid is None :     
    170             self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) 
    171         (userid, printerid) = self.getUPIds(username, printername) 
    172         if (userid is not None) and (printerid is not None) : 
    173             return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) 
    174          
    175     def getUPIds(self, username, printername) :     
    176         """Returns a tuple (userid, printerid) given a username and a printername.""" 
    177         return (self.getUserId(username), self.getPrinterId(printername)) 
    178          
    179     def getUserPQuota(self, username, printername) : 
    180         """Returns the Print Quota information for a given (username, printername).""" 
    181         (userid, printerid) = self.getUPIds(username, printername) 
    182         if (userid is not None) and (printerid is not None) : 
    183             result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
    184             try : 
    185                 return self.doParseResult(result)[0] 
    186             except TypeError :      # Not found     
    187                 pass 
    188          
    189     def setUserPQuota(self, username, printername, softlimit, hardlimit) : 
    190         """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" 
    191         (userid, printerid) = self.getUPIds(username, printername) 
    192         if (userid is not None) and (printerid is not None) : 
    193             self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) 
    194          
    195     def resetUserPQuota(self, username, printername) :     
    196         """Resets the page counter to zero. Life time page counter is kept unchanged.""" 
    197         (userid, printerid) = self.getUPIds(username, printername) 
    198         if (userid is not None) and (printerid is not None) : 
    199             self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
    200          
    201     def setDateLimit(self, username, printername, datelimit) : 
    202         """Sets the limit date for a soft limit to become an hard one given (username, printername).""" 
    203         (userid, printerid) = self.getUPIds(username, printername) 
    204         if (userid is not None) and (printerid is not None) : 
    205             self.doQuery("UPDATE userpquota SET datelimit=%s::TIMESTAMP WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(userid), self.doQuote(printerid))) 
    206          
    207     def updateUserPQuota(self, username, printername, pagecount) : 
    208         """Updates the used user Quota information given (username, printername) and a job size in pages.""" 
    209         (userid, printerid) = self.getUPIds(username, printername) 
    210         if (userid is not None) and (printerid is not None) : 
    211             self.doQuery("UPDATE userpquota SET lifepagecounter=lifepagecounter+(%s), pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) 
    212          
  • pykota/trunk/pykota/tool.py

    r873 r900  
    2121# 
    2222# $Log$ 
     23# Revision 1.30  2003/04/10 21:47:20  jalet 
     24# Job history added. Upgrade script neutralized for now ! 
     25# 
    2326# Revision 1.29  2003/03/29 13:45:27  jalet 
    2427# GPL paragraphs were incorrectly (from memory) copied into the sources. 
     
    276279    def checkUserPQuota(self, username, printername) : 
    277280        """Checks the user quota on a printer and deny or accept the job.""" 
    278         quota = self.storage.getUserPQuota(username, printername) 
     281        printerid = self.storage.getPrinterId(printername) 
     282        userid = self.storage.getUserId(username) 
     283        quota = self.storage.getUserPQuota(userid, printerid) 
    279284        if quota is None : 
    280285            # Unknown user or printer or combination 
     
    304309                            else : 
    305310                                datelimit = now + self.config.getGraceDelay(printername) 
    306                                 self.storage.setDateLimit(username, printername, datelimit) 
     311                                self.storage.setUserDateLimit(userid, printerid, datelimit) 
    307312                            if now < datelimit : 
    308313                                action = "WARN" 
  • pykota/trunk/README

    r887 r900  
    6363        - Special scripts included for a seamless integration of 
    6464          PyKota on Debian machines. 
     65           
     66        - Complete job history is saved. This will allow more 
     67          complex reports in the future. 
    6568           
    6669All the command line tools accept the -h | --help command line option