[695] | 1 | # PyKota |
---|
| 2 | # |
---|
| 3 | # PyKota : Print Quotas for CUPS |
---|
| 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 6 | # You're welcome to redistribute this software under the |
---|
| 7 | # terms of the GNU General Public Licence version 2.0 |
---|
| 8 | # or, at your option, any higher version. |
---|
| 9 | # |
---|
| 10 | # You can read the complete GNU GPL in the file COPYING |
---|
| 11 | # which should come along with this software, or visit |
---|
| 12 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
| 13 | # |
---|
| 14 | # $Id$ |
---|
| 15 | # |
---|
| 16 | # $Log$ |
---|
[706] | 17 | # Revision 1.4 2003/02/05 23:02:10 jalet |
---|
| 18 | # Typo |
---|
| 19 | # |
---|
[705] | 20 | # Revision 1.3 2003/02/05 23:00:12 jalet |
---|
| 21 | # Forgotten import |
---|
| 22 | # Bad datetime conversion |
---|
| 23 | # |
---|
[700] | 24 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
| 25 | # More robust storage |
---|
| 26 | # |
---|
[695] | 27 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 28 | # Initial import into CVS |
---|
| 29 | # |
---|
| 30 | # |
---|
| 31 | # |
---|
| 32 | |
---|
| 33 | from mx import DateTime |
---|
| 34 | |
---|
| 35 | class SQLStorage : |
---|
| 36 | def getUserId(self, username) : |
---|
| 37 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
---|
[700] | 38 | try : |
---|
| 39 | return self.doParseResult(result)[0]["id"] |
---|
| 40 | except TypeError : # Not found |
---|
| 41 | return |
---|
[695] | 42 | |
---|
| 43 | def getPrinterId(self, printername) : |
---|
| 44 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
[700] | 45 | try : |
---|
| 46 | return self.doParseResult(result)[0]["id"] |
---|
| 47 | except TypeError : # Not found |
---|
| 48 | return |
---|
[695] | 49 | |
---|
| 50 | def getPrinterPageCounter(self, printername) : |
---|
| 51 | result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
[700] | 52 | try : |
---|
| 53 | return self.doParseResult(result)[0] |
---|
| 54 | except TypeError : # Not found |
---|
| 55 | return |
---|
[695] | 56 | |
---|
| 57 | def updatePrinterPageCounter(self, printername, username, pagecount) : |
---|
| 58 | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) |
---|
| 59 | |
---|
| 60 | def addUserPQuota(self, username, printername) : |
---|
| 61 | printerid = self.getPrinterId(printername) |
---|
| 62 | if printerid is None : |
---|
| 63 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
---|
| 64 | printerid = self.getPrinterId(printername) |
---|
| 65 | userid = self.getUserId(username) |
---|
| 66 | if userid is None : |
---|
| 67 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
---|
| 68 | userid = self.getUserId(username) |
---|
| 69 | if (printerid is not None) and (userid is not None) : |
---|
| 70 | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
| 71 | |
---|
| 72 | def getUserPQuota(self, username, printername) : |
---|
| 73 | result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) |
---|
[700] | 74 | try : |
---|
| 75 | return self.doParseResult(result)[0] |
---|
| 76 | except TypeError : # Not found |
---|
| 77 | return |
---|
[695] | 78 | |
---|
| 79 | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
---|
| 80 | 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(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) |
---|
| 81 | |
---|
| 82 | def updateUserPQuota(self, username, printername, pagecount) : |
---|
| 83 | self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) |
---|
| 84 | |
---|
| 85 | def buyUserPQuota(self, username, printername, pagebought) : |
---|
| 86 | self.updateUserPQuota(username, printername, -pagebought) |
---|
| 87 | |
---|
| 88 | def checkUserPQuota(self, username, printername) : |
---|
| 89 | # TODO : this doesn't work as expected wrt dates |
---|
| 90 | # TODO : GRACEDELAY should come from the configuration file |
---|
| 91 | # TODO : move this into the PyKotaTool class |
---|
| 92 | now = DateTime.now() |
---|
| 93 | quota = self.getUserPQuota(username, printername) |
---|
| 94 | pagecounter = quota["pagecounter"] |
---|
| 95 | softlimit = quota["softlimit"] |
---|
| 96 | hardlimit = quota["hardlimit"] |
---|
| 97 | datelimit = quota["datelimit"] |
---|
| 98 | if datelimit : |
---|
| 99 | datelimit = DateTime.DateTime(datelimit) # TODO : check this ! |
---|
| 100 | if softlimit is not None : |
---|
| 101 | if pagecounter < softlimit : |
---|
| 102 | action = "ALLOW" |
---|
| 103 | elif hardlimit is not None : |
---|
| 104 | if softlimit <= pagecounter < hardlimit : |
---|
| 105 | if datelimit is None : |
---|
[706] | 106 | self.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i" % (now.year, now.month, now.day, now.hour, now.minute)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) |
---|
[695] | 107 | datelimit = now |
---|
| 108 | if (now - datelimit) <= GRACEDELAY : |
---|
| 109 | action = "WARN" |
---|
| 110 | else : |
---|
| 111 | action = "DENY" |
---|
| 112 | else : |
---|
| 113 | action = "DENY" |
---|
| 114 | else : |
---|
| 115 | action = "DENY" |
---|
| 116 | else : |
---|
| 117 | action = "ALLOW" |
---|
| 118 | return (action, (hardlimit - pagecounter), datelimit) |
---|
| 119 | |
---|