root / pykota / trunk / pykota / storages / pgstorage.py @ 1133

Revision 1133, 22.5 kB (checked in by jalet, 21 years ago)

Several optimizations, especially with LDAP backend

  • 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 and LPRng
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.15  2003/10/03 12:27:03  jalet
24# Several optimizations, especially with LDAP backend
25#
26# Revision 1.14  2003/10/03 08:57:55  jalet
27# Caching mechanism now caches all that's cacheable.
28#
29# Revision 1.13  2003/10/02 20:23:18  jalet
30# Storage caching mechanism added.
31#
32# Revision 1.12  2003/08/17 14:20:25  jalet
33# Bug fix by Oleg Biteryakov
34#
35# Revision 1.11  2003/07/29 20:55:17  jalet
36# 1.14 is out !
37#
38# Revision 1.10  2003/07/16 21:53:08  jalet
39# Really big modifications wrt new configuration file's location and content.
40#
41# Revision 1.9  2003/07/14 17:20:15  jalet
42# Bug in postgresql storage when modifying the prices for a printer
43#
44# Revision 1.8  2003/07/14 14:18:17  jalet
45# Wrong documentation strings
46#
47# Revision 1.7  2003/07/09 20:17:07  jalet
48# Email field added to PostgreSQL schema
49#
50# Revision 1.6  2003/07/07 11:49:24  jalet
51# Lots of small fixes with the help of PyChecker
52#
53# Revision 1.5  2003/07/07 08:33:19  jalet
54# Bug fix due to a typo in LDAP code
55#
56# Revision 1.4  2003/06/30 13:54:21  jalet
57# Sorts by user / group name
58#
59# Revision 1.3  2003/06/25 14:10:01  jalet
60# Hey, it may work (edpykota --reset excepted) !
61#
62# Revision 1.2  2003/06/12 21:09:57  jalet
63# wrongly placed code.
64#
65# Revision 1.1  2003/06/10 16:37:54  jalet
66# Deletion of the second user which is not needed anymore.
67# Added a debug configuration field in /etc/pykota.conf
68# All queries can now be sent to the logger in debug mode, this will
69# greatly help improve performance when time for this will come.
70#
71#
72#
73#
74
75from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
76
77try :
78    import pg
79except ImportError :   
80    import sys
81    # TODO : to translate or not to translate ?
82    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
83
84class Storage(BaseStorage) :
85    def __init__(self, pykotatool, host, dbname, user, passwd) :
86        """Opens the PostgreSQL database connection."""
87        BaseStorage.__init__(self, pykotatool)
88        try :
89            (host, port) = host.split(":")
90            port = int(port)
91        except ValueError :   
92            port = -1         # Use PostgreSQL's default tcp/ip port (5432).
93       
94        try :
95            self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)
96        except pg.error, msg :
97            raise PyKotaStorageError, msg
98        else :   
99            self.closed = 0
100            self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
101           
102    def close(self) :   
103        """Closes the database connection."""
104        if not self.closed :
105            self.database.close()
106            self.closed = 1
107            self.tool.logdebug("Database closed.")
108       
109    def beginTransaction(self) :   
110        """Starts a transaction."""
111        self.database.query("BEGIN;")
112        self.tool.logdebug("Transaction begins...")
113       
114    def commitTransaction(self) :   
115        """Commits a transaction."""
116        self.database.query("COMMIT;")
117        self.tool.logdebug("Transaction committed.")
118       
119    def rollbackTransaction(self) :     
120        """Rollbacks a transaction."""
121        self.database.query("ROLLBACK;")
122        self.tool.logdebug("Transaction aborted.")
123       
124    def doSearch(self, query) :
125        """Does a search query."""
126        query = query.strip()   
127        if not query.endswith(';') :   
128            query += ';'
129        try :
130            self.tool.logdebug("QUERY : %s" % query)
131            result = self.database.query(query)
132        except pg.error, msg :   
133            raise PyKotaStorageError, msg
134        else :   
135            if (result is not None) and (result.ntuples() > 0) : 
136                return result.dictresult()
137           
138    def doModify(self, query) :
139        """Does a (possibly multiple) modify query."""
140        query = query.strip()   
141        if not query.endswith(';') :   
142            query += ';'
143        try :
144            self.tool.logdebug("QUERY : %s" % query)
145            result = self.database.query(query)
146        except pg.error, msg :   
147            raise PyKotaStorageError, msg
148        else :   
149            return result
150           
151    def doQuote(self, field) :
152        """Quotes a field for use as a string in SQL queries."""
153        if type(field) == type(0.0) : 
154            typ = "decimal"
155        elif type(field) == type(0) :   
156            typ = "int"
157        else :   
158            typ = "text"
159        return pg._quote(field, typ)
160       
161    def getUserFromBackend(self, username) :   
162        """Extracts user information given its name."""
163        user = StorageUser(self, username)
164        result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username))
165        if result :
166            fields = result[0]
167            user.ident = fields.get("id")
168            user.LimitBy = fields.get("limitby")
169            user.AccountBalance = fields.get("balance")
170            user.LifeTimePaid = fields.get("lifetimepaid")
171            user.Email = fields.get("email")
172            user.Exists = 1
173        return user
174       
175    def getGroupFromBackend(self, groupname) :   
176        """Extracts group information given its name."""
177        group = StorageGroup(self, groupname)
178        result = self.doSearch("SELECT * FROM groups WHERE groupname=%s LIMIT 1" % self.doQuote(groupname))
179        if result :
180            fields = result[0]
181            group.ident = fields.get("id")
182            group.LimitBy = fields.get("limitby")
183            result = self.doSearch("SELECT SUM(balance) AS balance, SUM(lifetimepaid) AS lifetimepaid FROM users WHERE id IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" % self.doQuote(group.ident))
184            if result :
185                fields = result[0]
186                group.AccountBalance = fields.get("balance")
187                group.LifeTimePaid = fields.get("lifetimepaid")
188            group.Exists = 1
189        return group
190       
191    def getPrinterFromBackend(self, printername) :       
192        """Extracts printer information given its name."""
193        printer = StoragePrinter(self, printername)
194        result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername))
195        if result :
196            fields = result[0]
197            printer.ident = fields.get("id")
198            printer.PricePerJob = fields.get("priceperjob")
199            printer.PricePerPage = fields.get("priceperpage")
200            printer.LastJob = self.getPrinterLastJob(printer)
201            printer.Exists = 1
202        return printer   
203       
204    def getUserPQuotaFromBackend(self, user, printer) :       
205        """Extracts a user print quota."""
206        userpquota = StorageUserPQuota(self, user, printer)
207        if user.Exists :
208            result = self.doSearch("SELECT id, lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(user.ident), self.doQuote(printer.ident)))
209            if result :
210                fields = result[0]
211                userpquota.ident = fields.get("id")
212                userpquota.PageCounter = fields.get("pagecounter")
213                userpquota.LifePageCounter = fields.get("lifepagecounter")
214                userpquota.SoftLimit = fields.get("softlimit")
215                userpquota.HardLimit = fields.get("hardlimit")
216                userpquota.DateLimit = fields.get("datelimit")
217                userpquota.Exists = 1
218        return userpquota
219       
220    def getGroupPQuotaFromBackend(self, group, printer) :       
221        """Extracts a group print quota."""
222        grouppquota = StorageGroupPQuota(self, group, printer)
223        if group.Exists :
224            result = self.doSearch("SELECT id, softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident)))
225            if result :
226                fields = result[0]
227                grouppquota.ident = fields.get("id")
228                grouppquota.SoftLimit = fields.get("softlimit")
229                grouppquota.HardLimit = fields.get("hardlimit")
230                grouppquota.DateLimit = fields.get("datelimit")
231                result = self.doSearch("SELECT SUM(lifepagecounter) AS lifepagecounter, SUM(pagecounter) AS pagecounter FROM userpquota WHERE printerid=%s AND userid IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" % (self.doQuote(printer.ident), self.doQuote(group.ident)))
232                if result :
233                    fields = result[0]
234                    grouppquota.PageCounter = fields.get("pagecounter")
235                    grouppquota.LifePageCounter = fields.get("lifepagecounter")
236                grouppquota.Exists = 1
237        return grouppquota
238       
239    def getPrinterLastJobFromBackend(self, printer) :       
240        """Extracts a printer's last job information."""
241        lastjob = StorageLastJob(self, printer)
242        result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobdate FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident))
243        if result :
244            fields = result[0]
245            lastjob.ident = fields.get("id")
246            lastjob.JobId = fields.get("jobid")
247            lastjob.User = self.getUser(fields.get("username"))
248            lastjob.PrinterPageCounter = fields.get("pagecounter")
249            lastjob.JobSize = fields.get("jobsize")
250            lastjob.JobAction = fields.get("action")
251            lastjob.JobDate = fields.get("jobdate")
252            lastjob.Exists = 1
253        return lastjob
254           
255    def getUserGroups(self, user) :       
256        """Returns the user's groups list."""
257        groups = []
258        result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident))
259        if result :
260            for record in result :
261                groups.append(self.getGroup(record.get("groupname")))
262        return groups       
263       
264    def getGroupMembers(self, group) :       
265        """Returns the group's members list."""
266        groupmembers = []
267        result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident))
268        if result :
269            for record in result :
270                user = StorageUser(self, record.get("username"))
271                user.ident = record.get("userid")
272                user.LimitBy = record.get("limitby")
273                user.AccountBalance = record.get("balance")
274                user.LifeTimePaid = record.get("lifetimepaid")
275                user.Email = record.get("email")
276                user.Exists = 1
277                groupmembers.append(user)
278                self.cacheEntry("USERS", user.Name, user)
279        return groupmembers       
280       
281    def getMatchingPrinters(self, printerpattern) :
282        """Returns the list of all printers for which name matches a certain pattern."""
283        printers = []
284        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
285        # but we don't because other storages semantics may be different, so every
286        # storage should use fnmatch to match patterns and be storage agnostic
287        result = self.doSearch("SELECT * FROM printers")
288        if result :
289            for record in result :
290                if self.tool.matchString(record["printername"], [ printerpattern ]) :
291                    printer = StoragePrinter(self, record["printername"])
292                    printer.ident = record.get("id")
293                    printer.PricePerJob = record.get("priceperjob")
294                    printer.PricePerPage = record.get("priceperpage")
295                    printer.LastJob = self.getPrinterLastJob(printer)
296                    printer.Exists = 1
297                    printers.append(printer)
298                    self.cacheEntry("PRINTERS", printer.Name, printer)
299        return printers       
300       
301    def getPrinterUsersAndQuotas(self, printer, names=None) :       
302        """Returns the list of users who uses a given printer, along with their quotas."""
303        usersandquotas = []
304        result = self.doSearch("SELECT users.id as uid,username,balance,lifetimepaid,limitby,email,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident))
305        if result :
306            for record in result :
307                if (names is None) or self.tool.matchString(record.get("username"), names) :
308                    user = StorageUser(self, record.get("username"))
309                    user.ident = record.get("uid")
310                    user.LimitBy = record.get("limitby")
311                    user.AccountBalance = record.get("balance")
312                    user.LifeTimePaid = record.get("lifetimepaid")
313                    user.Email = record.get("email") 
314                    user.Exists = 1
315                    userpquota = StorageUserPQuota(self, user, printer)
316                    userpquota.ident = record.get("id")
317                    userpquota.PageCounter = record.get("pagecounter")
318                    userpquota.LifePageCounter = record.get("lifepagecounter")
319                    userpquota.SoftLimit = record.get("softlimit")
320                    userpquota.HardLimit = record.get("hardlimit")
321                    userpquota.DateLimit = record.get("datelimit")
322                    userpquota.Exists = 1
323                    usersandquotas.append((user, userpquota))
324                    self.cacheEntry("USERS", user.Name, user)
325                    self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota)
326        return usersandquotas
327               
328    def getPrinterGroupsAndQuotas(self, printer, names=None) :       
329        """Returns the list of groups which uses a given printer, along with their quotas."""
330        groupsandquotas = []
331        result = self.doSearch("SELECT groupname FROM groups JOIN grouppquota ON groups.id=grouppquota.groupid AND printerid=%s ORDER BY groupname ASC" % self.doQuote(printer.ident))
332        if result :
333            for record in result :
334                if (names is None) or self.tool.matchString(record.get("groupname"), names) :
335                    group = self.getGroup(record.get("groupname"))
336                    grouppquota = self.getGroupPQuota(group, printer)
337                    groupsandquotas.append((group, grouppquota))
338        return groupsandquotas
339       
340    def addPrinter(self, printername) :       
341        """Adds a printer to the quota storage, returns it."""
342        self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername))
343        return self.getPrinter(printername)
344       
345    def addUser(self, user) :       
346        """Adds a user to the quota storage, returns its id."""
347        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email) VALUES (%s, %s, %s, %s, %s)" % (self.doQuote(user.Name), self.doQuote(user.LimitBy), self.doQuote(user.AccountBalance), self.doQuote(user.LifeTimePaid), self.doQuote(user.Email)))
348        return self.getUser(user.Name)
349       
350    def addGroup(self, group) :       
351        """Adds a group to the quota storage, returns its id."""
352        self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy)))
353        return self.getGroup(group.Name)
354
355    def addUserToGroup(self, user, group) :   
356        """Adds an user to a group."""
357        result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident)))
358        try :
359            mexists = int(result[0].get("mexists"))
360        except (IndexError, TypeError) :   
361            mexists = 0
362        if not mexists :   
363            self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident)))
364           
365    def addUserPQuota(self, user, printer) :
366        """Initializes a user print quota on a printer."""
367        self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident)))
368        return self.getUserPQuota(user, printer)
369       
370    def addGroupPQuota(self, group, printer) :
371        """Initializes a group print quota on a printer."""
372        self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident)))
373        return self.getGroupPQuota(group, printer)
374       
375    def writePrinterPrices(self, printer) :   
376        """Write the printer's prices back into the storage."""
377        self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident)))
378       
379    def writeUserLimitBy(self, user, limitby) :   
380        """Sets the user's limiting factor."""
381        self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident)))
382       
383    def writeGroupLimitBy(self, group, limitby) :   
384        """Sets the group's limiting factor."""
385        self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident)))
386       
387    def writeUserPQuotaDateLimit(self, userpquota, datelimit) :   
388        """Sets the date limit permanently for a user print quota."""
389        self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident)))
390           
391    def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) :   
392        """Sets the date limit permanently for a group print quota."""
393        self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident)))
394       
395    def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) :   
396       """Sets the new page counters permanently for a user print quota."""
397       self.doModify("UPDATE userpquota SET pagecounter=%s,lifepagecounter=%s WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident)))
398       
399    def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) :   
400       """Sets the new account balance and eventually new lifetime paid."""
401       if newlifetimepaid is not None :
402           self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident)))
403       else :   
404           self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident)))
405           
406    def writeLastJobSize(self, lastjob, jobsize) :       
407        """Sets the last job's size permanently."""
408        self.doModify("UPDATE jobhistory SET jobsize=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(lastjob.ident)))
409       
410    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None) :   
411        """Adds a job in a printer's history."""
412        if jobsize is not None :
413            self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize) VALUES (%s, %s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action), self.doQuote(jobsize)))
414        else :   
415            self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action) VALUES (%s, %s, %s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident), self.doQuote(jobid), self.doQuote(pagecounter), self.doQuote(action)))
416           
417    def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) :
418        """Sets soft and hard limits for a user quota."""
419        self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident)))
420       
421    def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) :
422        """Sets soft and hard limits for a group quota on a specific printer."""
423        self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident)))
424
425    def deleteUser(self, user) :   
426        """Completely deletes an user from the Quota Storage."""
427        # TODO : What should we do if we delete the last person who used a given printer ?
428        # TODO : we can't reassign the last job to the previous one, because next user would be
429        # TODO : incorrectly charged (overcharged).
430        for q in [ 
431                    "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident),
432                    "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident),
433                    "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident),
434                    "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident),
435                  ] :
436            self.doModify(q)
437       
438    def deleteGroup(self, group) :   
439        """Completely deletes a group from the Quota Storage."""
440        for q in [
441                   "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident),
442                   "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident),
443                   "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident),
444                 ] : 
445            self.doModify(q)
446       
Note: See TracBrowser for help on using the browser.