root / pykota / trunk / pykota / storages / sql.py @ 791

Revision 791, 9.4 kB (checked in by jalet, 21 years ago)

Now repykota should output the recorded total page number for each printer too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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.18  2003/02/10 12:07:31  jalet
18# Now repykota should output the recorded total page number for each printer too.
19#
20# Revision 1.17  2003/02/10 08:41:36  jalet
21# edpykota's --reset command line option resets the limit date too.
22#
23# Revision 1.16  2003/02/08 22:39:46  jalet
24# --reset command line option added
25#
26# Revision 1.15  2003/02/08 22:12:09  jalet
27# Life time counter for users and groups added.
28#
29# Revision 1.14  2003/02/07 22:13:13  jalet
30# Perhaps edpykota is now able to add printers !!! Oh, stupid me !
31#
32# Revision 1.13  2003/02/07 00:08:52  jalet
33# Typos
34#
35# Revision 1.12  2003/02/06 23:20:03  jalet
36# warnpykota doesn't need any user/group name argument, mimicing the
37# warnquota disk quota tool.
38#
39# Revision 1.11  2003/02/06 15:05:13  jalet
40# self was forgotten
41#
42# Revision 1.10  2003/02/06 15:03:11  jalet
43# added a method to set the limit date
44#
45# Revision 1.9  2003/02/06 14:52:35  jalet
46# Forgotten import
47#
48# Revision 1.8  2003/02/06 14:49:04  jalet
49# edpykota should be ok now
50#
51# Revision 1.7  2003/02/06 14:28:59  jalet
52# edpykota should be ok, minus some typos
53#
54# Revision 1.6  2003/02/06 09:19:02  jalet
55# More robust behavior (hopefully) when the user or printer is not managed
56# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
57# printer and/or user not 'yet?' in storage.
58#
59# Revision 1.5  2003/02/05 23:26:22  jalet
60# Incorrect handling of grace delay
61#
62# Revision 1.4  2003/02/05 23:02:10  jalet
63# Typo
64#
65# Revision 1.3  2003/02/05 23:00:12  jalet
66# Forgotten import
67# Bad datetime conversion
68#
69# Revision 1.2  2003/02/05 22:28:38  jalet
70# More robust storage
71#
72# Revision 1.1  2003/02/05 21:28:17  jalet
73# Initial import into CVS
74#
75#
76#
77
78import fnmatch
79
80class SQLStorage :   
81    def getMatchingPrinters(self, printerpattern) :
82        """Returns the list of all printers tuples (name, pagecounter) which match a certain pattern for the printer name."""
83        printerslist = []
84        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
85        # but we don't because other storages semantics may be different, so every
86        # storage should use fnmatch to match patterns and be storage agnostic
87        result = self.doQuery("SELECT printername, pagecounter FROM printers;")
88        result = self.doParseResult(result)
89        if result is not None :
90            for printer in result :
91                if fnmatch.fnmatchcase(printer["printername"], printerpattern) :
92                    printerslist.append((printer["printername"], printer["pagecounter"]))
93        return printerslist       
94           
95    def addPrinter(self, printername) :       
96        """Adds a printer to the quota storage."""
97        self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername))
98       
99    def getPrinterUsers(self, printername) :       
100        """Returns the list of usernames which uses a given printer."""
101        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))
102        result = self.doParseResult(result)
103        if result is None :
104            return []
105        else :   
106            return [record["username"] for record in result]
107       
108    def getPrinterGroups(self, printername) :       
109        """Returns the list of groups which uses a given printer."""
110        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))
111        result = self.doParseResult(result)
112        if result is None :
113            return []
114        else :   
115            return [record["groupname"] for record in result]
116       
117    def getUserId(self, username) :
118        """Returns a userid given a username."""
119        result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username))
120        try :
121            return self.doParseResult(result)[0]["id"]
122        except TypeError :      # Not found
123            return
124           
125    def getPrinterId(self, printername) :       
126        """Returns a printerid given a printername."""
127        result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername))
128        try :
129            return self.doParseResult(result)[0]["id"]
130        except TypeError :      # Not found   
131            return
132           
133    def getPrinterPageCounter(self, printername) :
134        """Returns the last page counter value for a printer given its name."""
135        result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername))
136        try :
137            return self.doParseResult(result)[0]
138        except TypeError :      # Not found
139            return
140       
141    def updatePrinterPageCounter(self, printername, username, pagecount) :
142        """Updates the last page counter information for a printer given its name, last username and pagecount."""
143        return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername)))
144       
145    def addUserPQuota(self, username, printername) :
146        """Initializes a user print quota on a printer, adds the printer and the user to the quota storage if needed."""
147        (userid, printerid) = self.getUPIds(username, printername)
148        if printerid is None :   
149            self.addPrinter(printername)        # should we still add it ?
150        if userid is None :   
151            self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username))
152        (userid, printerid) = self.getUPIds(username, printername)
153        if (userid is not None) and (printerid is not None) :
154            return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid)))
155       
156    def getUPIds(self, username, printername) :   
157        """Returns a tuple (userid, printerid) given a username and a printername."""
158        return (self.getUserId(username), self.getPrinterId(printername))
159       
160    def getUserPQuota(self, username, printername) :
161        """Returns the Print Quota information for a given (username, printername)."""
162        (userid, printerid) = self.getUPIds(username, printername)
163        if (userid is not None) and (printerid is not None) :
164            result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid)))
165            try :
166                return self.doParseResult(result)[0]
167            except TypeError :      # Not found   
168                pass
169       
170    def setUserPQuota(self, username, printername, softlimit, hardlimit) :
171        """Sets soft and hard limits for a user quota on a specific printer given (username, printername)."""
172        (userid, printerid) = self.getUPIds(username, printername)
173        if (userid is not None) and (printerid is not None) :
174            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)))
175       
176    def resetUserPQuota(self, username, printername) :   
177        """Resets the page counter to zero. Life time page counter is kept unchanged."""
178        (userid, printerid) = self.getUPIds(username, printername)
179        if (userid is not None) and (printerid is not None) :
180            self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid)))
181       
182    def setDateLimit(self, username, printername, datelimit) :
183        """Sets the limit date for a soft limit to become an hard one given (username, printername)."""
184        (userid, printerid) = self.getUPIds(username, printername)
185        if (userid is not None) and (printerid is not None) :
186            self.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME 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)))
187       
188    def updateUserPQuota(self, username, printername, pagecount) :
189        """Updates the used user Quota information given (username, printername) and a job size in pages."""
190        (userid, printerid) = self.getUPIds(username, printername)
191        if (userid is not None) and (printerid is not None) :
192            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)))
193       
Note: See TracBrowser for help on using the browser.