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.67 2005/02/16 00:29:33 jalet |
---|
25 | # Fixed the maxdenybanners directive. |
---|
26 | # Introduced the denyduplicates directive. |
---|
27 | # Fixed some database related glitches. |
---|
28 | # |
---|
29 | # Revision 1.66 2005/02/13 22:48:38 jalet |
---|
30 | # Added the md5sum to the history |
---|
31 | # |
---|
32 | # Revision 1.65 2005/02/13 22:02:29 jalet |
---|
33 | # Big database structure changes. Upgrade script is now included as well as |
---|
34 | # the new LDAP schema. |
---|
35 | # Introduction of the -o | --overcharge command line option to edpykota. |
---|
36 | # The output of repykota is more complete, but doesn't fit in 80 columns anymore. |
---|
37 | # Introduction of the new 'maxdenybanners' directive. |
---|
38 | # |
---|
39 | # Revision 1.64 2005/01/18 19:47:50 jalet |
---|
40 | # Big bug fix wrt the datelimit attribute |
---|
41 | # |
---|
42 | # Revision 1.63 2005/01/01 08:16:17 jalet |
---|
43 | # Fixed a problem which occured when 'limitby' was unset in the PostgreSQL |
---|
44 | # database |
---|
45 | # |
---|
46 | # Revision 1.62 2004/12/21 16:19:44 jalet |
---|
47 | # Fixed the problem with dumpykota's filtering of printers groups membership |
---|
48 | # |
---|
49 | # Revision 1.61 2004/12/21 15:49:59 jalet |
---|
50 | # The dumpykota command now supports extended filtering capabilities with |
---|
51 | # the PostgreSQL backend. LDAP doesn't yet support such possibilities. |
---|
52 | # |
---|
53 | # Revision 1.60 2004/12/21 14:45:31 jalet |
---|
54 | # Prepared dumpykota to accept the new --filter command line option. Some |
---|
55 | # additionnal work needs to be done in the backends though. |
---|
56 | # |
---|
57 | # Revision 1.59 2004/10/25 14:12:25 jalet |
---|
58 | # For URGENT legal reasons (Italy), a new "privacy" directive was added to pykota.conf |
---|
59 | # to hide print jobs' title, filename, and options. |
---|
60 | # |
---|
61 | # Revision 1.58 2004/10/12 08:58:53 jalet |
---|
62 | # Now warnpykota only warns users who have already printed, to not confuse |
---|
63 | # users who have just opened their account. |
---|
64 | # |
---|
65 | # Revision 1.57 2004/10/10 10:12:21 jalet |
---|
66 | # Improved SQL queries for groups. Same work has to be done for groups print quotas. |
---|
67 | # |
---|
68 | # Revision 1.56 2004/10/07 21:14:28 jalet |
---|
69 | # Hopefully final fix for data encoding to and from the database |
---|
70 | # |
---|
71 | # Revision 1.55 2004/10/07 09:37:53 jalet |
---|
72 | # Fixes recently introduced bug wrt users groups (was it three days ago ?) |
---|
73 | # |
---|
74 | # Revision 1.54 2004/10/05 10:05:04 jalet |
---|
75 | # UnicodeEncodeError isn't defined in Python2.1 |
---|
76 | # |
---|
77 | # Revision 1.53 2004/10/05 09:59:20 jalet |
---|
78 | # Restore compatibility with Python 2.1 |
---|
79 | # |
---|
80 | # Revision 1.52 2004/10/04 22:23:54 jalet |
---|
81 | # Charset conversions for dumps from the PostgreSQL backend |
---|
82 | # |
---|
83 | # Revision 1.51 2004/10/04 16:11:38 jalet |
---|
84 | # Now outputs page counters when dumping user groups quotas |
---|
85 | # |
---|
86 | # Revision 1.50 2004/10/04 16:01:15 jalet |
---|
87 | # More complete dumps for groups and groups quotas |
---|
88 | # |
---|
89 | # Revision 1.49 2004/10/02 13:33:13 jalet |
---|
90 | # Some work done of user's charset handling in database dumps. |
---|
91 | # |
---|
92 | # Revision 1.48 2004/10/02 05:48:56 jalet |
---|
93 | # Should now correctly deal with charsets both when storing into databases and when |
---|
94 | # retrieving datas. Works with both PostgreSQL and LDAP. |
---|
95 | # |
---|
96 | # Revision 1.47 2004/09/15 07:26:20 jalet |
---|
97 | # Data dumps are now ordered by entry creation date if applicable. |
---|
98 | # Now dumpykota exits with a message when there's a broken pipe like |
---|
99 | # in dumpykota --data history | head -3 |
---|
100 | # |
---|
101 | # Revision 1.46 2004/09/15 06:58:25 jalet |
---|
102 | # User groups membership and printer groups membership can now be dumped too |
---|
103 | # |
---|
104 | # Revision 1.45 2004/09/14 22:29:13 jalet |
---|
105 | # First version of dumpykota. Works fine but only with PostgreSQL backend |
---|
106 | # for now. |
---|
107 | # |
---|
108 | # Revision 1.44 2004/09/10 21:32:54 jalet |
---|
109 | # Small fixes for incomplete entry intialization |
---|
110 | # |
---|
111 | # Revision 1.43 2004/07/01 17:45:49 jalet |
---|
112 | # Added code to handle the description field for printers |
---|
113 | # |
---|
114 | # Revision 1.42 2004/06/08 17:44:43 jalet |
---|
115 | # Payment now gets deleted when the user is deleted |
---|
116 | # |
---|
117 | # Revision 1.41 2004/06/05 22:03:50 jalet |
---|
118 | # Payments history is now stored in database |
---|
119 | # |
---|
120 | # Revision 1.40 2004/06/03 23:14:11 jalet |
---|
121 | # Now stores the job's size in bytes in the database. |
---|
122 | # Preliminary work on payments storage : database schemas are OK now, |
---|
123 | # but no code to store payments yet. |
---|
124 | # Removed schema picture, not relevant anymore. |
---|
125 | # |
---|
126 | # Revision 1.39 2004/05/26 14:50:12 jalet |
---|
127 | # First try at saving the job-originating-hostname in the database |
---|
128 | # |
---|
129 | # Revision 1.38 2004/05/06 12:37:47 jalet |
---|
130 | # pkpgcounter : comments |
---|
131 | # pkprinters : when --add is used, existing printers are now skipped. |
---|
132 | # |
---|
133 | # Revision 1.37 2004/02/23 22:53:21 jalet |
---|
134 | # Don't retrieve data when it's not needed, to avoid database queries |
---|
135 | # |
---|
136 | # Revision 1.36 2004/02/04 13:24:41 jalet |
---|
137 | # pkprinters can now remove printers from printers groups. |
---|
138 | # |
---|
139 | # Revision 1.35 2004/02/04 11:17:00 jalet |
---|
140 | # pkprinters command line tool added. |
---|
141 | # |
---|
142 | # Revision 1.34 2004/02/02 22:44:16 jalet |
---|
143 | # Preliminary work on Relationnal Database Independance via DB-API 2.0 |
---|
144 | # |
---|
145 | # |
---|
146 | # |
---|
147 | |
---|
148 | from types import StringType |
---|
149 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
150 | |
---|
151 | class SQLStorage : |
---|
152 | def prepareRawResult(self, result) : |
---|
153 | """Prepares a raw result by including the headers.""" |
---|
154 | if result.ntuples() > 0 : |
---|
155 | entries = [result.listfields()] |
---|
156 | entries.extend(result.getresult()) |
---|
157 | nbfields = len(entries[0]) |
---|
158 | for i in range(1, len(entries)) : |
---|
159 | fields = list(entries[i]) |
---|
160 | for j in range(nbfields) : |
---|
161 | field = fields[j] |
---|
162 | if type(field) == StringType : |
---|
163 | fields[j] = self.databaseToUserCharset(field) |
---|
164 | entries[i] = tuple(fields) |
---|
165 | return entries |
---|
166 | |
---|
167 | def createFilter(self, only) : |
---|
168 | """Returns the appropriate SQL filter.""" |
---|
169 | if only : |
---|
170 | expressions = [] |
---|
171 | for (k, v) in only.items() : |
---|
172 | expressions.append("%s=%s" % (k, self.doQuote(v))) |
---|
173 | return " AND ".join(expressions) |
---|
174 | return "" |
---|
175 | |
---|
176 | def extractPrinters(self, extractonly={}) : |
---|
177 | """Extracts all printer records.""" |
---|
178 | thefilter = self.createFilter(extractonly) |
---|
179 | if thefilter : |
---|
180 | thefilter = "WHERE %s" % thefilter |
---|
181 | result = self.doRawSearch("SELECT * FROM printers %s ORDER BY id ASC" % thefilter) |
---|
182 | return self.prepareRawResult(result) |
---|
183 | |
---|
184 | def extractUsers(self, extractonly={}) : |
---|
185 | """Extracts all user records.""" |
---|
186 | thefilter = self.createFilter(extractonly) |
---|
187 | if thefilter : |
---|
188 | thefilter = "WHERE %s" % thefilter |
---|
189 | result = self.doRawSearch("SELECT * FROM users %s ORDER BY id ASC" % thefilter) |
---|
190 | return self.prepareRawResult(result) |
---|
191 | |
---|
192 | def extractGroups(self, extractonly={}) : |
---|
193 | """Extracts all group records.""" |
---|
194 | thefilter = self.createFilter(extractonly) |
---|
195 | if thefilter : |
---|
196 | thefilter = "WHERE %s" % thefilter |
---|
197 | result = self.doRawSearch("SELECT groups.*,COALESCE(SUM(balance), 0) AS balance, COALESCE(SUM(lifetimepaid), 0) as lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) %s GROUP BY groups.id,groups.groupname,groups.limitby ORDER BY groups.id ASC" % thefilter) |
---|
198 | return self.prepareRawResult(result) |
---|
199 | |
---|
200 | def extractPayments(self, extractonly={}) : |
---|
201 | """Extracts all payment records.""" |
---|
202 | thefilter = self.createFilter(extractonly) |
---|
203 | if thefilter : |
---|
204 | thefilter = "AND %s" % thefilter |
---|
205 | result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %s ORDER BY payments.id ASC" % thefilter) |
---|
206 | return self.prepareRawResult(result) |
---|
207 | |
---|
208 | def extractUpquotas(self, extractonly={}) : |
---|
209 | """Extracts all userpquota records.""" |
---|
210 | thefilter = self.createFilter(extractonly) |
---|
211 | if thefilter : |
---|
212 | thefilter = "AND %s" % thefilter |
---|
213 | result = self.doRawSearch("SELECT users.username,printers.printername,userpquota.* FROM users,printers,userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid %s ORDER BY userpquota.id ASC" % thefilter) |
---|
214 | return self.prepareRawResult(result) |
---|
215 | |
---|
216 | def extractGpquotas(self, extractonly={}) : |
---|
217 | """Extracts all grouppquota records.""" |
---|
218 | thefilter = self.createFilter(extractonly) |
---|
219 | if thefilter : |
---|
220 | thefilter = "AND %s" % thefilter |
---|
221 | result = self.doRawSearch("SELECT groups.groupname,printers.printername,grouppquota.*,coalesce(sum(pagecounter), 0) AS pagecounter,coalesce(sum(lifepagecounter), 0) AS lifepagecounter FROM groups,printers,grouppquota,userpquota WHERE groups.id=grouppquota.groupid AND printers.id=grouppquota.printerid AND userpquota.printerid=grouppquota.printerid AND userpquota.userid IN (SELECT userid FROM groupsmembers WHERE groupsmembers.groupid=grouppquota.groupid) %s GROUP BY grouppquota.id,grouppquota.groupid,grouppquota.printerid,grouppquota.softlimit,grouppquota.hardlimit,grouppquota.datelimit,groups.groupname,printers.printername ORDER BY grouppquota.id" % thefilter) |
---|
222 | return self.prepareRawResult(result) |
---|
223 | |
---|
224 | def extractUmembers(self, extractonly={}) : |
---|
225 | """Extracts all user groups members.""" |
---|
226 | thefilter = self.createFilter(extractonly) |
---|
227 | if thefilter : |
---|
228 | thefilter = "AND %s" % thefilter |
---|
229 | result = self.doRawSearch("SELECT groups.groupname, users.username, groupsmembers.* FROM groups,users,groupsmembers WHERE users.id=groupsmembers.userid AND groups.id=groupsmembers.groupid %s ORDER BY groupsmembers.groupid, groupsmembers.userid ASC" % thefilter) |
---|
230 | return self.prepareRawResult(result) |
---|
231 | |
---|
232 | def extractPmembers(self, extractonly={}) : |
---|
233 | """Extracts all printer groups members.""" |
---|
234 | for (k, v) in extractonly.items() : |
---|
235 | if k == "pgroupname" : |
---|
236 | del extractonly[k] |
---|
237 | extractonly["p1.printername"] = v |
---|
238 | elif k == "printername" : |
---|
239 | del extractonly[k] |
---|
240 | extractonly["p2.printername"] = v |
---|
241 | thefilter = self.createFilter(extractonly) |
---|
242 | if thefilter : |
---|
243 | thefilter = "AND %s" % thefilter |
---|
244 | result = self.doRawSearch("SELECT p1.printername as pgroupname, p2.printername as printername, printergroupsmembers.* FROM printers p1, printers p2, printergroupsmembers WHERE p1.id=printergroupsmembers.groupid AND p2.id=printergroupsmembers.printerid %s ORDER BY printergroupsmembers.groupid, printergroupsmembers.printerid ASC" % thefilter) |
---|
245 | return self.prepareRawResult(result) |
---|
246 | |
---|
247 | def extractHistory(self, extractonly={}) : |
---|
248 | """Extracts all jobhistory records.""" |
---|
249 | thefilter = self.createFilter(extractonly) |
---|
250 | if thefilter : |
---|
251 | thefilter = "AND %s" % thefilter |
---|
252 | result = self.doRawSearch("SELECT users.username,printers.printername,jobhistory.* FROM users,printers,jobhistory WHERE users.id=jobhistory.userid AND printers.id=jobhistory.printerid %s ORDER BY jobhistory.id ASC" % thefilter) |
---|
253 | return self.prepareRawResult(result) |
---|
254 | |
---|
255 | def getAllUsersNames(self) : |
---|
256 | """Extracts all user names.""" |
---|
257 | usernames = [] |
---|
258 | result = self.doSearch("SELECT username FROM users") |
---|
259 | if result : |
---|
260 | usernames = [record["username"] for record in result] |
---|
261 | return usernames |
---|
262 | |
---|
263 | def getAllGroupsNames(self) : |
---|
264 | """Extracts all group names.""" |
---|
265 | groupnames = [] |
---|
266 | result = self.doSearch("SELECT groupname FROM groups") |
---|
267 | if result : |
---|
268 | groupnames = [record["groupname"] for record in result] |
---|
269 | return groupnames |
---|
270 | |
---|
271 | def getUserNbJobsFromHistory(self, user) : |
---|
272 | """Returns the number of jobs the user has in history.""" |
---|
273 | result = self.doSearch("SELECT COUNT(*) FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident)) |
---|
274 | if result : |
---|
275 | return result[0]["count"] |
---|
276 | return 0 |
---|
277 | |
---|
278 | def getUserFromBackend(self, username) : |
---|
279 | """Extracts user information given its name.""" |
---|
280 | user = StorageUser(self, username) |
---|
281 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) |
---|
282 | if result : |
---|
283 | fields = result[0] |
---|
284 | user.ident = fields.get("id") |
---|
285 | user.Name = fields.get("username", username) |
---|
286 | user.LimitBy = fields.get("limitby") or "quota" |
---|
287 | user.AccountBalance = fields.get("balance") |
---|
288 | user.LifeTimePaid = fields.get("lifetimepaid") |
---|
289 | user.Email = fields.get("email") |
---|
290 | user.OverCharge = fields.get("overcharge", 1.0) |
---|
291 | user.Exists = 1 |
---|
292 | return user |
---|
293 | |
---|
294 | def getGroupFromBackend(self, groupname) : |
---|
295 | """Extracts group information given its name.""" |
---|
296 | group = StorageGroup(self, groupname) |
---|
297 | result = self.doSearch("SELECT groups.*,COALESCE(SUM(balance), 0.0) AS balance, COALESCE(SUM(lifetimepaid), 0.0) AS lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) WHERE groupname=%s GROUP BY groups.id,groups.groupname,groups.limitby LIMIT 1" % self.doQuote(groupname)) |
---|
298 | if result : |
---|
299 | fields = result[0] |
---|
300 | group.ident = fields.get("id") |
---|
301 | group.Name = fields.get("groupname", groupname) |
---|
302 | group.LimitBy = fields.get("limitby") or "quota" |
---|
303 | group.AccountBalance = fields.get("balance") |
---|
304 | group.LifeTimePaid = fields.get("lifetimepaid") |
---|
305 | group.Exists = 1 |
---|
306 | return group |
---|
307 | |
---|
308 | def getPrinterFromBackend(self, printername) : |
---|
309 | """Extracts printer information given its name.""" |
---|
310 | printer = StoragePrinter(self, printername) |
---|
311 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) |
---|
312 | if result : |
---|
313 | fields = result[0] |
---|
314 | printer.ident = fields.get("id") |
---|
315 | printer.Name = fields.get("printername", printername) |
---|
316 | printer.PricePerJob = fields.get("priceperjob") or 0.0 |
---|
317 | printer.PricePerPage = fields.get("priceperpage") or 0.0 |
---|
318 | printer.Description = self.databaseToUserCharset(fields.get("description") or "") |
---|
319 | printer.Exists = 1 |
---|
320 | return printer |
---|
321 | |
---|
322 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
323 | """Extracts a user print quota.""" |
---|
324 | userpquota = StorageUserPQuota(self, user, printer) |
---|
325 | if printer.Exists and user.Exists : |
---|
326 | result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
327 | if result : |
---|
328 | fields = result[0] |
---|
329 | userpquota.ident = fields.get("id") |
---|
330 | userpquota.PageCounter = fields.get("pagecounter") |
---|
331 | userpquota.LifePageCounter = fields.get("lifepagecounter") |
---|
332 | userpquota.SoftLimit = fields.get("softlimit") |
---|
333 | userpquota.HardLimit = fields.get("hardlimit") |
---|
334 | userpquota.DateLimit = fields.get("datelimit") |
---|
335 | userpquota.WarnCount = fields.get("warncount") |
---|
336 | userpquota.Exists = 1 |
---|
337 | return userpquota |
---|
338 | |
---|
339 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
340 | """Extracts a group print quota.""" |
---|
341 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
342 | if group.Exists : |
---|
343 | result = self.doSearch("SELECT * FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
344 | if result : |
---|
345 | fields = result[0] |
---|
346 | grouppquota.ident = fields.get("id") |
---|
347 | grouppquota.SoftLimit = fields.get("softlimit") |
---|
348 | grouppquota.HardLimit = fields.get("hardlimit") |
---|
349 | grouppquota.DateLimit = fields.get("datelimit") |
---|
350 | 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))) |
---|
351 | if result : |
---|
352 | fields = result[0] |
---|
353 | grouppquota.PageCounter = fields.get("pagecounter") or 0 |
---|
354 | grouppquota.LifePageCounter = fields.get("lifepagecounter") or 0 |
---|
355 | grouppquota.Exists = 1 |
---|
356 | return grouppquota |
---|
357 | |
---|
358 | def getPrinterLastJobFromBackend(self, printer) : |
---|
359 | """Extracts a printer's last job information.""" |
---|
360 | lastjob = StorageLastJob(self, printer) |
---|
361 | result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate, md5sum, pages, billingcode FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident)) |
---|
362 | if result : |
---|
363 | fields = result[0] |
---|
364 | lastjob.ident = fields.get("id") |
---|
365 | lastjob.JobId = fields.get("jobid") |
---|
366 | lastjob.UserName = fields.get("username") |
---|
367 | lastjob.PrinterPageCounter = fields.get("pagecounter") |
---|
368 | lastjob.JobSize = fields.get("jobsize") |
---|
369 | lastjob.JobPrice = fields.get("jobprice") |
---|
370 | lastjob.JobAction = fields.get("action") |
---|
371 | lastjob.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") |
---|
372 | lastjob.JobTitle = self.databaseToUserCharset(fields.get("title") or "") |
---|
373 | lastjob.JobCopies = fields.get("copies") |
---|
374 | lastjob.JobOptions = self.databaseToUserCharset(fields.get("options") or "") |
---|
375 | lastjob.JobDate = fields.get("jobdate") |
---|
376 | lastjob.JobHostName = fields.get("hostname") |
---|
377 | lastjob.JobSizeBytes = fields.get("jobsizebytes") |
---|
378 | lastjob.JobMD5Sum = fields.get("md5sum") |
---|
379 | lastjob.JobPages = fields.get("pages") |
---|
380 | lastjob.JobBillingCode = fields.get("billingcode") |
---|
381 | lastjob.Exists = 1 |
---|
382 | return lastjob |
---|
383 | |
---|
384 | def getGroupMembersFromBackend(self, group) : |
---|
385 | """Returns the group's members list.""" |
---|
386 | groupmembers = [] |
---|
387 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
388 | if result : |
---|
389 | for record in result : |
---|
390 | user = StorageUser(self, record.get("username")) |
---|
391 | user.ident = record.get("userid") |
---|
392 | user.LimitBy = record.get("limitby") or "quota" |
---|
393 | user.AccountBalance = record.get("balance") |
---|
394 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
395 | user.Email = record.get("email") |
---|
396 | user.OverCharge = record.get("overcharge") |
---|
397 | user.Exists = 1 |
---|
398 | groupmembers.append(user) |
---|
399 | self.cacheEntry("USERS", user.Name, user) |
---|
400 | return groupmembers |
---|
401 | |
---|
402 | def getUserGroupsFromBackend(self, user) : |
---|
403 | """Returns the user's groups list.""" |
---|
404 | groups = [] |
---|
405 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
406 | if result : |
---|
407 | for record in result : |
---|
408 | groups.append(self.getGroup(record.get("groupname"))) |
---|
409 | return groups |
---|
410 | |
---|
411 | def getParentPrintersFromBackend(self, printer) : |
---|
412 | """Get all the printer groups this printer is a member of.""" |
---|
413 | pgroups = [] |
---|
414 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident)) |
---|
415 | if result : |
---|
416 | for record in result : |
---|
417 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
418 | parentprinter = self.getPrinter(record.get("printername")) |
---|
419 | if parentprinter.Exists : |
---|
420 | pgroups.append(parentprinter) |
---|
421 | return pgroups |
---|
422 | |
---|
423 | def getMatchingPrinters(self, printerpattern) : |
---|
424 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
425 | printers = [] |
---|
426 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
427 | # but we don't because other storages semantics may be different, so every |
---|
428 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
429 | result = self.doSearch("SELECT * FROM printers") |
---|
430 | if result : |
---|
431 | for record in result : |
---|
432 | if self.tool.matchString(record["printername"], printerpattern.split(",")) : |
---|
433 | printer = StoragePrinter(self, record["printername"]) |
---|
434 | printer.ident = record.get("id") |
---|
435 | printer.PricePerJob = record.get("priceperjob") or 0.0 |
---|
436 | printer.PricePerPage = record.get("priceperpage") or 0.0 |
---|
437 | printer.Description = self.databaseToUserCharset(record.get("description") or "") |
---|
438 | printer.Exists = 1 |
---|
439 | printers.append(printer) |
---|
440 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
441 | return printers |
---|
442 | |
---|
443 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
444 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
445 | usersandquotas = [] |
---|
446 | result = self.doSearch("SELECT users.id as uid,username,balance,lifetimepaid,limitby,email,overcharge,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit,warncount FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident)) |
---|
447 | if result : |
---|
448 | for record in result : |
---|
449 | if self.tool.matchString(record.get("username"), names) : |
---|
450 | user = StorageUser(self, record.get("username")) |
---|
451 | user.ident = record.get("uid") |
---|
452 | user.LimitBy = record.get("limitby") or "quota" |
---|
453 | user.AccountBalance = record.get("balance") |
---|
454 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
455 | user.Email = record.get("email") |
---|
456 | user.OverCharge = record.get("overcharge") |
---|
457 | user.Exists = 1 |
---|
458 | userpquota = StorageUserPQuota(self, user, printer) |
---|
459 | userpquota.ident = record.get("id") |
---|
460 | userpquota.PageCounter = record.get("pagecounter") |
---|
461 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
462 | userpquota.SoftLimit = record.get("softlimit") |
---|
463 | userpquota.HardLimit = record.get("hardlimit") |
---|
464 | userpquota.DateLimit = record.get("datelimit") |
---|
465 | userpquota.WarnCount = record.get("warncount") |
---|
466 | userpquota.Exists = 1 |
---|
467 | usersandquotas.append((user, userpquota)) |
---|
468 | self.cacheEntry("USERS", user.Name, user) |
---|
469 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
470 | return usersandquotas |
---|
471 | |
---|
472 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
473 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
474 | groupsandquotas = [] |
---|
475 | 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)) |
---|
476 | if result : |
---|
477 | for record in result : |
---|
478 | if self.tool.matchString(record.get("groupname"), names) : |
---|
479 | group = self.getGroup(record.get("groupname")) |
---|
480 | grouppquota = self.getGroupPQuota(group, printer) |
---|
481 | groupsandquotas.append((group, grouppquota)) |
---|
482 | return groupsandquotas |
---|
483 | |
---|
484 | def addPrinter(self, printername) : |
---|
485 | """Adds a printer to the quota storage, returns it.""" |
---|
486 | self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
487 | return self.getPrinter(printername) |
---|
488 | |
---|
489 | def addUser(self, user) : |
---|
490 | """Adds a user to the quota storage, returns its id.""" |
---|
491 | self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge) VALUES (%s, %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), self.doQuote(user.OverCharge))) |
---|
492 | return self.getUser(user.Name) |
---|
493 | |
---|
494 | def addGroup(self, group) : |
---|
495 | """Adds a group to the quota storage, returns its id.""" |
---|
496 | self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy or "quota"))) |
---|
497 | return self.getGroup(group.Name) |
---|
498 | |
---|
499 | def addUserToGroup(self, user, group) : |
---|
500 | """Adds an user to a group.""" |
---|
501 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
502 | try : |
---|
503 | mexists = int(result[0].get("mexists")) |
---|
504 | except (IndexError, TypeError) : |
---|
505 | mexists = 0 |
---|
506 | if not mexists : |
---|
507 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
508 | |
---|
509 | def addUserPQuota(self, user, printer) : |
---|
510 | """Initializes a user print quota on a printer.""" |
---|
511 | self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
512 | return self.getUserPQuota(user, printer) |
---|
513 | |
---|
514 | def addGroupPQuota(self, group, printer) : |
---|
515 | """Initializes a group print quota on a printer.""" |
---|
516 | self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
517 | return self.getGroupPQuota(group, printer) |
---|
518 | |
---|
519 | def writePrinterPrices(self, printer) : |
---|
520 | """Write the printer's prices back into the storage.""" |
---|
521 | self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident))) |
---|
522 | |
---|
523 | def writePrinterDescription(self, printer) : |
---|
524 | """Write the printer's description back into the storage.""" |
---|
525 | description = self.userCharsetToDatabase(printer.Description) |
---|
526 | self.doModify("UPDATE printers SET description=%s WHERE id=%s" % (self.doQuote(description), self.doQuote(printer.ident))) |
---|
527 | |
---|
528 | def writeUserOverCharge(self, user, factor) : |
---|
529 | """Sets the user's overcharging coefficient.""" |
---|
530 | self.doModify("UPDATE users SET overcharge=%s WHERE id=%s" % (self.doQuote(factor), self.doQuote(user.ident))) |
---|
531 | |
---|
532 | def writeUserLimitBy(self, user, limitby) : |
---|
533 | """Sets the user's limiting factor.""" |
---|
534 | self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident))) |
---|
535 | |
---|
536 | def writeGroupLimitBy(self, group, limitby) : |
---|
537 | """Sets the group's limiting factor.""" |
---|
538 | self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident))) |
---|
539 | |
---|
540 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
541 | """Sets the date limit permanently for a user print quota.""" |
---|
542 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
543 | |
---|
544 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
545 | """Sets the date limit permanently for a group print quota.""" |
---|
546 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
547 | |
---|
548 | def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : |
---|
549 | """Increase page counters for a user print quota.""" |
---|
550 | self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) |
---|
551 | |
---|
552 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
553 | """Sets the new page counters permanently for a user print quota.""" |
---|
554 | self.doModify("UPDATE userpquota SET pagecounter=%s, lifepagecounter=%s, warncount=0, datelimit=NULL WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident))) |
---|
555 | |
---|
556 | def decreaseUserAccountBalance(self, user, amount) : |
---|
557 | """Decreases user's account balance from an amount.""" |
---|
558 | self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) |
---|
559 | |
---|
560 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
561 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
562 | if newlifetimepaid is not None : |
---|
563 | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
---|
564 | else : |
---|
565 | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
---|
566 | |
---|
567 | def writeNewPayment(self, user, amount) : |
---|
568 | """Adds a new payment to the payments history.""" |
---|
569 | self.doModify("INSERT INTO payments (userid, amount) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(amount))) |
---|
570 | |
---|
571 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
572 | """Sets the last job's size permanently.""" |
---|
573 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
574 | |
---|
575 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None) : |
---|
576 | """Adds a job in a printer's history.""" |
---|
577 | if self.privacy : |
---|
578 | # For legal reasons, we want to hide the title, filename and options |
---|
579 | title = filename = options = "Hidden because of privacy concerns" |
---|
580 | filename = self.userCharsetToDatabase(filename) |
---|
581 | title = self.userCharsetToDatabase(title) |
---|
582 | options = self.userCharsetToDatabase(options) |
---|
583 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
584 | if jobsize is not None : |
---|
585 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes, md5sum) VALUES (%s, %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), self.doQuote(jobmd5sum))) |
---|
586 | else : |
---|
587 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes, md5sum) VALUES (%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(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum))) |
---|
588 | else : |
---|
589 | # here we explicitly want to reset jobsize to NULL if needed |
---|
590 | 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, md5sum=%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(jobmd5sum), self.doQuote(printer.LastJob.ident))) |
---|
591 | |
---|
592 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
593 | """Sets soft and hard limits for a user quota.""" |
---|
594 | self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=0, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident))) |
---|
595 | |
---|
596 | def writeUserPQuotaWarnCount(self, userpquota, warncount) : |
---|
597 | """Sets the warn counter value for a user quota.""" |
---|
598 | self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) |
---|
599 | |
---|
600 | def increaseUserPQuotaWarnCount(self, userpquota) : |
---|
601 | """Increases the warn counter value for a user quota.""" |
---|
602 | self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) |
---|
603 | |
---|
604 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
605 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
606 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident))) |
---|
607 | |
---|
608 | def writePrinterToGroup(self, pgroup, printer) : |
---|
609 | """Puts a printer into a printer group.""" |
---|
610 | children = [] |
---|
611 | result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident)) |
---|
612 | if result : |
---|
613 | for record in result : |
---|
614 | children.append(record.get("printerid")) # TODO : put this into the database integrity rules |
---|
615 | if printer.ident not in children : |
---|
616 | self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
617 | |
---|
618 | def removePrinterFromGroup(self, pgroup, printer) : |
---|
619 | """Removes a printer from a printer group.""" |
---|
620 | self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
621 | |
---|
622 | def retrieveHistory(self, user=None, printer=None, datelimit=None, hostname=None, limit=100) : |
---|
623 | """Retrieves all print jobs for user on printer (or all) before date, limited to first 100 results.""" |
---|
624 | query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid" |
---|
625 | where = [] |
---|
626 | if (user is not None) and user.Exists : |
---|
627 | where.append("userid=%s" % self.doQuote(user.ident)) |
---|
628 | if (printer is not None) and printer.Exists : |
---|
629 | where.append("printerid=%s" % self.doQuote(printer.ident)) |
---|
630 | if hostname is not None : |
---|
631 | where.append("hostname=%s" % self.doQuote(hostname)) |
---|
632 | if datelimit is not None : |
---|
633 | where.append("jobdate<=%s" % self.doQuote(datelimit)) |
---|
634 | if where : |
---|
635 | query += " AND %s" % " AND ".join(where) |
---|
636 | query += " ORDER BY id DESC" |
---|
637 | if limit : |
---|
638 | query += " LIMIT %s" % self.doQuote(int(limit)) |
---|
639 | jobs = [] |
---|
640 | result = self.doSearch(query) |
---|
641 | if result : |
---|
642 | for fields in result : |
---|
643 | job = StorageJob(self) |
---|
644 | job.ident = fields.get("id") |
---|
645 | job.JobId = fields.get("jobid") |
---|
646 | job.PrinterPageCounter = fields.get("pagecounter") |
---|
647 | job.JobSize = fields.get("jobsize") |
---|
648 | job.JobPrice = fields.get("jobprice") |
---|
649 | job.JobAction = fields.get("action") |
---|
650 | job.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") |
---|
651 | job.JobTitle = self.databaseToUserCharset(fields.get("title") or "") |
---|
652 | job.JobCopies = fields.get("copies") |
---|
653 | job.JobOptions = self.databaseToUserCharset(fields.get("options") or "") |
---|
654 | job.JobDate = fields.get("jobdate") |
---|
655 | job.JobHostName = fields.get("hostname") |
---|
656 | job.JobSizeBytes = fields.get("jobsizebytes") |
---|
657 | job.JobMD5Sum = fields.get("md5sum") |
---|
658 | job.JobPages = fields.get("pages") |
---|
659 | job.JobBillingCode = fields.get("billingcode") |
---|
660 | job.UserName = fields.get("username") |
---|
661 | job.PrinterName = fields.get("printername") |
---|
662 | job.Exists = 1 |
---|
663 | jobs.append(job) |
---|
664 | return jobs |
---|
665 | |
---|
666 | def deleteUser(self, user) : |
---|
667 | """Completely deletes an user from the Quota Storage.""" |
---|
668 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
669 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
670 | # TODO : incorrectly charged (overcharged). |
---|
671 | for q in [ |
---|
672 | "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident), |
---|
673 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
674 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
675 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
676 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
677 | ] : |
---|
678 | self.doModify(q) |
---|
679 | |
---|
680 | def deleteGroup(self, group) : |
---|
681 | """Completely deletes a group from the Quota Storage.""" |
---|
682 | for q in [ |
---|
683 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
684 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
685 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
686 | ] : |
---|
687 | self.doModify(q) |
---|
688 | |
---|
689 | def deletePrinter(self, printer) : |
---|
690 | """Completely deletes a printer from the Quota Storage.""" |
---|
691 | for q in [ |
---|
692 | "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), |
---|
693 | "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
694 | "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
695 | "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
696 | "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident), |
---|
697 | ] : |
---|
698 | self.doModify(q) |
---|
699 | |
---|