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

Revision 723, 6.9 kB (checked in by jalet, 21 years ago)

self was forgotten

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