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

Revision 1522, 26.6 kB (checked in by jalet, 20 years ago)

Payments history is now stored in database

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