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

Revision 1711, 27.4 kB (checked in by jalet, 20 years ago)

Small fixes for incomplete entry intialization

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