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

Revision 1144, 22.7 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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