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

Revision 812, 9.5 kB (checked in by jalet, 21 years ago)

DATETIME is not supported anymore in PostgreSQL 7.3 it seems, but
TIMESTAMP is.

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