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.28 2004/01/08 14:10:33 jalet |
---|
25 | # Copyright year changed. |
---|
26 | # |
---|
27 | # Revision 1.27 2004/01/06 14:24:59 jalet |
---|
28 | # Printer groups should be cached now, if caching is enabled. |
---|
29 | # |
---|
30 | # Revision 1.26 2003/12/29 14:12:48 uid67467 |
---|
31 | # Tries to workaround possible integrity violations when retrieving printer groups |
---|
32 | # |
---|
33 | # Revision 1.25 2003/12/27 16:49:25 uid67467 |
---|
34 | # Should be ok now. |
---|
35 | # |
---|
36 | # Revision 1.24 2003/11/29 22:02:14 jalet |
---|
37 | # Don't try to retrieve the user print quota information if current printer |
---|
38 | # doesn't exist. |
---|
39 | # |
---|
40 | # Revision 1.23 2003/11/23 19:01:37 jalet |
---|
41 | # Job price added to history |
---|
42 | # |
---|
43 | # Revision 1.22 2003/11/21 14:28:46 jalet |
---|
44 | # More complete job history. |
---|
45 | # |
---|
46 | # Revision 1.21 2003/11/12 13:06:38 jalet |
---|
47 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
48 | # |
---|
49 | # Revision 1.20 2003/10/09 21:25:26 jalet |
---|
50 | # Multiple printer names or wildcards can be passed on the command line |
---|
51 | # separated with commas. |
---|
52 | # Beta phase. |
---|
53 | # |
---|
54 | # Revision 1.19 2003/10/08 07:01:20 jalet |
---|
55 | # Job history can be disabled. |
---|
56 | # Some typos in README. |
---|
57 | # More messages in setup script. |
---|
58 | # |
---|
59 | # Revision 1.18 2003/10/07 09:07:30 jalet |
---|
60 | # Character encoding added to please latest version of Python |
---|
61 | # |
---|
62 | # Revision 1.17 2003/10/06 13:12:28 jalet |
---|
63 | # More work on caching |
---|
64 | # |
---|
65 | # Revision 1.16 2003/10/03 18:01:49 jalet |
---|
66 | # Nothing interesting... |
---|
67 | # |
---|
68 | # Revision 1.15 2003/10/03 12:27:03 jalet |
---|
69 | # Several optimizations, especially with LDAP backend |
---|
70 | # |
---|
71 | # Revision 1.14 2003/10/03 08:57:55 jalet |
---|
72 | # Caching mechanism now caches all that's cacheable. |
---|
73 | # |
---|
74 | # Revision 1.13 2003/10/02 20:23:18 jalet |
---|
75 | # Storage caching mechanism added. |
---|
76 | # |
---|
77 | # Revision 1.12 2003/08/17 14:20:25 jalet |
---|
78 | # Bug fix by Oleg Biteryakov |
---|
79 | # |
---|
80 | # Revision 1.11 2003/07/29 20:55:17 jalet |
---|
81 | # 1.14 is out ! |
---|
82 | # |
---|
83 | # Revision 1.10 2003/07/16 21:53:08 jalet |
---|
84 | # Really big modifications wrt new configuration file's location and content. |
---|
85 | # |
---|
86 | # Revision 1.9 2003/07/14 17:20:15 jalet |
---|
87 | # Bug in postgresql storage when modifying the prices for a printer |
---|
88 | # |
---|
89 | # Revision 1.8 2003/07/14 14:18:17 jalet |
---|
90 | # Wrong documentation strings |
---|
91 | # |
---|
92 | # Revision 1.7 2003/07/09 20:17:07 jalet |
---|
93 | # Email field added to PostgreSQL schema |
---|
94 | # |
---|
95 | # Revision 1.6 2003/07/07 11:49:24 jalet |
---|
96 | # Lots of small fixes with the help of PyChecker |
---|
97 | # |
---|
98 | # Revision 1.5 2003/07/07 08:33:19 jalet |
---|
99 | # Bug fix due to a typo in LDAP code |
---|
100 | # |
---|
101 | # Revision 1.4 2003/06/30 13:54:21 jalet |
---|
102 | # Sorts by user / group name |
---|
103 | # |
---|
104 | # Revision 1.3 2003/06/25 14:10:01 jalet |
---|
105 | # Hey, it may work (edpykota --reset excepted) ! |
---|
106 | # |
---|
107 | # Revision 1.2 2003/06/12 21:09:57 jalet |
---|
108 | # wrongly placed code. |
---|
109 | # |
---|
110 | # Revision 1.1 2003/06/10 16:37:54 jalet |
---|
111 | # Deletion of the second user which is not needed anymore. |
---|
112 | # Added a debug configuration field in /etc/pykota.conf |
---|
113 | # All queries can now be sent to the logger in debug mode, this will |
---|
114 | # greatly help improve performance when time for this will come. |
---|
115 | # |
---|
116 | # |
---|
117 | # |
---|
118 | # |
---|
119 | |
---|
120 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
121 | |
---|
122 | try : |
---|
123 | import pg |
---|
124 | except ImportError : |
---|
125 | import sys |
---|
126 | # TODO : to translate or not to translate ? |
---|
127 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
128 | |
---|
129 | class Storage(BaseStorage) : |
---|
130 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
131 | """Opens the PostgreSQL database connection.""" |
---|
132 | BaseStorage.__init__(self, pykotatool) |
---|
133 | try : |
---|
134 | (host, port) = host.split(":") |
---|
135 | port = int(port) |
---|
136 | except ValueError : |
---|
137 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
138 | |
---|
139 | try : |
---|
140 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
141 | except pg.error, msg : |
---|
142 | raise PyKotaStorageError, msg |
---|
143 | else : |
---|
144 | self.closed = 0 |
---|
145 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
146 | |
---|
147 | def close(self) : |
---|
148 | """Closes the database connection.""" |
---|
149 | if not self.closed : |
---|
150 | self.database.close() |
---|
151 | self.closed = 1 |
---|
152 | self.tool.logdebug("Database closed.") |
---|
153 | |
---|
154 | def beginTransaction(self) : |
---|
155 | """Starts a transaction.""" |
---|
156 | self.database.query("BEGIN;") |
---|
157 | self.tool.logdebug("Transaction begins...") |
---|
158 | |
---|
159 | def commitTransaction(self) : |
---|
160 | """Commits a transaction.""" |
---|
161 | self.database.query("COMMIT;") |
---|
162 | self.tool.logdebug("Transaction committed.") |
---|
163 | |
---|
164 | def rollbackTransaction(self) : |
---|
165 | """Rollbacks a transaction.""" |
---|
166 | self.database.query("ROLLBACK;") |
---|
167 | self.tool.logdebug("Transaction aborted.") |
---|
168 | |
---|
169 | def doSearch(self, query) : |
---|
170 | """Does a search query.""" |
---|
171 | query = query.strip() |
---|
172 | if not query.endswith(';') : |
---|
173 | query += ';' |
---|
174 | try : |
---|
175 | self.tool.logdebug("QUERY : %s" % query) |
---|
176 | result = self.database.query(query) |
---|
177 | except pg.error, msg : |
---|
178 | raise PyKotaStorageError, msg |
---|
179 | else : |
---|
180 | if (result is not None) and (result.ntuples() > 0) : |
---|
181 | return result.dictresult() |
---|
182 | |
---|
183 | def doModify(self, query) : |
---|
184 | """Does a (possibly multiple) modify query.""" |
---|
185 | query = query.strip() |
---|
186 | if not query.endswith(';') : |
---|
187 | query += ';' |
---|
188 | try : |
---|
189 | self.tool.logdebug("QUERY : %s" % query) |
---|
190 | result = self.database.query(query) |
---|
191 | except pg.error, msg : |
---|
192 | raise PyKotaStorageError, msg |
---|
193 | else : |
---|
194 | return result |
---|
195 | |
---|
196 | def doQuote(self, field) : |
---|
197 | """Quotes a field for use as a string in SQL queries.""" |
---|
198 | if type(field) == type(0.0) : |
---|
199 | typ = "decimal" |
---|
200 | elif type(field) == type(0) : |
---|
201 | typ = "int" |
---|
202 | else : |
---|
203 | typ = "text" |
---|
204 | return pg._quote(field, typ) |
---|
205 | |
---|
206 | def getAllUsersNames(self) : |
---|
207 | """Extracts all user names.""" |
---|
208 | usernames = [] |
---|
209 | result = self.doSearch("SELECT username FROM users;") |
---|
210 | if result : |
---|
211 | usernames = [record["username"] for record in result] |
---|
212 | return usernames |
---|
213 | |
---|
214 | def getAllGroupsNames(self) : |
---|
215 | """Extracts all group names.""" |
---|
216 | groupnames = [] |
---|
217 | result = self.doSearch("SELECT groupname FROM groups;") |
---|
218 | if result : |
---|
219 | groupnames = [record["groupname"] for record in result] |
---|
220 | return groupnames |
---|
221 | |
---|
222 | def getUserFromBackend(self, username) : |
---|
223 | """Extracts user information given its name.""" |
---|
224 | user = StorageUser(self, username) |
---|
225 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) |
---|
226 | if result : |
---|
227 | fields = result[0] |
---|
228 | user.ident = fields.get("id") |
---|
229 | user.LimitBy = fields.get("limitby") |
---|
230 | user.AccountBalance = fields.get("balance") |
---|
231 | user.LifeTimePaid = fields.get("lifetimepaid") |
---|
232 | user.Email = fields.get("email") |
---|
233 | user.Exists = 1 |
---|
234 | return user |
---|
235 | |
---|
236 | def getGroupFromBackend(self, groupname) : |
---|
237 | """Extracts group information given its name.""" |
---|
238 | group = StorageGroup(self, groupname) |
---|
239 | result = self.doSearch("SELECT * FROM groups WHERE groupname=%s LIMIT 1" % self.doQuote(groupname)) |
---|
240 | if result : |
---|
241 | fields = result[0] |
---|
242 | group.ident = fields.get("id") |
---|
243 | group.LimitBy = fields.get("limitby") |
---|
244 | 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)) |
---|
245 | if result : |
---|
246 | fields = result[0] |
---|
247 | group.AccountBalance = fields.get("balance") |
---|
248 | group.LifeTimePaid = fields.get("lifetimepaid") |
---|
249 | group.Exists = 1 |
---|
250 | return group |
---|
251 | |
---|
252 | def getPrinterFromBackend(self, printername) : |
---|
253 | """Extracts printer information given its name.""" |
---|
254 | printer = StoragePrinter(self, printername) |
---|
255 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) |
---|
256 | if result : |
---|
257 | fields = result[0] |
---|
258 | printer.ident = fields.get("id") |
---|
259 | printer.PricePerJob = fields.get("priceperjob") |
---|
260 | printer.PricePerPage = fields.get("priceperpage") |
---|
261 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
262 | printer.Exists = 1 |
---|
263 | return printer |
---|
264 | |
---|
265 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
266 | """Extracts a user print quota.""" |
---|
267 | userpquota = StorageUserPQuota(self, user, printer) |
---|
268 | if printer.Exists and user.Exists : |
---|
269 | 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))) |
---|
270 | if result : |
---|
271 | fields = result[0] |
---|
272 | userpquota.ident = fields.get("id") |
---|
273 | userpquota.PageCounter = fields.get("pagecounter") |
---|
274 | userpquota.LifePageCounter = fields.get("lifepagecounter") |
---|
275 | userpquota.SoftLimit = fields.get("softlimit") |
---|
276 | userpquota.HardLimit = fields.get("hardlimit") |
---|
277 | userpquota.DateLimit = fields.get("datelimit") |
---|
278 | userpquota.Exists = 1 |
---|
279 | return userpquota |
---|
280 | |
---|
281 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
282 | """Extracts a group print quota.""" |
---|
283 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
284 | if group.Exists : |
---|
285 | result = self.doSearch("SELECT id, softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
286 | if result : |
---|
287 | fields = result[0] |
---|
288 | grouppquota.ident = fields.get("id") |
---|
289 | grouppquota.SoftLimit = fields.get("softlimit") |
---|
290 | grouppquota.HardLimit = fields.get("hardlimit") |
---|
291 | grouppquota.DateLimit = fields.get("datelimit") |
---|
292 | 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))) |
---|
293 | if result : |
---|
294 | fields = result[0] |
---|
295 | grouppquota.PageCounter = fields.get("pagecounter") |
---|
296 | grouppquota.LifePageCounter = fields.get("lifepagecounter") |
---|
297 | grouppquota.Exists = 1 |
---|
298 | return grouppquota |
---|
299 | |
---|
300 | def getPrinterLastJobFromBackend(self, printer) : |
---|
301 | """Extracts a printer's last job information.""" |
---|
302 | lastjob = StorageLastJob(self, printer) |
---|
303 | result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, jobdate FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident)) |
---|
304 | if result : |
---|
305 | fields = result[0] |
---|
306 | lastjob.ident = fields.get("id") |
---|
307 | lastjob.JobId = fields.get("jobid") |
---|
308 | lastjob.User = self.getUser(fields.get("username")) |
---|
309 | lastjob.PrinterPageCounter = fields.get("pagecounter") |
---|
310 | lastjob.JobSize = fields.get("jobsize") |
---|
311 | lastjob.JobPrice = fields.get("jobprice") |
---|
312 | lastjob.JobAction = fields.get("action") |
---|
313 | lastjob.JobFileName = fields.get("filename") |
---|
314 | lastjob.JobTitle = fields.get("title") |
---|
315 | lastjob.JobCopies = fields.get("copies") |
---|
316 | lastjob.JobOptions = fields.get("options") |
---|
317 | lastjob.JobDate = fields.get("jobdate") |
---|
318 | lastjob.Exists = 1 |
---|
319 | return lastjob |
---|
320 | |
---|
321 | def getGroupMembersFromBackend(self, group) : |
---|
322 | """Returns the group's members list.""" |
---|
323 | groupmembers = [] |
---|
324 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
325 | if result : |
---|
326 | for record in result : |
---|
327 | user = StorageUser(self, record.get("username")) |
---|
328 | user.ident = record.get("userid") |
---|
329 | user.LimitBy = record.get("limitby") |
---|
330 | user.AccountBalance = record.get("balance") |
---|
331 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
332 | user.Email = record.get("email") |
---|
333 | user.Exists = 1 |
---|
334 | groupmembers.append(user) |
---|
335 | self.cacheEntry("USERS", user.Name, user) |
---|
336 | return groupmembers |
---|
337 | |
---|
338 | def getUserGroupsFromBackend(self, user) : |
---|
339 | """Returns the user's groups list.""" |
---|
340 | groups = [] |
---|
341 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
342 | if result : |
---|
343 | for record in result : |
---|
344 | groups.append(self.getGroup(record.get("groupname"))) |
---|
345 | return groups |
---|
346 | |
---|
347 | def getParentPrintersFromBackend(self, printer) : |
---|
348 | """Get all the printer groups this printer is a member of.""" |
---|
349 | pgroups = [] |
---|
350 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s;" % self.doQuote(printer.ident)) |
---|
351 | if result : |
---|
352 | for record in result : |
---|
353 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
354 | parentprinter = self.getPrinter(record.get("printername")) |
---|
355 | if parentprinter.Exists : |
---|
356 | pgroups.append(parentprinter) |
---|
357 | return pgroups |
---|
358 | |
---|
359 | def getMatchingPrinters(self, printerpattern) : |
---|
360 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
361 | printers = [] |
---|
362 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
363 | # but we don't because other storages semantics may be different, so every |
---|
364 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
365 | result = self.doSearch("SELECT * FROM printers") |
---|
366 | if result : |
---|
367 | for record in result : |
---|
368 | if self.tool.matchString(record["printername"], printerpattern.split(",")) : |
---|
369 | printer = StoragePrinter(self, record["printername"]) |
---|
370 | printer.ident = record.get("id") |
---|
371 | printer.PricePerJob = record.get("priceperjob") |
---|
372 | printer.PricePerPage = record.get("priceperpage") |
---|
373 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
374 | printer.Exists = 1 |
---|
375 | printers.append(printer) |
---|
376 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
377 | return printers |
---|
378 | |
---|
379 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
380 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
381 | usersandquotas = [] |
---|
382 | 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)) |
---|
383 | if result : |
---|
384 | for record in result : |
---|
385 | if self.tool.matchString(record.get("username"), names) : |
---|
386 | user = StorageUser(self, record.get("username")) |
---|
387 | user.ident = record.get("uid") |
---|
388 | user.LimitBy = record.get("limitby") |
---|
389 | user.AccountBalance = record.get("balance") |
---|
390 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
391 | user.Email = record.get("email") |
---|
392 | user.Exists = 1 |
---|
393 | userpquota = StorageUserPQuota(self, user, printer) |
---|
394 | userpquota.ident = record.get("id") |
---|
395 | userpquota.PageCounter = record.get("pagecounter") |
---|
396 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
397 | userpquota.SoftLimit = record.get("softlimit") |
---|
398 | userpquota.HardLimit = record.get("hardlimit") |
---|
399 | userpquota.DateLimit = record.get("datelimit") |
---|
400 | userpquota.Exists = 1 |
---|
401 | usersandquotas.append((user, userpquota)) |
---|
402 | self.cacheEntry("USERS", user.Name, user) |
---|
403 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
404 | return usersandquotas |
---|
405 | |
---|
406 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
407 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
408 | groupsandquotas = [] |
---|
409 | 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)) |
---|
410 | if result : |
---|
411 | for record in result : |
---|
412 | if self.tool.matchString(record.get("groupname"), names) : |
---|
413 | group = self.getGroup(record.get("groupname")) |
---|
414 | grouppquota = self.getGroupPQuota(group, printer) |
---|
415 | groupsandquotas.append((group, grouppquota)) |
---|
416 | return groupsandquotas |
---|
417 | |
---|
418 | def addPrinter(self, printername) : |
---|
419 | """Adds a printer to the quota storage, returns it.""" |
---|
420 | self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
421 | return self.getPrinter(printername) |
---|
422 | |
---|
423 | def addUser(self, user) : |
---|
424 | """Adds a user to the quota storage, returns its id.""" |
---|
425 | 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))) |
---|
426 | return self.getUser(user.Name) |
---|
427 | |
---|
428 | def addGroup(self, group) : |
---|
429 | """Adds a group to the quota storage, returns its id.""" |
---|
430 | self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy))) |
---|
431 | return self.getGroup(group.Name) |
---|
432 | |
---|
433 | def addUserToGroup(self, user, group) : |
---|
434 | """Adds an user to a group.""" |
---|
435 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
436 | try : |
---|
437 | mexists = int(result[0].get("mexists")) |
---|
438 | except (IndexError, TypeError) : |
---|
439 | mexists = 0 |
---|
440 | if not mexists : |
---|
441 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
442 | |
---|
443 | def addUserPQuota(self, user, printer) : |
---|
444 | """Initializes a user print quota on a printer.""" |
---|
445 | self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
446 | return self.getUserPQuota(user, printer) |
---|
447 | |
---|
448 | def addGroupPQuota(self, group, printer) : |
---|
449 | """Initializes a group print quota on a printer.""" |
---|
450 | self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
451 | return self.getGroupPQuota(group, printer) |
---|
452 | |
---|
453 | def writePrinterPrices(self, printer) : |
---|
454 | """Write the printer's prices back into the storage.""" |
---|
455 | self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident))) |
---|
456 | |
---|
457 | def writeUserLimitBy(self, user, limitby) : |
---|
458 | """Sets the user's limiting factor.""" |
---|
459 | self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident))) |
---|
460 | |
---|
461 | def writeGroupLimitBy(self, group, limitby) : |
---|
462 | """Sets the group's limiting factor.""" |
---|
463 | self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident))) |
---|
464 | |
---|
465 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
466 | """Sets the date limit permanently for a user print quota.""" |
---|
467 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
468 | |
---|
469 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
470 | """Sets the date limit permanently for a group print quota.""" |
---|
471 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
472 | |
---|
473 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
474 | """Sets the new page counters permanently for a user print quota.""" |
---|
475 | self.doModify("UPDATE userpquota SET pagecounter=%s,lifepagecounter=%s WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident))) |
---|
476 | |
---|
477 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
478 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
479 | if newlifetimepaid is not None : |
---|
480 | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
---|
481 | else : |
---|
482 | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
---|
483 | |
---|
484 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
485 | """Sets the last job's size permanently.""" |
---|
486 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
487 | |
---|
488 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) : |
---|
489 | """Adds a job in a printer's history.""" |
---|
490 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
491 | if jobsize is not None : |
---|
492 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options) 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(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options))) |
---|
493 | else : |
---|
494 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options) VALUES (%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))) |
---|
495 | else : |
---|
496 | # here we explicitly want to reset jobsize to NULL if needed |
---|
497 | 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, 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(printer.LastJob.ident))) |
---|
498 | |
---|
499 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
500 | """Sets soft and hard limits for a user quota.""" |
---|
501 | self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident))) |
---|
502 | |
---|
503 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
504 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
505 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident))) |
---|
506 | |
---|
507 | def deleteUser(self, user) : |
---|
508 | """Completely deletes an user from the Quota Storage.""" |
---|
509 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
510 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
511 | # TODO : incorrectly charged (overcharged). |
---|
512 | for q in [ |
---|
513 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
514 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
515 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
516 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
517 | ] : |
---|
518 | self.doModify(q) |
---|
519 | |
---|
520 | def deleteGroup(self, group) : |
---|
521 | """Completely deletes a group from the Quota Storage.""" |
---|
522 | for q in [ |
---|
523 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
524 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
525 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
526 | ] : |
---|
527 | self.doModify(q) |
---|
528 | |
---|