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

Revision 1131, 22.4 kB (checked in by jalet, 21 years ago)

Caching mechanism now caches all that's cacheable.

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