[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$ |
---|
[700] | 17 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
| 18 | # More robust storage |
---|
| 19 | # |
---|
[695] | 20 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 21 | # Initial import into CVS |
---|
| 22 | # |
---|
| 23 | # |
---|
| 24 | # |
---|
| 25 | |
---|
| 26 | from mx import DateTime |
---|
| 27 | |
---|
| 28 | class SQLStorage : |
---|
| 29 | def getUserId(self, username) : |
---|
| 30 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
---|
[700] | 31 | try : |
---|
| 32 | return self.doParseResult(result)[0]["id"] |
---|
| 33 | except TypeError : # Not found |
---|
| 34 | return |
---|
[695] | 35 | |
---|
| 36 | def getPrinterId(self, printername) : |
---|
| 37 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
[700] | 38 | try : |
---|
| 39 | return self.doParseResult(result)[0]["id"] |
---|
| 40 | except TypeError : # Not found |
---|
| 41 | return |
---|
[695] | 42 | |
---|
| 43 | def getPrinterPageCounter(self, printername) : |
---|
| 44 | result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
[700] | 45 | try : |
---|
| 46 | return self.doParseResult(result)[0] |
---|
| 47 | except TypeError : # Not found |
---|
| 48 | return |
---|
[695] | 49 | |
---|
| 50 | def updatePrinterPageCounter(self, printername, username, pagecount) : |
---|
| 51 | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) |
---|
| 52 | |
---|
| 53 | def addUserPQuota(self, username, printername) : |
---|
| 54 | printerid = self.getPrinterId(printername) |
---|
| 55 | if printerid is None : |
---|
| 56 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
---|
| 57 | printerid = self.getPrinterId(printername) |
---|
| 58 | userid = self.getUserId(username) |
---|
| 59 | if userid is None : |
---|
| 60 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
---|
| 61 | userid = self.getUserId(username) |
---|
| 62 | if (printerid is not None) and (userid is not None) : |
---|
| 63 | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
| 64 | |
---|
| 65 | def getUserPQuota(self, username, printername) : |
---|
| 66 | 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] | 67 | try : |
---|
| 68 | return self.doParseResult(result)[0] |
---|
| 69 | except TypeError : # Not found |
---|
| 70 | return |
---|
[695] | 71 | |
---|
| 72 | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
---|
| 73 | 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)))) |
---|
| 74 | |
---|
| 75 | def updateUserPQuota(self, username, printername, pagecount) : |
---|
| 76 | 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)))) |
---|
| 77 | |
---|
| 78 | def buyUserPQuota(self, username, printername, pagebought) : |
---|
| 79 | self.updateUserPQuota(username, printername, -pagebought) |
---|
| 80 | |
---|
| 81 | def checkUserPQuota(self, username, printername) : |
---|
| 82 | # TODO : this doesn't work as expected wrt dates |
---|
| 83 | # TODO : GRACEDELAY should come from the configuration file |
---|
| 84 | # TODO : move this into the PyKotaTool class |
---|
| 85 | now = DateTime.now() |
---|
| 86 | quota = self.getUserPQuota(username, printername) |
---|
| 87 | pagecounter = quota["pagecounter"] |
---|
| 88 | softlimit = quota["softlimit"] |
---|
| 89 | hardlimit = quota["hardlimit"] |
---|
| 90 | datelimit = quota["datelimit"] |
---|
| 91 | if datelimit : |
---|
| 92 | datelimit = DateTime.DateTime(datelimit) # TODO : check this ! |
---|
| 93 | if softlimit is not None : |
---|
| 94 | if pagecounter < softlimit : |
---|
| 95 | action = "ALLOW" |
---|
| 96 | elif hardlimit is not None : |
---|
| 97 | if softlimit <= pagecounter < hardlimit : |
---|
| 98 | if datelimit is None : |
---|
| 99 | self.doQuery("UPDATE userpquota SET datelimit=%s WHERE userid=%s AND printerid=%s;" % ('%04i-%02i-%02i' % (now.year, now.month, now.day), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) |
---|
| 100 | datelimit = now |
---|
| 101 | if (now - datelimit) <= GRACEDELAY : |
---|
| 102 | action = "WARN" |
---|
| 103 | else : |
---|
| 104 | action = "DENY" |
---|
| 105 | else : |
---|
| 106 | action = "DENY" |
---|
| 107 | else : |
---|
| 108 | action = "DENY" |
---|
| 109 | else : |
---|
| 110 | action = "ALLOW" |
---|
| 111 | return (action, (hardlimit - pagecounter), datelimit) |
---|
| 112 | |
---|