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