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

Revision 748, 8.4 kB (checked in by jalet, 21 years ago)

Perhaps edpykota is now able to add printers !!! Oh, stupid me !

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