root / pykota / trunk / pykota / storages / sql.py @ 1531

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

Payment now gets deleted when the user is deleted

  • 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.42  2004/06/08 17:44:43  jalet
25# Payment now gets deleted when the user is deleted
26#
27# Revision 1.41  2004/06/05 22:03:50  jalet
28# Payments history is now stored in database
29#
30# Revision 1.40  2004/06/03 23:14:11  jalet
31# Now stores the job's size in bytes in the database.
32# Preliminary work on payments storage : database schemas are OK now,
33# but no code to store payments yet.
34# Removed schema picture, not relevant anymore.
35#
36# Revision 1.39  2004/05/26 14:50:12  jalet
37# First try at saving the job-originating-hostname in the database
38#
39# Revision 1.38  2004/05/06 12:37:47  jalet
40# pkpgcounter : comments
41# pkprinters : when --add is used, existing printers are now skipped.
42#
43# Revision 1.37  2004/02/23 22:53:21  jalet
44# Don't retrieve data when it's not needed, to avoid database queries
45#
46# Revision 1.36  2004/02/04 13:24:41  jalet
47# pkprinters can now remove printers from printers groups.
48#
49# Revision 1.35  2004/02/04 11:17:00  jalet
50# pkprinters command line tool added.
51#
52# Revision 1.34  2004/02/02 22:44:16  jalet
53# Preliminary work on Relationnal Database Independance via DB-API 2.0
54#
55#
56#
57
58from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
59
60class SQLStorage :
61    def getAllUsersNames(self) :   
62        """Extracts all user names."""
63        usernames = []
64        result = self.doSearch("SELECT username FROM users")
65        if result :
66            usernames = [record["username"] for record in result]
67        return usernames
68       
69    def getAllGroupsNames(self) :   
70        """Extracts all group names."""
71        groupnames = []
72        result = self.doSearch("SELECT groupname FROM groups")
73        if result :
74            groupnames = [record["groupname"] for record in result]
75        return groupnames
76       
77    def getUserFromBackend(self, username) :   
78        """Extracts user information given its name."""
79        user = StorageUser(self, username)
80        result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username))
81        if result :
82            fields = result[0]
83            user.ident = fields.get("id")
84            user.Name = fields.get("username", username)
85            user.LimitBy = fields.get("limitby")
86            user.AccountBalance = fields.get("balance")
87            user.LifeTimePaid = fields.get("lifetimepaid")
88            user.Email = fields.get("email")
89            user.Exists = 1
90        return user
91       
92    def getGroupFromBackend(self, groupname) :   
93        """Extracts group information given its name."""
94        group = StorageGroup(self, groupname)
95        result = self.doSearch("SELECT * FROM groups WHERE groupname=%s LIMIT 1" % self.doQuote(groupname))
96        if result :
97            fields = result[0]
98            group.ident = fields.get("id")
99            group.Name = fields.get("groupname", groupname)
100            group.LimitBy = fields.get("limitby")
101            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))
102            if result :
103                fields = result[0]
104                group.AccountBalance = fields.get("balance")
105                group.LifeTimePaid = fields.get("lifetimepaid")
106            group.Exists = 1
107        return group
108       
109    def getPrinterFromBackend(self, printername) :       
110        """Extracts printer information given its name."""
111        printer = StoragePrinter(self, printername)
112        result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername))
113        if result :
114            fields = result[0]
115            printer.ident = fields.get("id")
116            printer.Name = fields.get("printername", printername)
117            printer.PricePerJob = fields.get("priceperjob")
118            printer.PricePerPage = fields.get("priceperpage")
119            printer.Exists = 1
120        return printer   
121       
122    def getUserPQuotaFromBackend(self, user, printer) :       
123        """Extracts a user print quota."""
124        userpquota = StorageUserPQuota(self, user, printer)
125        if printer.Exists and user.Exists :
126            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)))
127            if result :
128                fields = result[0]
129                userpquota.ident = fields.get("id")
130                userpquota.PageCounter = fields.get("pagecounter")
131                userpquota.LifePageCounter = fields.get("lifepagecounter")
132                userpquota.SoftLimit = fields.get("softlimit")
133                userpquota.HardLimit = fields.get("hardlimit")
134                userpquota.DateLimit = fields.get("datelimit")
135                userpquota.Exists = 1
136        return userpquota
137       
138    def getGroupPQuotaFromBackend(self, group, printer) :       
139        """Extracts a group print quota."""
140        grouppquota = StorageGroupPQuota(self, group, printer)
141        if group.Exists :
142            result = self.doSearch("SELECT id, softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident)))
143            if result :
144                fields = result[0]
145                grouppquota.ident = fields.get("id")
146                grouppquota.SoftLimit = fields.get("softlimit")
147                grouppquota.HardLimit = fields.get("hardlimit")
148                grouppquota.DateLimit = fields.get("datelimit")
149                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)))
150                if result :
151                    fields = result[0]
152                    grouppquota.PageCounter = fields.get("pagecounter")
153                    grouppquota.LifePageCounter = fields.get("lifepagecounter")
154                grouppquota.Exists = 1
155        return grouppquota
156       
157    def getPrinterLastJobFromBackend(self, printer) :       
158        """Extracts a printer's last job information."""
159        lastjob = StorageLastJob(self, printer)
160        result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident))
161        if result :
162            fields = result[0]
163            lastjob.ident = fields.get("id")
164            lastjob.JobId = fields.get("jobid")
165            lastjob.UserName = fields.get("username")
166            lastjob.PrinterPageCounter = fields.get("pagecounter")
167            lastjob.JobSize = fields.get("jobsize")
168            lastjob.JobPrice = fields.get("jobprice")
169            lastjob.JobAction = fields.get("action")
170            lastjob.JobFileName = fields.get("filename")
171            lastjob.JobTitle = fields.get("title")
172            lastjob.JobCopies = fields.get("copies")
173            lastjob.JobOptions = fields.get("options")
174            lastjob.JobDate = fields.get("jobdate")
175            lastjob.JobHostName = fields.get("hostname")
176            lastjob.JobSizeBytes = fields.get("jobsizebytes")
177            lastjob.Exists = 1
178        return lastjob
179           
180    def getGroupMembersFromBackend(self, group) :       
181        """Returns the group's members list."""
182        groupmembers = []
183        result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident))
184        if result :
185            for record in result :
186                user = StorageUser(self, record.get("username"))
187                user.ident = record.get("userid")
188                user.LimitBy = record.get("limitby")
189                user.AccountBalance = record.get("balance")
190                user.LifeTimePaid = record.get("lifetimepaid")
191                user.Email = record.get("email")
192                user.Exists = 1
193                groupmembers.append(user)
194                self.cacheEntry("USERS", user.Name, user)
195        return groupmembers       
196       
197    def getUserGroupsFromBackend(self, user) :       
198        """Returns the user's groups list."""
199        groups = []
200        result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident))
201        if result :
202            for record in result :
203                groups.append(self.getGroup(record.get("groupname")))
204        return groups       
205       
206    def getParentPrintersFromBackend(self, printer) :   
207        """Get all the printer groups this printer is a member of."""
208        pgroups = []
209        result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident))
210        if result :
211            for record in result :
212                if record["groupid"] != printer.ident : # in case of integrity violation
213                    parentprinter = self.getPrinter(record.get("printername"))
214                    if parentprinter.Exists :
215                        pgroups.append(parentprinter)
216        return pgroups
217       
218    def getMatchingPrinters(self, printerpattern) :
219        """Returns the list of all printers for which name matches a certain pattern."""
220        printers = []
221        # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ...
222        # but we don't because other storages semantics may be different, so every
223        # storage should use fnmatch to match patterns and be storage agnostic
224        result = self.doSearch("SELECT * FROM printers")
225        if result :
226            for record in result :
227                if self.tool.matchString(record["printername"], printerpattern.split(",")) :
228                    printer = StoragePrinter(self, record["printername"])
229                    printer.ident = record.get("id")
230                    printer.PricePerJob = record.get("priceperjob")
231                    printer.PricePerPage = record.get("priceperpage")
232                    printer.Exists = 1
233                    printers.append(printer)
234                    self.cacheEntry("PRINTERS", printer.Name, printer)
235        return printers       
236       
237    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :       
238        """Returns the list of users who uses a given printer, along with their quotas."""
239        usersandquotas = []
240        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))
241        if result :
242            for record in result :
243                if self.tool.matchString(record.get("username"), names) :
244                    user = StorageUser(self, record.get("username"))
245                    user.ident = record.get("uid")
246                    user.LimitBy = record.get("limitby")
247                    user.AccountBalance = record.get("balance")
248                    user.LifeTimePaid = record.get("lifetimepaid")
249                    user.Email = record.get("email") 
250                    user.Exists = 1
251                    userpquota = StorageUserPQuota(self, user, printer)
252                    userpquota.ident = record.get("id")
253                    userpquota.PageCounter = record.get("pagecounter")
254                    userpquota.LifePageCounter = record.get("lifepagecounter")
255                    userpquota.SoftLimit = record.get("softlimit")
256                    userpquota.HardLimit = record.get("hardlimit")
257                    userpquota.DateLimit = record.get("datelimit")
258                    userpquota.Exists = 1
259                    usersandquotas.append((user, userpquota))
260                    self.cacheEntry("USERS", user.Name, user)
261                    self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota)
262        return usersandquotas
263               
264    def getPrinterGroupsAndQuotas(self, printer, names=["*"]) :       
265        """Returns the list of groups which uses a given printer, along with their quotas."""
266        groupsandquotas = []
267        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))
268        if result :
269            for record in result :
270                if self.tool.matchString(record.get("groupname"), names) :
271                    group = self.getGroup(record.get("groupname"))
272                    grouppquota = self.getGroupPQuota(group, printer)
273                    groupsandquotas.append((group, grouppquota))
274        return groupsandquotas
275       
276    def addPrinter(self, printername) :       
277        """Adds a printer to the quota storage, returns it."""
278        self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername))
279        return self.getPrinter(printername)
280       
281    def addUser(self, user) :       
282        """Adds a user to the quota storage, returns its id."""
283        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)))
284        return self.getUser(user.Name)
285       
286    def addGroup(self, group) :       
287        """Adds a group to the quota storage, returns its id."""
288        self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy)))
289        return self.getGroup(group.Name)
290
291    def addUserToGroup(self, user, group) :   
292        """Adds an user to a group."""
293        result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident)))
294        try :
295            mexists = int(result[0].get("mexists"))
296        except (IndexError, TypeError) :   
297            mexists = 0
298        if not mexists :   
299            self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident)))
300           
301    def addUserPQuota(self, user, printer) :
302        """Initializes a user print quota on a printer."""
303        self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident)))
304        return self.getUserPQuota(user, printer)
305       
306    def addGroupPQuota(self, group, printer) :
307        """Initializes a group print quota on a printer."""
308        self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident)))
309        return self.getGroupPQuota(group, printer)
310       
311    def writePrinterPrices(self, printer) :   
312        """Write the printer's prices back into the storage."""
313        self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident)))
314       
315    def writeUserLimitBy(self, user, limitby) :   
316        """Sets the user's limiting factor."""
317        self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident)))
318       
319    def writeGroupLimitBy(self, group, limitby) :   
320        """Sets the group's limiting factor."""
321        self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident)))
322       
323    def writeUserPQuotaDateLimit(self, userpquota, datelimit) :   
324        """Sets the date limit permanently for a user print quota."""
325        self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident)))
326           
327    def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) :   
328        """Sets the date limit permanently for a group print quota."""
329        self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident)))
330       
331    def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) :   
332        """Increase page counters for a user print quota."""
333        self.doModify("UPDATE userpquota SET pagecounter=pagecounter+%s,lifepagecounter=lifepagecounter+%s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident)))
334       
335    def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) :   
336        """Sets the new page counters permanently for a user print quota."""
337        self.doModify("UPDATE userpquota SET pagecounter=%s,lifepagecounter=%s WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident)))
338       
339    def decreaseUserAccountBalance(self, user, amount) :   
340        """Decreases user's account balance from an amount."""
341        self.doModify("UPDATE users SET balance=balance-%s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident)))
342       
343    def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) :   
344        """Sets the new account balance and eventually new lifetime paid."""
345        if newlifetimepaid is not None :
346            self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident)))
347        else :   
348            self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident)))
349           
350    def writeNewPayment(self, user, amount) :       
351        """Adds a new payment to the payments history."""
352        self.doModify("INSERT INTO payments (userid, amount) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(amount)))
353       
354    def writeLastJobSize(self, lastjob, jobsize, jobprice) :       
355        """Sets the last job's size permanently."""
356        self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident)))
357       
358    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None) :   
359        """Adds a job in a printer's history."""
360        if (not self.disablehistory) or (not printer.LastJob.Exists) :
361            if jobsize is not None :
362                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes) VALUES (%s, %s, %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), self.doQuote(clienthost), self.doQuote(jobsizebytes)))
363            else :   
364                self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes) 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(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes)))
365        else :       
366            # here we explicitly want to reset jobsize to NULL if needed
367            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, hostname=%s, jobsizebytes=%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(clienthost), self.doQuote(jobsizebytes), self.doQuote(printer.LastJob.ident)))
368           
369    def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) :
370        """Sets soft and hard limits for a user quota."""
371        self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident)))
372       
373    def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) :
374        """Sets soft and hard limits for a group quota on a specific printer."""
375        self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident)))
376
377    def writePrinterToGroup(self, pgroup, printer) :
378        """Puts a printer into a printer group."""
379        children = []
380        result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident))
381        if result :
382            for record in result :
383                children.append(record.get("printerid")) # TODO : put this into the database integrity rules
384        if printer.ident not in children :       
385            self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident)))
386       
387    def removePrinterFromGroup(self, pgroup, printer) :
388        """Removes a printer from a printer group."""
389        self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident)))
390       
391    def retrieveHistory(self, user=None, printer=None, datelimit=None, hostname=None, limit=100) :   
392        """Retrieves all print jobs for user on printer (or all) before date, limited to first 100 results."""
393        query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid"
394        where = []
395        if (user is not None) and user.Exists :
396            where.append("userid=%s" % self.doQuote(user.ident))
397        if (printer is not None) and printer.Exists :
398            where.append("printerid=%s" % self.doQuote(printer.ident))
399        if hostname is not None :   
400            where.append("hostname=%s" % self.doQuote(hostname))
401        if datelimit is not None :   
402            where.append("jobdate<=%s" % self.doQuote(datelimit))
403        if where :   
404            query += " AND %s" % " AND ".join(where)
405        query += " ORDER BY id DESC"
406        if limit :
407            query += " LIMIT %s" % self.doQuote(int(limit))
408        jobs = []   
409        result = self.doSearch(query)   
410        if result :
411            for fields in result :
412                job = StorageJob(self)
413                job.ident = fields.get("id")
414                job.JobId = fields.get("jobid")
415                job.PrinterPageCounter = fields.get("pagecounter")
416                job.JobSize = fields.get("jobsize")
417                job.JobPrice = fields.get("jobprice")
418                job.JobAction = fields.get("action")
419                job.JobFileName = fields.get("filename")
420                job.JobTitle = fields.get("title")
421                job.JobCopies = fields.get("copies")
422                job.JobOptions = fields.get("options")
423                job.JobDate = fields.get("jobdate")
424                job.JobHostName = fields.get("hostname")
425                job.JobSizeBytes = fields.get("jobsizebytes")
426                job.UserName = fields.get("username")
427                job.PrinterName = fields.get("printername")
428                job.Exists = 1
429                jobs.append(job)
430        return jobs
431       
432    def deleteUser(self, user) :   
433        """Completely deletes an user from the Quota Storage."""
434        # TODO : What should we do if we delete the last person who used a given printer ?
435        # TODO : we can't reassign the last job to the previous one, because next user would be
436        # TODO : incorrectly charged (overcharged).
437        for q in [ 
438                    "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident),
439                    "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident),
440                    "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident),
441                    "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident),
442                    "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident),
443                  ] :
444            self.doModify(q)
445       
446    def deleteGroup(self, group) :   
447        """Completely deletes a group from the Quota Storage."""
448        for q in [
449                   "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident),
450                   "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident),
451                   "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident),
452                 ] : 
453            self.doModify(q)
454           
455    def deletePrinter(self, printer) :   
456        """Completely deletes a printer from the Quota Storage."""
457        for q in [ 
458                    "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)),
459                    "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident),
460                    "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident),
461                    "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident),
462                    "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident),
463                  ] :
464            self.doModify(q)
465       
Note: See TracBrowser for help on using the browser.