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

Revision 732, 8.1 kB (checked in by jalet, 21 years ago)

Typos

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