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.8 2003/02/06 14:49:04 jalet |
---|
18 | # edpykota should be ok now |
---|
19 | # |
---|
20 | # Revision 1.7 2003/02/06 14:28:59 jalet |
---|
21 | # edpykota should be ok, minus some typos |
---|
22 | # |
---|
23 | # Revision 1.6 2003/02/06 09:19:02 jalet |
---|
24 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
25 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
26 | # printer and/or user not 'yet?' in storage. |
---|
27 | # |
---|
28 | # Revision 1.5 2003/02/05 23:26:22 jalet |
---|
29 | # Incorrect handling of grace delay |
---|
30 | # |
---|
31 | # Revision 1.4 2003/02/05 23:02:10 jalet |
---|
32 | # Typo |
---|
33 | # |
---|
34 | # Revision 1.3 2003/02/05 23:00:12 jalet |
---|
35 | # Forgotten import |
---|
36 | # Bad datetime conversion |
---|
37 | # |
---|
38 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
39 | # More robust storage |
---|
40 | # |
---|
41 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
42 | # Initial import into CVS |
---|
43 | # |
---|
44 | # |
---|
45 | # |
---|
46 | |
---|
47 | class SQLStorage : |
---|
48 | def getMatchingPrinters(self, printerpattern) : |
---|
49 | """Returns the list of all printer names which match a certain pattern.""" |
---|
50 | printerslist = [] |
---|
51 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
52 | # but we don't because other storages semantics may be different, so every |
---|
53 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
54 | result = self.doQuery("SELECT printername FROM printers;") |
---|
55 | result = self.doParseResult(result) |
---|
56 | if result is not None : |
---|
57 | for printer in result : |
---|
58 | if fnmatch.fnmatchcase(printer["printername"], printerpattern) : |
---|
59 | printerslist.append(printer["printername"]) |
---|
60 | return printerslist |
---|
61 | |
---|
62 | def getUserId(self, username) : |
---|
63 | """Returns a userid given a username.""" |
---|
64 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
---|
65 | try : |
---|
66 | return self.doParseResult(result)[0]["id"] |
---|
67 | except TypeError : # Not found |
---|
68 | return |
---|
69 | |
---|
70 | def getPrinterId(self, printername) : |
---|
71 | """Returns a printerid given a printername.""" |
---|
72 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
73 | try : |
---|
74 | return self.doParseResult(result)[0]["id"] |
---|
75 | except TypeError : # Not found |
---|
76 | return |
---|
77 | |
---|
78 | def getPrinterPageCounter(self, printername) : |
---|
79 | """Returns the last page counter value for a printer given its name.""" |
---|
80 | result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
81 | try : |
---|
82 | return self.doParseResult(result)[0] |
---|
83 | except TypeError : # Not found |
---|
84 | return |
---|
85 | |
---|
86 | def updatePrinterPageCounter(self, printername, username, pagecount) : |
---|
87 | """Updates the last page counter information for a printer given its name, last username and pagecount.""" |
---|
88 | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) |
---|
89 | |
---|
90 | def addUserPQuota(self, username, printername) : |
---|
91 | (userid, printerid) = self.getUPIds(username, printername) |
---|
92 | if printerid is None : |
---|
93 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
---|
94 | if userid is None : |
---|
95 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
---|
96 | (userid, printerid) = self.getUPIds(username, printername) |
---|
97 | if (userid is not None) and (printerid is not None) : |
---|
98 | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
99 | |
---|
100 | def getUPIds(self, username, printername) : |
---|
101 | """Returns a tuple (userid, printerid) given a username and a printername.""" |
---|
102 | return (self.getUserId(username), self.getPrinterId(printername)) |
---|
103 | |
---|
104 | def getUserPQuota(self, username, printername) : |
---|
105 | """Returns the Print Quota information for a given (username, printername).""" |
---|
106 | (userid, printerid) = self.getUPIds(username, printername) |
---|
107 | if (userid is not None) and (printerid is not None) : |
---|
108 | result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
109 | try : |
---|
110 | return self.doParseResult(result)[0] |
---|
111 | except TypeError : # Not found |
---|
112 | pass |
---|
113 | |
---|
114 | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
---|
115 | """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" |
---|
116 | (userid, printerid) = self.getUPIds(username, printername) |
---|
117 | if (userid is not None) and (printerid is not None) : |
---|
118 | 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))) |
---|
119 | |
---|
120 | def updateUserPQuota(self, username, printername, pagecount) : |
---|
121 | """Updates the used user Quota information given (username, printername) and a job size in pages.""" |
---|
122 | (userid, printerid) = self.getUPIds(username, printername) |
---|
123 | if (userid is not None) and (printerid is not None) : |
---|
124 | self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) |
---|
125 | |
---|
126 | def buyUserPQuota(self, username, printername, pagebought) : |
---|
127 | """Buys pages for a given (username, printername).""" |
---|
128 | self.updateUserPQuota(username, printername, -pagebought) |
---|
129 | |
---|