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$ |
---|
17 | # Revision 1.5 2003/02/05 23:26:22 jalet |
---|
18 | # Incorrect handling of grace delay |
---|
19 | # |
---|
20 | # Revision 1.4 2003/02/05 23:02:10 jalet |
---|
21 | # Typo |
---|
22 | # |
---|
23 | # Revision 1.3 2003/02/05 23:00:12 jalet |
---|
24 | # Forgotten import |
---|
25 | # Bad datetime conversion |
---|
26 | # |
---|
27 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
28 | # More robust storage |
---|
29 | # |
---|
30 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
31 | # Initial import into CVS |
---|
32 | # |
---|
33 | # |
---|
34 | # |
---|
35 | |
---|
36 | class SQLStorage : |
---|
37 | def getUserId(self, username) : |
---|
38 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
---|
39 | try : |
---|
40 | return self.doParseResult(result)[0]["id"] |
---|
41 | except TypeError : # Not found |
---|
42 | return |
---|
43 | |
---|
44 | def getPrinterId(self, printername) : |
---|
45 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
46 | try : |
---|
47 | return self.doParseResult(result)[0]["id"] |
---|
48 | except TypeError : # Not found |
---|
49 | return |
---|
50 | |
---|
51 | def getPrinterPageCounter(self, printername) : |
---|
52 | result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
53 | try : |
---|
54 | return self.doParseResult(result)[0] |
---|
55 | except TypeError : # Not found |
---|
56 | return |
---|
57 | |
---|
58 | def updatePrinterPageCounter(self, printername, username, pagecount) : |
---|
59 | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) |
---|
60 | |
---|
61 | def addUserPQuota(self, username, printername) : |
---|
62 | printerid = self.getPrinterId(printername) |
---|
63 | if printerid is None : |
---|
64 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
---|
65 | printerid = self.getPrinterId(printername) |
---|
66 | userid = self.getUserId(username) |
---|
67 | if userid is None : |
---|
68 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
---|
69 | userid = self.getUserId(username) |
---|
70 | if (printerid is not None) and (userid is not None) : |
---|
71 | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
72 | |
---|
73 | def getUserPQuota(self, username, printername) : |
---|
74 | 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)))) |
---|
75 | try : |
---|
76 | return self.doParseResult(result)[0] |
---|
77 | except TypeError : # Not found |
---|
78 | return |
---|
79 | |
---|
80 | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
---|
81 | 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)))) |
---|
82 | |
---|
83 | def updateUserPQuota(self, username, printername, pagecount) : |
---|
84 | 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)))) |
---|
85 | |
---|
86 | def buyUserPQuota(self, username, printername, pagebought) : |
---|
87 | self.updateUserPQuota(username, printername, -pagebought) |
---|
88 | |
---|