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

Revision 1130, 22.0 kB (checked in by jalet, 21 years ago)

Storage caching mechanism added.

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