- Timestamp:
- 04/10/03 23:47:20 (22 years ago)
- Location:
- pykota/trunk
- Files:
-
- 12 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/edpykota
r895 r900 23 23 # 24 24 # $Log$ 25 # Revision 1.33 2003/04/10 21:47:20 jalet 26 # Job history added. Upgrade script neutralized for now ! 27 # 25 28 # Revision 1.32 2003/04/08 21:31:39 jalet 26 29 # (anything or 0) = anything !!! Go back to school Jerome ! … … 232 235 def main(self, names, options) : 233 236 """Edit user or group quotas.""" 234 printer names = self.storage.getMatchingPrinters(options["printer"])235 if not printer names :237 printers = self.storage.getMatchingPrinters(options["printer"]) 238 if not printers : 236 239 pname = options["printer"] 237 240 if options["add"] and pname : 238 241 if self.isValidName(pname) : 239 self.storage.addPrinter(pname)240 printer names = [ (pname, 0) ]242 printerid = self.storage.addPrinter(pname) 243 printers = [ (printerid, pname) ] 241 244 else : 242 245 raise PyKotaToolError, _("Invalid printer name %s") % pname … … 263 266 else : 264 267 names = [ "*" ] # all users 265 for (printer , printerpagecounter) in printernames :268 for (printerid, printer) in printers : 266 269 if options["prototype"] : 267 270 if options["groups"] : 268 prototype = self.storage.getGroupPQuota( options["prototype"], printer)271 prototype = self.storage.getGroupPQuota(self.storage.getGroupId(options["prototype"]), printerid) 269 272 else : 270 273 # default is user quota edition 271 prototype = self.storage.getUserPQuota( options["prototype"], printer)274 prototype = self.storage.getUserPQuota(self.storage.getUserId(options["prototype"]), printerid) 272 275 if prototype is None : 273 276 self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer)) … … 283 286 if softlimit is not None : 284 287 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)) : 286 289 raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.") 287 290 if options["add"] : 288 all names = names291 allidnames = [(None, n) for n in names] 289 292 else : 290 293 if options["groups"] : 291 all names = self.storage.getPrinterGroups(printer)294 allidnames = self.storage.getPrinterGroups(printerid) 292 295 else : 293 all names = 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)]: 295 298 if options["groups"] : 296 quota = self.storage.getGroupPQuota( name, printer)299 quota = self.storage.getGroupPQuota(ident, printerid) 297 300 else : 298 quota = self.storage.getUserPQuota( name, printer)301 quota = self.storage.getUserPQuota(ident, printerid) 299 302 if quota is None : 300 303 # not found … … 306 309 if options["groups"] : 307 310 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) 310 313 else : 311 314 self.logger.log_message(_("Invalid group name %s") % name) 312 315 else : 313 316 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) 316 319 else : 317 320 self.logger.log_message(_("Invalid user name %s") % name) … … 320 323 else : 321 324 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) 324 327 if options["reset"] : 325 self.storage.resetGroupPQuota(name, printer) 328 self.storage.resetGroupPQuota(ident, printerid) 329 self.warnGroupPQuota(name, printer) 326 330 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) 329 333 if options["reset"] : 330 self.storage.resetUserPQuota(name, printer) 334 self.storage.resetUserPQuota(ident, printerid) 335 self.warnUserPQuota(name, printer) 331 336 332 337 if __name__ == "__main__" : -
pykota/trunk/bin/pykota
r887 r900 23 23 # 24 24 # $Log$ 25 # Revision 1.17 2003/04/10 21:47:20 jalet 26 # Job history added. Upgrade script neutralized for now ! 27 # 25 28 # Revision 1.16 2003/04/08 20:38:08 jalet 26 29 # The last job Id is saved now for each printer, this will probably … … 151 154 # can't get actual page counter, assume printer is off, but warns in log 152 155 kotafilter.logger.log_message("%s" % msg, "warn") 156 counterbeforejob = None 153 157 printerIsOff = 1 154 158 else : … … 156 160 157 161 # Get the last page counter and last username from the Quota Storage backend 158 p gc = kotafilter.storage.getPrinterPageCounter(kotafilter.printername)159 if p gcis None :162 printerid = kotafilter.storage.getPrinterId(kotafilter.printername) 163 if printerid is None : 160 164 # The printer is unknown from the Quota Storage perspective 161 165 # we let the job pass through, but log a warning message 162 166 kotafilter.logger.log_message(_("Printer %s not registered in the PyKota system") % kotafilter.printername, "warn") 163 167 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) 170 202 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) 179 205 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 195 210 196 211 # pass the job untouched to the underlying layer -
pykota/trunk/bin/repykota
r895 r900 23 23 # 24 24 # $Log$ 25 # Revision 1.21 2003/04/10 21:47:20 jalet 26 # Job history added. Upgrade script neutralized for now ! 27 # 25 28 # Revision 1.20 2003/04/08 21:31:39 jalet 26 29 # (anything or 0) = anything !!! Go back to school Jerome ! … … 149 152 def main(self, options) : 150 153 """Print Quota reports generator.""" 151 printer names = self.storage.getMatchingPrinters(options["printer"])152 if not printer names :154 printers = self.storage.getMatchingPrinters(options["printer"]) 155 if not printers : 153 156 raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 154 for (printer , printerpagecounter) in printernames :157 for (printerid, printer) in printers : 155 158 print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer) 156 159 print _("Pages grace time: %idays") % self.config.getGraceDelay(printer) … … 159 162 print _("Group used soft hard grace total") 160 163 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) 163 166 total += self.printQuota(name, quota) 164 167 else : … … 166 169 print _("User used soft hard grace total") 167 170 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) 170 173 total += self.printQuota(name, quota) 171 174 if total : 172 175 print (" " * 43) + (_("Total : %9i") % total) 176 printerpagecounter = self.storage.getPrinterPageCounter(printerid) 173 177 if printerpagecounter is None : 174 178 msg = _("unknown") 175 179 else : 176 msg = "%9i" % printerpagecounter 180 msg = "%9i" % printerpagecounter["pagecounter"] 177 181 print (" " * 44) + (_("Real : %s") % msg) 178 182 print -
pykota/trunk/bin/warnpykota
r895 r900 23 23 # 24 24 # $Log$ 25 # Revision 1.15 2003/04/10 21:47:20 jalet 26 # Job history added. Upgrade script neutralized for now ! 27 # 25 28 # Revision 1.14 2003/04/08 21:31:39 jalet 26 29 # (anything or 0) = anything !!! Go back to school Jerome ! … … 134 137 def main(self, options) : 135 138 """Warn users or groups over print quota.""" 136 printer names = self.storage.getMatchingPrinters(options["printer"])137 if not printer names :139 printers = self.storage.getMatchingPrinters(options["printer"]) 140 if not printers : 138 141 raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 139 for (printer , printerpagecounter) in printernames :142 for (printerid, printer) in printers : 140 143 if options["groups"] : 141 for name in self.storage.getPrinterGroups(printer) :144 for (ident, name) in self.storage.getPrinterGroups(printerid) : 142 145 self.warnGroupPQuota(name, printer) 143 146 else : 144 for name in self.storage.getPrinterUsers(printer) :147 for (ident, name) in self.storage.getPrinterUsers(printerid) : 145 148 self.warnUserPQuota(name, printer) 146 149 -
pykota/trunk/initscripts/pykota-postgresql.sql
r897 r900 20 20 -- 21 21 -- $Log$ 22 -- Revision 1.10 2003/04/10 21:47:20 jalet 23 -- Job history added. Upgrade script neutralized for now ! 24 -- 22 25 -- Revision 1.9 2003/04/09 20:11:29 jalet 23 26 -- Added a field to save the action taken for this job (Allow, Deny) … … 93 96 -- 94 97 CREATE 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); 99 99 100 100 -- … … 111 111 112 112 -- 113 -- Create the job history table for u113 -- Create the job history table 114 114 -- 115 115 CREATE TABLE jobhistory(id SERIAL PRIMARY KEY NOT NULL, … … 117 117 userid INT4 REFERENCES users(id), 118 118 printerid INT4 REFERENCES printers(id), 119 pagecounter INT4 DEFAULT 0, 120 jobsize INT4, 119 121 action TEXT, 120 jobsize INT4,121 122 jobdate TIMESTAMP DEFAULT now()); 122 123 … … 145 146 GRANT SELECT, UPDATE ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq TO pykotaadmin; 146 147 GRANT SELECT, UPDATE ON printers, userpquota, grouppquota TO pykotauser; 147 GRANT SELECT ON users, groups, groupsmembers, jobhistory TO pykotauser; 148 GRANT SELECT ON users, groups, groupsmembers TO pykotauser; 149 GRANT SELECT, INSERT, UPDATE ON jobhistory TO pykotauser; 150 GRANT SELECT, UPDATE ON jobhistory_id_seq TO pykotauser; 148 151 -
pykota/trunk/initscripts/README.postgresql
r887 r900 93 93 1.03, YOU HAVE TO RUN the upgrade-1.01-to-1.03.sh shell script. 94 94 NOT LAUNCHING THIS SCRIPT WILL CAUSE PYKOTA TO STOP WORKING ! 95 Please don't run this script if your version of PyKota is already96 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 5 5 echo WARNING : If unsure press Ctrl+C NOW TO STOP, else press ENTER TO PROCEED 6 6 read 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 8 echo This script does not work at all yet ! 9 echo Please be patient so that I can write a safe upgrade script 10 echo of completely drop your existing database to start anew 11 echo 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 24 24 - 1.03alpha : 25 25 26 - IMPORTANT : DataBase schema has changed , YOU HAVE TO27 UPGRADE using the upgrade-1.01-to-1.03.sh script.28 Please read initscripts/README first and then29 read initscripts/README.postgresql26 - 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. 30 30 31 31 - The installation script now allows to install the sample … … 38 38 - The job history is now kept, this will allow per-period 39 39 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 41 45 - 1.02 : 42 46 -
pykota/trunk/pykota/storage.py
r873 r900 21 21 # 22 22 # $Log$ 23 # Revision 1.11 2003/04/10 21:47:20 jalet 24 # Job history added. Upgrade script neutralized for now ! 25 # 23 26 # Revision 1.10 2003/03/29 13:45:27 jalet 24 27 # GPL paragraphs were incorrectly (from memory) copied into the sources. … … 71 74 return self.message 72 75 __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 pass79 80 def addPrinter(self, printername) :81 """Adds a printer to the quota storage."""82 pass83 84 def getPrinterUsers(self, printername) :85 """Returns the list of usernames which uses a given printer."""86 pass87 88 def getPrinterGroups(self, printername) :89 """Returns the list of groups which uses a given printer."""90 pass91 92 def getPrinterPageCounter(self, printername) :93 """Returns the last page counter value for a printer given its name."""94 pass95 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 pass99 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 pass103 104 def getUPIds(self, username, printername) :105 """Returns a tuple (userid, printerid) given a username and a printername."""106 pass107 108 def getUserPQuota(self, username, printername) :109 """Returns the Print Quota information for a given (username, printername)."""110 pass111 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 pass115 116 def resetUserPQuota(self, username, printername) :117 """Resets the page counter to zero. Life time page counter is kept unchanged."""118 pass119 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 pass123 124 def updateUserPQuota(self, username, printername, pagecount) :125 """Updates the used user Quota information given (username, printername) and a job size in pages."""126 pass127 128 def buyUserPQuota(self, username, printername, pagebought) :129 """Buys pages for a given (username, printername)."""130 pass131 76 132 77 def openConnection(config, asadmin=0) : -
pykota/trunk/pykota/storages/sql.py
r887 r900 21 21 # 22 22 # $Log$ 23 # Revision 1.22 2003/04/10 21:47:20 jalet 24 # Job history added. Upgrade script neutralized for now ! 25 # 23 26 # Revision 1.21 2003/04/08 20:38:08 jalet 24 27 # The last job Id is saved now for each printer, this will probably … … 99 102 class SQLStorage : 100 103 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.""" 102 105 printerslist = [] 103 106 # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... 104 107 # but we don't because other storages semantics may be different, so every 105 108 # storage should use fnmatch to match patterns and be storage agnostic 106 result = self.doQuery("SELECT printername, pagecounterFROM printers;")109 result = self.doQuery("SELECT id, printername FROM printers;") 107 110 result = self.doParseResult(result) 108 111 if result is not None : 109 112 for printer in result : 110 113 if fnmatch.fnmatchcase(printer["printername"], printerpattern) : 111 printerslist.append((printer[" printername"], printer["pagecounter"]))114 printerslist.append((printer["id"], printer["printername"])) 112 115 return printerslist 113 116 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) : 119 150 """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)) 121 152 result = self.doParseResult(result) 122 153 if result is None : 123 154 return [] 124 155 else : 125 return [ record["username"]for record in result]126 127 def getPrinterGroups(self, printer name) :156 return [(record["id"], record["username"]) for record in result] 157 158 def getPrinterGroups(self, printerid) : 128 159 """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)) 130 161 result = self.doParseResult(result) 131 162 if result is None : 132 163 return [] 133 164 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] 149 215 except TypeError : # Not found 150 216 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)) 155 234 try : 156 235 return self.doParseResult(result)[0] … … 158 237 return 159 238 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 found187 pass188 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 21 21 # 22 22 # $Log$ 23 # Revision 1.30 2003/04/10 21:47:20 jalet 24 # Job history added. Upgrade script neutralized for now ! 25 # 23 26 # Revision 1.29 2003/03/29 13:45:27 jalet 24 27 # GPL paragraphs were incorrectly (from memory) copied into the sources. … … 276 279 def checkUserPQuota(self, username, printername) : 277 280 """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) 279 284 if quota is None : 280 285 # Unknown user or printer or combination … … 304 309 else : 305 310 datelimit = now + self.config.getGraceDelay(printername) 306 self.storage.set DateLimit(username, printername, datelimit)311 self.storage.setUserDateLimit(userid, printerid, datelimit) 307 312 if now < datelimit : 308 313 action = "WARN" -
pykota/trunk/README
r887 r900 63 63 - Special scripts included for a seamless integration of 64 64 PyKota on Debian machines. 65 66 - Complete job history is saved. This will allow more 67 complex reports in the future. 65 68 66 69 All the command line tools accept the -h | --help command line option