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

Revision 1520, 26.3 kB (checked in by jalet, 20 years ago)

Now stores the job's size in bytes in the database.
Preliminary work on payments storage : database schemas are OK now,
but no code to store payments yet.
Removed schema picture, not relevant anymore.

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