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

Revision 900, 11.4 kB (checked in by jalet, 21 years ago)

Job history added. Upgrade script neutralized for now !

  • 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# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
22# $Log$
23# Revision 1.22  2003/04/10 21:47:20  jalet
24# Job history added. Upgrade script neutralized for now !
25#
26# Revision 1.21  2003/04/08 20:38:08  jalet
27# The last job Id is saved now for each printer, this will probably
28# allow other accounting methods in the future.
29#
30# Revision 1.20  2003/03/29 13:45:27  jalet
31# GPL paragraphs were incorrectly (from memory) copied into the sources.
32# Two README files were added.
33# Upgrade script for PostgreSQL pre 1.01 schema was added.
34#
35# Revision 1.19  2003/02/27 08:41:49  jalet
36# DATETIME is not supported anymore in PostgreSQL 7.3 it seems, but
37# TIMESTAMP is.
38#
39# Revision 1.18  2003/02/10 12:07:31  jalet
40# Now repykota should output the recorded total page number for each printer too.
41#
42# Revision 1.17  2003/02/10 08:41:36  jalet
43# edpykota's --reset command line option resets the limit date too.
44#
45# Revision 1.16  2003/02/08 22:39:46  jalet
46# --reset command line option added
47#
48# Revision 1.15  2003/02/08 22:12:09  jalet
49# Life time counter for users and groups added.
50#
51# Revision 1.14  2003/02/07 22:13:13  jalet
52# Perhaps edpykota is now able to add printers !!! Oh, stupid me !
53#
54# Revision 1.13  2003/02/07 00:08:52  jalet
55# Typos
56#
57# Revision 1.12  2003/02/06 23:20:03  jalet
58# warnpykota doesn't need any user/group name argument, mimicing the
59# warnquota disk quota tool.
60#
61# Revision 1.11  2003/02/06 15:05:13  jalet
62# self was forgotten
63#
64# Revision 1.10  2003/02/06 15:03:11  jalet
65# added a method to set the limit date
66#
67# Revision 1.9  2003/02/06 14:52:35  jalet
68# Forgotten import
69#
70# Revision 1.8  2003/02/06 14:49:04  jalet
71# edpykota should be ok now
72#
73# Revision 1.7  2003/02/06 14:28:59  jalet
74# edpykota should be ok, minus some typos
75#
76# Revision 1.6  2003/02/06 09:19:02  jalet
77# More robust behavior (hopefully) when the user or printer is not managed
78# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
79# printer and/or user not 'yet?' in storage.
80#
81# Revision 1.5  2003/02/05 23:26:22  jalet
82# Incorrect handling of grace delay
83#
84# Revision 1.4  2003/02/05 23:02:10  jalet
85# Typo
86#
87# Revision 1.3  2003/02/05 23:00:12  jalet
88# Forgotten import
89# Bad datetime conversion
90#
91# Revision 1.2  2003/02/05 22:28:38  jalet
92# More robust storage
93#
94# Revision 1.1  2003/02/05 21:28:17  jalet
95# Initial import into CVS
96#
97#
98#
99
100import fnmatch
101
102class SQLStorage :   
103    def getMatchingPrinters(self, printerpattern) :
104        """Returns the list of all printers as tuples (id, name) for printer names which match a certain pattern."""
105        printerslist = []
106        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
107        # but we don't because other storages semantics may be different, so every
108        # storage should use fnmatch to match patterns and be storage agnostic
109        result = self.doQuery("SELECT id, printername FROM printers;")
110        result = self.doParseResult(result)
111        if result is not None :
112            for printer in result :
113                if fnmatch.fnmatchcase(printer["printername"], printerpattern) :
114                    printerslist.append((printer["id"], printer["printername"]))
115        return printerslist       
116           
117    def getPrinterId(self, printername) :       
118        """Returns a printerid given a printername."""
119        result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername))
120        try :
121            return self.doParseResult(result)[0]["id"]
122        except TypeError :      # Not found   
123            return
124           
125    def getUserId(self, username) :
126        """Returns a userid given a username."""
127        result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username))
128        try :
129            return self.doParseResult(result)[0]["id"]
130        except TypeError :      # Not found
131            return
132           
133    def getGroupId(self, groupname) :
134        """Returns a groupid given a grupname."""
135        result = self.doQuery("SELECT id FROM groups WHERE groupname=%s;" % self.doQuote(groupname))
136        try :
137            return self.doParseResult(result)[0]["id"]
138        except TypeError :      # Not found
139            return
140           
141    def getJobHistoryId(self, jobid, userid, printerid) :       
142        """Returns the history line's id given a (jobid, userid, printerid)."""
143        result = self.doQuery("SELECT id FROM jobhistory WHERE jobid=%s AND userid=%s AND printerid=%s;" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid)))
144        try :
145            return self.doParseResult(result)[0]["id"]
146        except TypeError :      # Not found   
147            return
148           
149    def getPrinterUsers(self, printerid) :       
150        """Returns the list of usernames which uses a given printer."""
151        result = self.doQuery("SELECT DISTINCT id, username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid=%s) ORDER BY username;" % self.doQuote(printerid))
152        result = self.doParseResult(result)
153        if result is None :
154            return []
155        else :   
156            return [(record["id"], record["username"]) for record in result]
157       
158    def getPrinterGroups(self, printerid) :       
159        """Returns the list of groups which uses a given printer."""
160        result = self.doQuery("SELECT DISTINCT id, groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid=%s);" % self.doQuote(printerid))
161        result = self.doParseResult(result)
162        if result is None :
163            return []
164        else :   
165            return [(record["id"], record["groupname"]) for record in result]
166       
167    def addPrinter(self, printername) :       
168        """Adds a printer to the quota storage, returns its id."""
169        self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername))
170        return self.getPrinterId(printername)
171       
172    def addUser(self, username) :       
173        """Adds a user to the quota storage, returns its id."""
174        self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username))
175        return self.getUserId(username)
176       
177    def addGroup(self, groupname) :       
178        """Adds a group to the quota storage, returns its id."""
179        self.doQuery("INSERT INTO groups (groupname) VALUES (%s);" % self.doQuote(groupname))
180        return self.getGroupId(groupname)
181       
182    def addUserPQuota(self, username, printerid) :
183        """Initializes a user print quota on a printer, adds the user to the quota storage if needed."""
184        userid = self.getUserId(username)     
185        if userid is None :   
186            userid = self.addUser(username)
187        self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid)))
188        return (userid, printerid)
189       
190    def addGroupPQuota(self, groupname, printerid) :
191        """Initializes a group print quota on a printer, adds the group to the quota storage if needed."""
192        groupid = self.getGroupId(groupname)     
193        if groupid is None :   
194            groupid = self.addUser(groupname)
195        self.doQuery("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s);" % (self.doQuote(groupid), self.doQuote(printerid)))
196        return (groupid, printerid)
197       
198    def setUserPQuota(self, userid, printerid, softlimit, hardlimit) :
199        """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid)."""
200        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)))
201       
202    def resetUserPQuota(self, userid, printerid) :   
203        """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged."""
204        self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid)))
205       
206    def updateUserPQuota(self, userid, printerid, pagecount) :
207        """Updates the used user Quota information given (userid, printerid) and a job size in pages."""
208        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)))
209       
210    def getUserPQuota(self, userid, printerid) :
211        """Returns the Print Quota information for a given (userid, printerid)."""
212        result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid)))
213        try :
214            return self.doParseResult(result)[0]
215        except TypeError :      # Not found   
216            return
217       
218    def setUserDateLimit(self, userid, printerid, datelimit) :
219        """Sets the limit date for a soft limit to become an hard one given (userid, printerid)."""
220        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)))
221       
222    def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) :
223        """Adds a job to the history: (jobid, userid, printerid, last page counter taken from requester)."""
224        self.doQuery("INSERT INTO jobhistory (jobid, userid, printerid, pagecounter, action) VALUES (%s, %s, %s, %s, %s);" % (self.doQuote(jobid), self.doQuote(userid), self.doQuote(printerid), self.doQuote(pagecounter), self.doQuote(action)))
225        return self.getJobHistoryId(jobid, userid, printerid) # in case jobid is not sufficient
226   
227    def updateJobSizeInHistory(self, historyid, jobsize) :
228        """Updates a job size in the history given the history line's id."""
229        self.doQuery("UPDATE jobhistory SET jobsize=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(historyid)))
230   
231    def getPrinterPageCounter(self, printerid) :
232        """Returns the last page counter value for a printer given its id, also returns last username, last jobid and history line id."""
233        result = self.doQuery("SELECT jobhistory.id, jobid, userid, username, pagecounter FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1;" % self.doQuote(printerid))
234        try :
235            return self.doParseResult(result)[0]
236        except TypeError :      # Not found
237            return
238       
Note: See TracBrowser for help on using the browser.