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

Revision 1258, 26.8 kB (checked in by jalet, 20 years ago)

edpykota now supports adding printers to printer groups.

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