[1327] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2622] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[1327] | 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 |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1327] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2066] | 23 | # |
---|
[1327] | 24 | |
---|
[2830] | 25 | from pykota.storage import StorageUser, StorageGroup, StoragePrinter, \ |
---|
[2342] | 26 | StorageJob, StorageLastJob, StorageUserPQuota, \ |
---|
| 27 | StorageGroupPQuota, StorageBillingCode |
---|
[1327] | 28 | |
---|
| 29 | class SQLStorage : |
---|
[2948] | 30 | def storageUserFromRecord(self, username, record) : |
---|
| 31 | """Returns a StorageUser instance from a database record.""" |
---|
| 32 | user = StorageUser(self, username) |
---|
| 33 | user.ident = record.get("uid", record.get("userid", record.get("id"))) |
---|
| 34 | user.LimitBy = record.get("limitby") or "quota" |
---|
| 35 | user.AccountBalance = record.get("balance") |
---|
| 36 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 37 | user.Email = record.get("email") |
---|
| 38 | user.Description = self.databaseToUserCharset(record.get("description")) |
---|
| 39 | user.OverCharge = record.get("overcharge", 1.0) |
---|
| 40 | user.Exists = True |
---|
| 41 | return user |
---|
| 42 | |
---|
| 43 | def storageGroupFromRecord(self, groupname, record) : |
---|
| 44 | """Returns a StorageGroup instance from a database record.""" |
---|
| 45 | group = StorageGroup(self, groupname) |
---|
| 46 | group.ident = record.get("id") |
---|
| 47 | group.LimitBy = record.get("limitby") or "quota" |
---|
| 48 | group.AccountBalance = record.get("balance") |
---|
| 49 | group.LifeTimePaid = record.get("lifetimepaid") |
---|
| 50 | group.Description = self.databaseToUserCharset(record.get("description")) |
---|
| 51 | group.Exists = True |
---|
| 52 | return group |
---|
| 53 | |
---|
| 54 | def storagePrinterFromRecord(self, printername, record) : |
---|
| 55 | """Returns a StoragePrinter instance from a database record.""" |
---|
| 56 | printer = StoragePrinter(self, printername) |
---|
| 57 | printer.ident = record.get("id") |
---|
| 58 | printer.PricePerJob = record.get("priceperjob") or 0.0 |
---|
| 59 | printer.PricePerPage = record.get("priceperpage") or 0.0 |
---|
| 60 | printer.MaxJobSize = record.get("maxjobsize") or 0 |
---|
| 61 | printer.PassThrough = record.get("passthrough") or 0 |
---|
| 62 | if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : |
---|
| 63 | printer.PassThrough = True |
---|
| 64 | else : |
---|
| 65 | printer.PassThrough = False |
---|
| 66 | printer.Description = self.databaseToUserCharset(record.get("description") or "") # TODO : is 'or ""' still needed ? |
---|
| 67 | printer.Exists = True |
---|
| 68 | return printer |
---|
| 69 | |
---|
| 70 | def setJobAttributesFromRecord(self, job, record) : |
---|
| 71 | """Sets the attributes of a job from a database record.""" |
---|
| 72 | job.ident = record.get("id") |
---|
| 73 | job.JobId = record.get("jobid") |
---|
| 74 | job.PrinterPageCounter = record.get("pagecounter") |
---|
| 75 | job.JobSize = record.get("jobsize") |
---|
| 76 | job.JobPrice = record.get("jobprice") |
---|
| 77 | job.JobAction = record.get("action") |
---|
| 78 | job.JobFileName = self.databaseToUserCharset(record.get("filename") or "") |
---|
| 79 | job.JobTitle = self.databaseToUserCharset(record.get("title") or "") |
---|
| 80 | job.JobCopies = record.get("copies") |
---|
| 81 | job.JobOptions = self.databaseToUserCharset(record.get("options") or "") |
---|
| 82 | job.JobDate = record.get("jobdate") |
---|
| 83 | job.JobHostName = record.get("hostname") |
---|
| 84 | job.JobSizeBytes = record.get("jobsizebytes") |
---|
| 85 | job.JobMD5Sum = record.get("md5sum") |
---|
| 86 | job.JobPages = record.get("pages") |
---|
| 87 | job.JobBillingCode = self.databaseToUserCharset(record.get("billingcode") or "") |
---|
| 88 | job.PrecomputedJobSize = record.get("precomputedjobsize") |
---|
| 89 | job.PrecomputedJobPrice = record.get("precomputedjobprice") |
---|
| 90 | job.UserName = self.databaseToUserCharset(record.get("username")) |
---|
| 91 | job.PrinterName = self.databaseToUserCharset(record.get("printername")) |
---|
| 92 | if job.JobTitle == job.JobFileName == job.JobOptions == "hidden" : |
---|
| 93 | (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 |
---|
| 94 | job.Exists = True |
---|
| 95 | |
---|
| 96 | def storageJobFromRecord(self, record) : |
---|
| 97 | """Returns a StorageJob instance from a database record.""" |
---|
| 98 | job = StorageJob(self) |
---|
| 99 | self.setJobAttributesFromRecord(job, record) |
---|
| 100 | return job |
---|
| 101 | |
---|
| 102 | def storageLastJobFromRecord(self, printer, record) : |
---|
| 103 | """Returns a StorageLastJob instance from a database record.""" |
---|
| 104 | lastjob = StorageLastJob(self, printer) |
---|
| 105 | self.setJobAttributesFromRecord(lastjob, record) |
---|
| 106 | return lastjob |
---|
| 107 | |
---|
| 108 | def storageUserPQuotaFromRecord(self, user, printer, record) : |
---|
| 109 | """Returns a StorageUserPQuota instance from a database record.""" |
---|
| 110 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 111 | userpquota.ident = record.get("id") |
---|
| 112 | userpquota.PageCounter = record.get("pagecounter") |
---|
| 113 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
| 114 | userpquota.SoftLimit = record.get("softlimit") |
---|
| 115 | userpquota.HardLimit = record.get("hardlimit") |
---|
| 116 | userpquota.DateLimit = record.get("datelimit") |
---|
| 117 | userpquota.WarnCount = record.get("warncount") or 0 |
---|
| 118 | userpquota.Exists = True |
---|
| 119 | return userpquota |
---|
| 120 | |
---|
| 121 | def storageGroupPQuotaFromRecord(self, group, printer, record) : |
---|
| 122 | """Returns a StorageGroupPQuota instance from a database record.""" |
---|
| 123 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 124 | grouppquota.ident = record.get("id") |
---|
| 125 | grouppquota.SoftLimit = record.get("softlimit") |
---|
| 126 | grouppquota.HardLimit = record.get("hardlimit") |
---|
| 127 | grouppquota.DateLimit = record.get("datelimit") |
---|
| 128 | 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)" \ |
---|
| 129 | % (self.doQuote(printer.ident), self.doQuote(group.ident))) |
---|
| 130 | if result : |
---|
| 131 | grouppquota.PageCounter = result[0].get("pagecounter") or 0 |
---|
| 132 | grouppquota.LifePageCounter = result[0].get("lifepagecounter") or 0 |
---|
| 133 | grouppquota.Exists = True |
---|
| 134 | return grouppquota |
---|
| 135 | |
---|
| 136 | def storageBillingCodeFromRecord(self, billingcode, record) : |
---|
| 137 | """Returns a StorageBillingCode instance from a database record.""" |
---|
| 138 | code = StorageBillingCode(self, billingcode) |
---|
| 139 | code.ident = record.get("id") |
---|
| 140 | code.Description = self.databaseToUserCharset(record.get("description") or "") # TODO : is 'or ""' still needed ? |
---|
| 141 | code.Balance = record.get("balance") or 0.0 |
---|
| 142 | code.PageCounter = record.get("pagecounter") or 0 |
---|
| 143 | code.Exists = True |
---|
| 144 | return code |
---|
| 145 | |
---|
[1991] | 146 | def createFilter(self, only) : |
---|
| 147 | """Returns the appropriate SQL filter.""" |
---|
| 148 | if only : |
---|
| 149 | expressions = [] |
---|
| 150 | for (k, v) in only.items() : |
---|
[2218] | 151 | expressions.append("%s=%s" % (k, self.doQuote(self.userCharsetToDatabase(v)))) |
---|
[1991] | 152 | return " AND ".join(expressions) |
---|
| 153 | return "" |
---|
| 154 | |
---|
[1990] | 155 | def extractPrinters(self, extractonly={}) : |
---|
[1717] | 156 | """Extracts all printer records.""" |
---|
[1991] | 157 | thefilter = self.createFilter(extractonly) |
---|
| 158 | if thefilter : |
---|
| 159 | thefilter = "WHERE %s" % thefilter |
---|
| 160 | result = self.doRawSearch("SELECT * FROM printers %s ORDER BY id ASC" % thefilter) |
---|
[1717] | 161 | return self.prepareRawResult(result) |
---|
| 162 | |
---|
[1990] | 163 | def extractUsers(self, extractonly={}) : |
---|
[1717] | 164 | """Extracts all user records.""" |
---|
[1991] | 165 | thefilter = self.createFilter(extractonly) |
---|
| 166 | if thefilter : |
---|
| 167 | thefilter = "WHERE %s" % thefilter |
---|
| 168 | result = self.doRawSearch("SELECT * FROM users %s ORDER BY id ASC" % thefilter) |
---|
[1717] | 169 | return self.prepareRawResult(result) |
---|
| 170 | |
---|
[2342] | 171 | def extractBillingcodes(self, extractonly={}) : |
---|
| 172 | """Extracts all billing codes records.""" |
---|
| 173 | thefilter = self.createFilter(extractonly) |
---|
| 174 | if thefilter : |
---|
| 175 | thefilter = "WHERE %s" % thefilter |
---|
| 176 | result = self.doRawSearch("SELECT * FROM billingcodes %s ORDER BY id ASC" % thefilter) |
---|
| 177 | return self.prepareRawResult(result) |
---|
| 178 | |
---|
[1990] | 179 | def extractGroups(self, extractonly={}) : |
---|
[1717] | 180 | """Extracts all group records.""" |
---|
[1991] | 181 | thefilter = self.createFilter(extractonly) |
---|
| 182 | if thefilter : |
---|
| 183 | thefilter = "WHERE %s" % thefilter |
---|
[2320] | 184 | 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,groups.description ORDER BY groups.id ASC" % thefilter) |
---|
[1717] | 185 | return self.prepareRawResult(result) |
---|
| 186 | |
---|
[1990] | 187 | def extractPayments(self, extractonly={}) : |
---|
[1717] | 188 | """Extracts all payment records.""" |
---|
[1991] | 189 | thefilter = self.createFilter(extractonly) |
---|
| 190 | if thefilter : |
---|
| 191 | thefilter = "AND %s" % thefilter |
---|
| 192 | result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %s ORDER BY payments.id ASC" % thefilter) |
---|
[1717] | 193 | return self.prepareRawResult(result) |
---|
| 194 | |
---|
[1990] | 195 | def extractUpquotas(self, extractonly={}) : |
---|
[1717] | 196 | """Extracts all userpquota records.""" |
---|
[1991] | 197 | thefilter = self.createFilter(extractonly) |
---|
| 198 | if thefilter : |
---|
| 199 | thefilter = "AND %s" % thefilter |
---|
| 200 | 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) |
---|
[1717] | 201 | return self.prepareRawResult(result) |
---|
| 202 | |
---|
[1990] | 203 | def extractGpquotas(self, extractonly={}) : |
---|
[1717] | 204 | """Extracts all grouppquota records.""" |
---|
[1991] | 205 | thefilter = self.createFilter(extractonly) |
---|
| 206 | if thefilter : |
---|
| 207 | thefilter = "AND %s" % thefilter |
---|
[2320] | 208 | 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,grouppquota.maxjobsize,groups.groupname,printers.printername ORDER BY grouppquota.id" % thefilter) |
---|
[1717] | 209 | return self.prepareRawResult(result) |
---|
| 210 | |
---|
[1990] | 211 | def extractUmembers(self, extractonly={}) : |
---|
[1718] | 212 | """Extracts all user groups members.""" |
---|
[1991] | 213 | thefilter = self.createFilter(extractonly) |
---|
| 214 | if thefilter : |
---|
| 215 | thefilter = "AND %s" % thefilter |
---|
| 216 | 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) |
---|
[1718] | 217 | return self.prepareRawResult(result) |
---|
| 218 | |
---|
[1990] | 219 | def extractPmembers(self, extractonly={}) : |
---|
[1718] | 220 | """Extracts all printer groups members.""" |
---|
[1992] | 221 | for (k, v) in extractonly.items() : |
---|
| 222 | if k == "pgroupname" : |
---|
| 223 | del extractonly[k] |
---|
| 224 | extractonly["p1.printername"] = v |
---|
| 225 | elif k == "printername" : |
---|
| 226 | del extractonly[k] |
---|
| 227 | extractonly["p2.printername"] = v |
---|
[1991] | 228 | thefilter = self.createFilter(extractonly) |
---|
| 229 | if thefilter : |
---|
| 230 | thefilter = "AND %s" % thefilter |
---|
| 231 | 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) |
---|
[1718] | 232 | return self.prepareRawResult(result) |
---|
| 233 | |
---|
[1990] | 234 | def extractHistory(self, extractonly={}) : |
---|
[1717] | 235 | """Extracts all jobhistory records.""" |
---|
[2266] | 236 | startdate = extractonly.get("start") |
---|
| 237 | enddate = extractonly.get("end") |
---|
| 238 | for limit in ("start", "end") : |
---|
| 239 | try : |
---|
| 240 | del extractonly[limit] |
---|
| 241 | except KeyError : |
---|
| 242 | pass |
---|
[1991] | 243 | thefilter = self.createFilter(extractonly) |
---|
| 244 | if thefilter : |
---|
| 245 | thefilter = "AND %s" % thefilter |
---|
[2266] | 246 | (startdate, enddate) = self.cleanDates(startdate, enddate) |
---|
[2665] | 247 | if startdate : |
---|
| 248 | thefilter = "%s AND jobdate>=%s" % (thefilter, self.doQuote(startdate)) |
---|
| 249 | if enddate : |
---|
| 250 | thefilter = "%s AND jobdate<=%s" % (thefilter, self.doQuote(enddate)) |
---|
[1991] | 251 | 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) |
---|
[1717] | 252 | return self.prepareRawResult(result) |
---|
[2266] | 253 | |
---|
[2651] | 254 | def filterNames(self, records, attribute, patterns=None) : |
---|
| 255 | """Returns a list of 'attribute' from a list of records. |
---|
| 256 | |
---|
| 257 | Logs any missing attribute. |
---|
| 258 | """ |
---|
| 259 | result = [] |
---|
| 260 | for record in records : |
---|
| 261 | attrval = record.get(attribute, [None]) |
---|
| 262 | if attrval is None : |
---|
| 263 | self.tool.printInfo("Object %s has no %s attribute !" % (repr(record), attribute), "error") |
---|
| 264 | else : |
---|
[2653] | 265 | attrval = self.databaseToUserCharset(attrval) |
---|
[2651] | 266 | if patterns : |
---|
| 267 | if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) : |
---|
| 268 | patterns = [ patterns ] |
---|
| 269 | if self.tool.matchString(attrval, patterns) : |
---|
| 270 | result.append(attrval) |
---|
| 271 | else : |
---|
| 272 | result.append(attrval) |
---|
| 273 | return result |
---|
| 274 | |
---|
| 275 | def getAllBillingCodes(self, billingcode=None) : |
---|
| 276 | """Extracts all billing codes or only the billing codes matching the optional parameter.""" |
---|
| 277 | result = self.doSearch("SELECT billingcode FROM billingcodes") |
---|
| 278 | if result : |
---|
| 279 | return self.filterNames(result, "billingcode", billingcode) |
---|
| 280 | else : |
---|
| 281 | return [] |
---|
| 282 | |
---|
| 283 | def getAllPrintersNames(self, printername=None) : |
---|
| 284 | """Extracts all printer names or only the printers' names matching the optional parameter.""" |
---|
| 285 | result = self.doSearch("SELECT printername FROM printers") |
---|
| 286 | if result : |
---|
| 287 | return self.filterNames(result, "printername", printername) |
---|
| 288 | else : |
---|
| 289 | return [] |
---|
| 290 | |
---|
| 291 | def getAllUsersNames(self, username=None) : |
---|
[1327] | 292 | """Extracts all user names.""" |
---|
| 293 | result = self.doSearch("SELECT username FROM users") |
---|
| 294 | if result : |
---|
[2651] | 295 | return self.filterNames(result, "username", username) |
---|
| 296 | else : |
---|
| 297 | return [] |
---|
[1327] | 298 | |
---|
[2651] | 299 | def getAllGroupsNames(self, groupname=None) : |
---|
[1327] | 300 | """Extracts all group names.""" |
---|
| 301 | result = self.doSearch("SELECT groupname FROM groups") |
---|
| 302 | if result : |
---|
[2651] | 303 | return self.filterNames(result, "groupname", groupname) |
---|
| 304 | else : |
---|
| 305 | return [] |
---|
[1327] | 306 | |
---|
[1806] | 307 | def getUserNbJobsFromHistory(self, user) : |
---|
| 308 | """Returns the number of jobs the user has in history.""" |
---|
| 309 | result = self.doSearch("SELECT COUNT(*) FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 310 | if result : |
---|
| 311 | return result[0]["count"] |
---|
| 312 | return 0 |
---|
| 313 | |
---|
[1327] | 314 | def getUserFromBackend(self, username) : |
---|
| 315 | """Extracts user information given its name.""" |
---|
[2948] | 316 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1"\ |
---|
| 317 | % self.doQuote(self.userCharsetToDatabase(username))) |
---|
[1327] | 318 | if result : |
---|
[2948] | 319 | return self.storageUserFromRecord(username, result[0]) |
---|
| 320 | else : |
---|
| 321 | return StorageUser(self, username) |
---|
[1327] | 322 | |
---|
| 323 | def getGroupFromBackend(self, groupname) : |
---|
| 324 | """Extracts group information given its name.""" |
---|
[2948] | 325 | 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,groups.description LIMIT 1" \ |
---|
| 326 | % self.doQuote(self.userCharsetToDatabase(groupname))) |
---|
[1327] | 327 | if result : |
---|
[2948] | 328 | return self.storageGroupFromRecord(groupname, result[0]) |
---|
| 329 | else : |
---|
| 330 | return StorageGroup(self, groupname) |
---|
[1327] | 331 | |
---|
| 332 | def getPrinterFromBackend(self, printername) : |
---|
| 333 | """Extracts printer information given its name.""" |
---|
[2948] | 334 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" \ |
---|
| 335 | % self.doQuote(self.userCharsetToDatabase(printername))) |
---|
[1327] | 336 | if result : |
---|
[2948] | 337 | return self.storagePrinterFromRecord(printername, result[0]) |
---|
| 338 | else : |
---|
| 339 | return StoragePrinter(self, printername) |
---|
[1327] | 340 | |
---|
[2342] | 341 | def getBillingCodeFromBackend(self, label) : |
---|
| 342 | """Extracts a billing code information given its name.""" |
---|
[2948] | 343 | result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s LIMIT 1" \ |
---|
| 344 | % self.doQuote(self.userCharsetToDatabase(label))) |
---|
[2342] | 345 | if result : |
---|
[2948] | 346 | return self.storageBillingCodeFromRecord(label, result[0]) |
---|
| 347 | else : |
---|
| 348 | return StorageBillingCode(self, label) |
---|
[2342] | 349 | |
---|
[1327] | 350 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
| 351 | """Extracts a user print quota.""" |
---|
| 352 | if printer.Exists and user.Exists : |
---|
[2948] | 353 | result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s;" \ |
---|
| 354 | % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
[1327] | 355 | if result : |
---|
[2948] | 356 | return self.storageUserPQuotaFromRecord(user, printer, result[0]) |
---|
| 357 | return StorageUserPQuota(self, user, printer) |
---|
[1327] | 358 | |
---|
| 359 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
| 360 | """Extracts a group print quota.""" |
---|
[2948] | 361 | if printer.Exists and group.Exists : |
---|
| 362 | result = self.doSearch("SELECT * FROM grouppquota WHERE groupid=%s AND printerid=%s" \ |
---|
| 363 | % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
[1327] | 364 | if result : |
---|
[2948] | 365 | return self.storageGroupPQuotaFromRecord(group, printer, result[0]) |
---|
| 366 | return StorageGroupPQuota(self, group, printer) |
---|
[1327] | 367 | |
---|
| 368 | def getPrinterLastJobFromBackend(self, printer) : |
---|
| 369 | """Extracts a printer's last job information.""" |
---|
[2455] | 370 | result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, hostname, jobdate, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident)) |
---|
[1327] | 371 | if result : |
---|
[2948] | 372 | return self.storageLastJobFromRecord(printer, result[0]) |
---|
| 373 | else : |
---|
| 374 | return StorageLastJob(self, printer) |
---|
[1327] | 375 | |
---|
| 376 | def getGroupMembersFromBackend(self, group) : |
---|
| 377 | """Returns the group's members list.""" |
---|
| 378 | groupmembers = [] |
---|
| 379 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
| 380 | if result : |
---|
| 381 | for record in result : |
---|
[2948] | 382 | user = self.storageUserFromRecord(self.databaseToUserCharset(record.get("username")), \ |
---|
| 383 | record) |
---|
[1327] | 384 | groupmembers.append(user) |
---|
| 385 | self.cacheEntry("USERS", user.Name, user) |
---|
| 386 | return groupmembers |
---|
| 387 | |
---|
| 388 | def getUserGroupsFromBackend(self, user) : |
---|
| 389 | """Returns the user's groups list.""" |
---|
| 390 | groups = [] |
---|
| 391 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 392 | if result : |
---|
| 393 | for record in result : |
---|
[2654] | 394 | groups.append(self.getGroup(self.databaseToUserCharset(record.get("groupname")))) |
---|
[1327] | 395 | return groups |
---|
| 396 | |
---|
| 397 | def getParentPrintersFromBackend(self, printer) : |
---|
| 398 | """Get all the printer groups this printer is a member of.""" |
---|
| 399 | pgroups = [] |
---|
| 400 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident)) |
---|
| 401 | if result : |
---|
| 402 | for record in result : |
---|
| 403 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
[2654] | 404 | parentprinter = self.getPrinter(self.databaseToUserCharset(record.get("printername"))) |
---|
[1327] | 405 | if parentprinter.Exists : |
---|
| 406 | pgroups.append(parentprinter) |
---|
| 407 | return pgroups |
---|
| 408 | |
---|
| 409 | def getMatchingPrinters(self, printerpattern) : |
---|
| 410 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 411 | printers = [] |
---|
| 412 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
| 413 | # but we don't because other storages semantics may be different, so every |
---|
| 414 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
| 415 | result = self.doSearch("SELECT * FROM printers") |
---|
| 416 | if result : |
---|
[2657] | 417 | patterns = printerpattern.split(",") |
---|
[2754] | 418 | try : |
---|
| 419 | patdict = {}.fromkeys(patterns) |
---|
| 420 | except AttributeError : |
---|
| 421 | # Python v2.2 or earlier |
---|
| 422 | patdict = {} |
---|
| 423 | for p in patterns : |
---|
| 424 | patdict[p] = None |
---|
[1327] | 425 | for record in result : |
---|
[2654] | 426 | pname = self.databaseToUserCharset(record["printername"]) |
---|
[2754] | 427 | if patdict.has_key(pname) or self.tool.matchString(pname, patterns) : |
---|
[2948] | 428 | printer = self.storagePrinterFromRecord(pname, record) |
---|
[1327] | 429 | printers.append(printer) |
---|
| 430 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
| 431 | return printers |
---|
| 432 | |
---|
[2657] | 433 | def getMatchingUsers(self, userpattern) : |
---|
| 434 | """Returns the list of all users for which name matches a certain pattern.""" |
---|
| 435 | users = [] |
---|
| 436 | # We 'could' do a SELECT username FROM users WHERE username LIKE ... |
---|
| 437 | # but we don't because other storages semantics may be different, so every |
---|
| 438 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
| 439 | result = self.doSearch("SELECT * FROM users") |
---|
| 440 | if result : |
---|
| 441 | patterns = userpattern.split(",") |
---|
[2754] | 442 | try : |
---|
| 443 | patdict = {}.fromkeys(patterns) |
---|
| 444 | except AttributeError : |
---|
| 445 | # Python v2.2 or earlier |
---|
| 446 | patdict = {} |
---|
| 447 | for p in patterns : |
---|
| 448 | patdict[p] = None |
---|
[2657] | 449 | for record in result : |
---|
| 450 | uname = self.databaseToUserCharset(record["username"]) |
---|
[2754] | 451 | if patdict.has_key(uname) or self.tool.matchString(uname, patterns) : |
---|
[2948] | 452 | user = self.storageUserFromRecord(uname, record) |
---|
[2657] | 453 | users.append(user) |
---|
| 454 | self.cacheEntry("USERS", user.Name, user) |
---|
| 455 | return users |
---|
| 456 | |
---|
| 457 | def getMatchingGroups(self, grouppattern) : |
---|
| 458 | """Returns the list of all groups for which name matches a certain pattern.""" |
---|
| 459 | groups = [] |
---|
| 460 | # We 'could' do a SELECT groupname FROM groups WHERE groupname LIKE ... |
---|
| 461 | # but we don't because other storages semantics may be different, so every |
---|
| 462 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
| 463 | 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) GROUP BY groups.id,groups.groupname,groups.limitby,groups.description") |
---|
| 464 | if result : |
---|
| 465 | patterns = grouppattern.split(",") |
---|
[2754] | 466 | try : |
---|
| 467 | patdict = {}.fromkeys(patterns) |
---|
| 468 | except AttributeError : |
---|
| 469 | # Python v2.2 or earlier |
---|
| 470 | patdict = {} |
---|
| 471 | for p in patterns : |
---|
| 472 | patdict[p] = None |
---|
[2657] | 473 | for record in result : |
---|
| 474 | gname = self.databaseToUserCharset(record["groupname"]) |
---|
[2754] | 475 | if patdict.has_key(gname) or self.tool.matchString(gname, patterns) : |
---|
[2948] | 476 | group = self.storageGroupFromRecord(gname, record) |
---|
[2657] | 477 | groups.append(group) |
---|
| 478 | self.cacheEntry("GROUPS", group.Name, group) |
---|
| 479 | return groups |
---|
| 480 | |
---|
[2342] | 481 | def getMatchingBillingCodes(self, billingcodepattern) : |
---|
| 482 | """Returns the list of all billing codes for which the label matches a certain pattern.""" |
---|
| 483 | codes = [] |
---|
| 484 | result = self.doSearch("SELECT * FROM billingcodes") |
---|
| 485 | if result : |
---|
[2657] | 486 | patterns = billingcodepattern.split(",") |
---|
[2775] | 487 | try : |
---|
| 488 | patdict = {}.fromkeys(patterns) |
---|
| 489 | except AttributeError : |
---|
| 490 | # Python v2.2 or earlier |
---|
| 491 | patdict = {} |
---|
| 492 | for p in patterns : |
---|
| 493 | patdict[p] = None |
---|
[2342] | 494 | for record in result : |
---|
[2678] | 495 | codename = self.databaseToUserCharset(record["billingcode"]) |
---|
[2775] | 496 | if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : |
---|
[2948] | 497 | code = self.storageBillingCodeFromRecord(codename, record) |
---|
[2342] | 498 | codes.append(code) |
---|
| 499 | self.cacheEntry("BILLINGCODES", code.BillingCode, code) |
---|
| 500 | return codes |
---|
| 501 | |
---|
[1327] | 502 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
| 503 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 504 | usersandquotas = [] |
---|
[2722] | 505 | result = self.doSearch("SELECT users.id as uid,username,description,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)) |
---|
[1327] | 506 | if result : |
---|
| 507 | for record in result : |
---|
[2654] | 508 | uname = self.databaseToUserCharset(record.get("username")) |
---|
| 509 | if self.tool.matchString(uname, names) : |
---|
[2948] | 510 | user = self.storageUserFromRecord(uname, record) |
---|
| 511 | userpquota = self.storageUserPQuotaFromRecord(user, printer, record) |
---|
[1327] | 512 | usersandquotas.append((user, userpquota)) |
---|
| 513 | self.cacheEntry("USERS", user.Name, user) |
---|
| 514 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
| 515 | return usersandquotas |
---|
| 516 | |
---|
| 517 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
| 518 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 519 | groupsandquotas = [] |
---|
| 520 | 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)) |
---|
| 521 | if result : |
---|
| 522 | for record in result : |
---|
[2654] | 523 | gname = self.databaseToUserCharset(record.get("groupname")) |
---|
| 524 | if self.tool.matchString(gname, names) : |
---|
| 525 | group = self.getGroup(gname) |
---|
[1327] | 526 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 527 | groupsandquotas.append((group, grouppquota)) |
---|
| 528 | return groupsandquotas |
---|
| 529 | |
---|
[2768] | 530 | def addPrinter(self, printer) : |
---|
| 531 | """Adds a printer to the quota storage, returns the old value if it already exists.""" |
---|
[2787] | 532 | oldentry = self.getPrinter(printer.Name) |
---|
| 533 | if oldentry.Exists : |
---|
| 534 | return oldentry |
---|
| 535 | self.doModify("INSERT INTO printers (printername, passthrough, maxjobsize, description, priceperpage, priceperjob) VALUES (%s, %s, %s, %s, %s, %s)" \ |
---|
| 536 | % (self.doQuote(self.userCharsetToDatabase(printer.Name)), \ |
---|
| 537 | self.doQuote((printer.PassThrough and "t") or "f"), \ |
---|
| 538 | self.doQuote(printer.MaxJobSize or 0), \ |
---|
| 539 | self.doQuote(self.userCharsetToDatabase(printer.Description)), \ |
---|
| 540 | self.doQuote(printer.PricePerPage or 0.0), \ |
---|
| 541 | self.doQuote(printer.PricePerJob or 0.0))) |
---|
| 542 | printer.isDirty = False |
---|
| 543 | return None # the entry created doesn't need further modification |
---|
[1327] | 544 | |
---|
[2765] | 545 | def addBillingCode(self, bcode) : |
---|
| 546 | """Adds a billing code to the quota storage, returns the old value if it already exists.""" |
---|
[2787] | 547 | oldentry = self.getBillingCode(bcode.BillingCode) |
---|
| 548 | if oldentry.Exists : |
---|
| 549 | return oldentry |
---|
| 550 | self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ |
---|
| 551 | % (self.doQuote(self.userCharsetToDatabase(bcode.BillingCode)), |
---|
| 552 | self.doQuote(bcode.Balance or 0.0), \ |
---|
| 553 | self.doQuote(bcode.PageCounter or 0), \ |
---|
| 554 | self.doQuote(self.userCharsetToDatabase(bcode.Description)))) |
---|
| 555 | bcode.isDirty = False |
---|
| 556 | return None # the entry created doesn't need further modification |
---|
[2342] | 557 | |
---|
[1327] | 558 | def addUser(self, user) : |
---|
[2773] | 559 | """Adds a user to the quota storage, returns the old value if it already exists.""" |
---|
[2787] | 560 | oldentry = self.getUser(user.Name) |
---|
| 561 | if oldentry.Exists : |
---|
| 562 | return oldentry |
---|
| 563 | self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ |
---|
| 564 | (self.doQuote(self.userCharsetToDatabase(user.Name)), \ |
---|
| 565 | self.doQuote(user.LimitBy or 'quota'), \ |
---|
| 566 | self.doQuote(user.AccountBalance or 0.0), \ |
---|
| 567 | self.doQuote(user.LifeTimePaid or 0.0), \ |
---|
| 568 | self.doQuote(user.Email), \ |
---|
| 569 | self.doQuote(user.OverCharge), \ |
---|
| 570 | self.doQuote(self.userCharsetToDatabase(user.Description)))) |
---|
| 571 | if user.PaymentsBacklog : |
---|
| 572 | for (value, comment) in user.PaymentsBacklog : |
---|
| 573 | self.writeNewPayment(user, value, comment) |
---|
| 574 | user.PaymentsBacklog = [] |
---|
| 575 | user.isDirty = False |
---|
| 576 | return None # the entry created doesn't need further modification |
---|
[1327] | 577 | |
---|
| 578 | def addGroup(self, group) : |
---|
[2773] | 579 | """Adds a group to the quota storage, returns the old value if it already exists.""" |
---|
[2787] | 580 | oldentry = self.getGroup(group.Name) |
---|
| 581 | if oldentry.Exists : |
---|
| 582 | return oldentry |
---|
| 583 | self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ |
---|
| 584 | (self.doQuote(self.userCharsetToDatabase(group.Name)), \ |
---|
| 585 | self.doQuote(group.LimitBy or "quota"), \ |
---|
| 586 | self.doQuote(self.userCharsetToDatabase(group.Description)))) |
---|
| 587 | group.isDirty = False |
---|
| 588 | return None # the entry created doesn't need further modification |
---|
[1327] | 589 | |
---|
| 590 | def addUserToGroup(self, user, group) : |
---|
| 591 | """Adds an user to a group.""" |
---|
| 592 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 593 | try : |
---|
| 594 | mexists = int(result[0].get("mexists")) |
---|
| 595 | except (IndexError, TypeError) : |
---|
| 596 | mexists = 0 |
---|
| 597 | if not mexists : |
---|
| 598 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 599 | |
---|
[2706] | 600 | def delUserFromGroup(self, user, group) : |
---|
| 601 | """Removes an user from a group.""" |
---|
| 602 | self.doModify("DELETE FROM groupsmembers WHERE groupid=%s AND userid=%s" % \ |
---|
| 603 | (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 604 | |
---|
[2749] | 605 | def addUserPQuota(self, upq) : |
---|
[1327] | 606 | """Initializes a user print quota on a printer.""" |
---|
[2787] | 607 | oldentry = self.getUserPQuota(upq.User, upq.Printer) |
---|
| 608 | if oldentry.Exists : |
---|
| 609 | return oldentry |
---|
| 610 | self.doModify("INSERT INTO userpquota (userid, printerid, softlimit, hardlimit, warncount, datelimit, pagecounter, lifepagecounter, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" \ |
---|
| 611 | % (self.doQuote(upq.User.ident), \ |
---|
| 612 | self.doQuote(upq.Printer.ident), \ |
---|
| 613 | self.doQuote(upq.SoftLimit), \ |
---|
| 614 | self.doQuote(upq.HardLimit), \ |
---|
[2857] | 615 | self.doQuote(upq.WarnCount or 0), \ |
---|
[2787] | 616 | self.doQuote(upq.DateLimit), \ |
---|
| 617 | self.doQuote(upq.PageCounter or 0), \ |
---|
| 618 | self.doQuote(upq.LifePageCounter or 0), \ |
---|
| 619 | self.doQuote(upq.MaxJobSize))) |
---|
| 620 | upq.isDirty = False |
---|
| 621 | return None # the entry created doesn't need further modification |
---|
[1327] | 622 | |
---|
[2749] | 623 | def addGroupPQuota(self, gpq) : |
---|
[1327] | 624 | """Initializes a group print quota on a printer.""" |
---|
[2787] | 625 | oldentry = self.getGroupPQuota(gpq.Group, gpq.Printer) |
---|
| 626 | if oldentry.Exists : |
---|
| 627 | return oldentry |
---|
| 628 | self.doModify("INSERT INTO grouppquota (groupid, printerid, softlimit, hardlimit, datelimit, maxjobsize) VALUES (%s, %s, %s, %s, %s, %s)" \ |
---|
| 629 | % (self.doQuote(gpq.Group.ident), \ |
---|
| 630 | self.doQuote(gpq.Printer.ident), \ |
---|
| 631 | self.doQuote(gpq.SoftLimit), \ |
---|
| 632 | self.doQuote(gpq.HardLimit), \ |
---|
| 633 | self.doQuote(gpq.DateLimit), \ |
---|
| 634 | self.doQuote(gpq.MaxJobSize))) |
---|
| 635 | gpq.isDirty = False |
---|
| 636 | return None # the entry created doesn't need further modification |
---|
[1327] | 637 | |
---|
[2686] | 638 | def savePrinter(self, printer) : |
---|
| 639 | """Saves the printer to the database in a single operation.""" |
---|
| 640 | self.doModify("UPDATE printers SET passthrough=%s, maxjobsize=%s, description=%s, priceperpage=%s, priceperjob=%s WHERE id=%s" \ |
---|
| 641 | % (self.doQuote((printer.PassThrough and "t") or "f"), \ |
---|
[2768] | 642 | self.doQuote(printer.MaxJobSize or 0), \ |
---|
[2686] | 643 | self.doQuote(self.userCharsetToDatabase(printer.Description)), \ |
---|
[2768] | 644 | self.doQuote(printer.PricePerPage or 0.0), \ |
---|
| 645 | self.doQuote(printer.PricePerJob or 0.0), \ |
---|
[2686] | 646 | self.doQuote(printer.ident))) |
---|
[2706] | 647 | |
---|
| 648 | def saveUser(self, user) : |
---|
| 649 | """Saves the user to the database in a single operation.""" |
---|
[2707] | 650 | self.doModify("UPDATE users SET limitby=%s, balance=%s, lifetimepaid=%s, email=%s, overcharge=%s, description=%s WHERE id=%s" \ |
---|
[2706] | 651 | % (self.doQuote(user.LimitBy or 'quota'), \ |
---|
[2707] | 652 | self.doQuote(user.AccountBalance or 0.0), \ |
---|
| 653 | self.doQuote(user.LifeTimePaid or 0.0), \ |
---|
[2706] | 654 | self.doQuote(user.Email), \ |
---|
| 655 | self.doQuote(user.OverCharge), \ |
---|
| 656 | self.doQuote(self.userCharsetToDatabase(user.Description)), \ |
---|
| 657 | self.doQuote(user.ident))) |
---|
| 658 | |
---|
| 659 | def saveGroup(self, group) : |
---|
| 660 | """Saves the group to the database in a single operation.""" |
---|
| 661 | self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \ |
---|
| 662 | % (self.doQuote(group.LimitBy or 'quota'), \ |
---|
| 663 | self.doQuote(self.userCharsetToDatabase(group.Description)), \ |
---|
| 664 | self.doQuote(group.ident))) |
---|
[1327] | 665 | |
---|
| 666 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 667 | """Sets the date limit permanently for a user print quota.""" |
---|
| 668 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
| 669 | |
---|
| 670 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 671 | """Sets the date limit permanently for a group print quota.""" |
---|
| 672 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
| 673 | |
---|
| 674 | def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : |
---|
| 675 | """Increase page counters for a user print quota.""" |
---|
[2054] | 676 | self.doModify("UPDATE userpquota SET pagecounter=pagecounter + %s,lifepagecounter=lifepagecounter + %s WHERE id=%s" % (self.doQuote(nbpages), self.doQuote(nbpages), self.doQuote(userpquota.ident))) |
---|
[1327] | 677 | |
---|
[2765] | 678 | def saveBillingCode(self, bcode) : |
---|
[2676] | 679 | """Saves the billing code to the database.""" |
---|
| 680 | self.doModify("UPDATE billingcodes SET balance=%s, pagecounter=%s, description=%s WHERE id=%s" \ |
---|
[2765] | 681 | % (self.doQuote(bcode.Balance or 0.0), \ |
---|
| 682 | self.doQuote(bcode.PageCounter or 0), \ |
---|
| 683 | self.doQuote(self.userCharsetToDatabase(bcode.Description)), \ |
---|
| 684 | self.doQuote(bcode.ident))) |
---|
[2342] | 685 | |
---|
[2765] | 686 | def consumeBillingCode(self, bcode, pagecounter, balance) : |
---|
[2342] | 687 | """Consumes from a billing code.""" |
---|
[2765] | 688 | self.doModify("UPDATE billingcodes SET balance=balance + %s, pagecounter=pagecounter + %s WHERE id=%s" % (self.doQuote(balance), self.doQuote(pagecounter), self.doQuote(bcode.ident))) |
---|
[2342] | 689 | |
---|
[1327] | 690 | def decreaseUserAccountBalance(self, user, amount) : |
---|
| 691 | """Decreases user's account balance from an amount.""" |
---|
[2054] | 692 | self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) |
---|
[1327] | 693 | |
---|
[2452] | 694 | def writeNewPayment(self, user, amount, comment="") : |
---|
[1522] | 695 | """Adds a new payment to the payments history.""" |
---|
[2773] | 696 | if user.ident is not None : |
---|
| 697 | self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment)))) |
---|
| 698 | else : |
---|
| 699 | self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(self.userCharsetToDatabase(user.Name)), self.doQuote(amount), self.doQuote(self.userCharsetToDatabase(comment)))) |
---|
[1522] | 700 | |
---|
[1327] | 701 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
| 702 | """Sets the last job's size permanently.""" |
---|
| 703 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
| 704 | |
---|
[2455] | 705 | 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, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) : |
---|
[1327] | 706 | """Adds a job in a printer's history.""" |
---|
[1875] | 707 | if self.privacy : |
---|
| 708 | # For legal reasons, we want to hide the title, filename and options |
---|
[2287] | 709 | title = filename = options = "hidden" |
---|
[1790] | 710 | filename = self.userCharsetToDatabase(filename) |
---|
| 711 | title = self.userCharsetToDatabase(title) |
---|
| 712 | options = self.userCharsetToDatabase(options) |
---|
[2217] | 713 | jobbilling = self.userCharsetToDatabase(jobbilling) |
---|
[1327] | 714 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 715 | if jobsize is not None : |
---|
[2455] | 716 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %s, %s, %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), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice))) |
---|
[1327] | 717 | else : |
---|
[2455] | 718 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options, hostname, jobsizebytes, md5sum, pages, billingcode, precomputedjobsize, precomputedjobprice) VALUES (%s, %s, %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(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options), self.doQuote(clienthost), self.doQuote(jobsizebytes), self.doQuote(jobmd5sum), self.doQuote(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice))) |
---|
[1327] | 719 | else : |
---|
| 720 | # here we explicitly want to reset jobsize to NULL if needed |
---|
[2455] | 721 | 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, pages=%s, billingcode=%s, precomputedjobsize=%s, precomputedjobprice=%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(jobpages), self.doQuote(jobbilling), self.doQuote(precomputedsize), self.doQuote(precomputedprice), self.doQuote(printer.LastJob.ident))) |
---|
[1327] | 722 | |
---|
[2735] | 723 | def saveUserPQuota(self, userpquota) : |
---|
| 724 | """Saves an user print quota entry.""" |
---|
[2749] | 725 | self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, warncount=%s, datelimit=%s, pagecounter=%s, lifepagecounter=%s, maxjobsize=%s WHERE id=%s" \ |
---|
[2735] | 726 | % (self.doQuote(userpquota.SoftLimit), \ |
---|
| 727 | self.doQuote(userpquota.HardLimit), \ |
---|
[2857] | 728 | self.doQuote(userpquota.WarnCount or 0), \ |
---|
[2735] | 729 | self.doQuote(userpquota.DateLimit), \ |
---|
[2758] | 730 | self.doQuote(userpquota.PageCounter or 0), \ |
---|
| 731 | self.doQuote(userpquota.LifePageCounter or 0), \ |
---|
[2749] | 732 | self.doQuote(userpquota.MaxJobSize), \ |
---|
[2735] | 733 | self.doQuote(userpquota.ident))) |
---|
[1327] | 734 | |
---|
[2054] | 735 | def writeUserPQuotaWarnCount(self, userpquota, warncount) : |
---|
| 736 | """Sets the warn counter value for a user quota.""" |
---|
| 737 | self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) |
---|
| 738 | |
---|
| 739 | def increaseUserPQuotaWarnCount(self, userpquota) : |
---|
| 740 | """Increases the warn counter value for a user quota.""" |
---|
| 741 | self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) |
---|
| 742 | |
---|
[2735] | 743 | def saveGroupPQuota(self, grouppquota) : |
---|
| 744 | """Saves a group print quota entry.""" |
---|
| 745 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=%s WHERE id=%s" \ |
---|
| 746 | % (self.doQuote(grouppquota.SoftLimit), \ |
---|
| 747 | self.doQuote(grouppquota.HardLimit), \ |
---|
| 748 | self.doQuote(grouppquota.DateLimit), \ |
---|
| 749 | self.doQuote(grouppquota.ident))) |
---|
[1327] | 750 | |
---|
| 751 | def writePrinterToGroup(self, pgroup, printer) : |
---|
| 752 | """Puts a printer into a printer group.""" |
---|
| 753 | children = [] |
---|
| 754 | result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident)) |
---|
| 755 | if result : |
---|
| 756 | for record in result : |
---|
| 757 | children.append(record.get("printerid")) # TODO : put this into the database integrity rules |
---|
| 758 | if printer.ident not in children : |
---|
| 759 | self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 760 | |
---|
[1332] | 761 | def removePrinterFromGroup(self, pgroup, printer) : |
---|
| 762 | """Removes a printer from a printer group.""" |
---|
| 763 | self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 764 | |
---|
[2266] | 765 | def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, limit=100, start=None, end=None) : |
---|
| 766 | """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results.""" |
---|
[1327] | 767 | query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid" |
---|
| 768 | where = [] |
---|
[2222] | 769 | if user is not None : # user.ident is None anyway if user doesn't exist |
---|
[1327] | 770 | where.append("userid=%s" % self.doQuote(user.ident)) |
---|
[2222] | 771 | if printer is not None : # printer.ident is None anyway if printer doesn't exist |
---|
[1327] | 772 | where.append("printerid=%s" % self.doQuote(printer.ident)) |
---|
[1502] | 773 | if hostname is not None : |
---|
| 774 | where.append("hostname=%s" % self.doQuote(hostname)) |
---|
[2220] | 775 | if billingcode is not None : |
---|
| 776 | where.append("billingcode=%s" % self.doQuote(self.userCharsetToDatabase(billingcode))) |
---|
[2266] | 777 | if start is not None : |
---|
| 778 | where.append("jobdate>=%s" % self.doQuote(start)) |
---|
| 779 | if end is not None : |
---|
| 780 | where.append("jobdate<=%s" % self.doQuote(end)) |
---|
[1327] | 781 | if where : |
---|
| 782 | query += " AND %s" % " AND ".join(where) |
---|
[2596] | 783 | query += " ORDER BY jobhistory.id DESC" |
---|
[1327] | 784 | if limit : |
---|
| 785 | query += " LIMIT %s" % self.doQuote(int(limit)) |
---|
| 786 | jobs = [] |
---|
| 787 | result = self.doSearch(query) |
---|
| 788 | if result : |
---|
| 789 | for fields in result : |
---|
[2948] | 790 | job = storageJobFromRecord(fields) |
---|
[1327] | 791 | jobs.append(job) |
---|
| 792 | return jobs |
---|
| 793 | |
---|
| 794 | def deleteUser(self, user) : |
---|
[2717] | 795 | """Completely deletes an user from the database.""" |
---|
[1327] | 796 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 797 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 798 | # TODO : incorrectly charged (overcharged). |
---|
| 799 | for q in [ |
---|
[1531] | 800 | "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident), |
---|
[1327] | 801 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 802 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 803 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 804 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
| 805 | ] : |
---|
| 806 | self.doModify(q) |
---|
[2717] | 807 | |
---|
[2771] | 808 | def multipleQueriesInTransaction(self, queries) : |
---|
| 809 | """Does many modifications in a single transaction.""" |
---|
[2763] | 810 | self.beginTransaction() |
---|
| 811 | try : |
---|
| 812 | for q in queries : |
---|
| 813 | self.doModify(q) |
---|
| 814 | except : |
---|
| 815 | self.rollbackTransaction() |
---|
| 816 | raise |
---|
| 817 | else : |
---|
| 818 | self.commitTransaction() |
---|
| 819 | |
---|
[2765] | 820 | def deleteManyBillingCodes(self, billingcodes) : |
---|
| 821 | """Deletes many billing codes.""" |
---|
| 822 | codeids = ", ".join(["%s" % self.doQuote(b.ident) for b in billingcodes]) |
---|
[2771] | 823 | self.multipleQueriesInTransaction([ |
---|
[2765] | 824 | "DELETE FROM billingcodes WHERE id IN (%s)" % codeids,]) |
---|
[2763] | 825 | |
---|
[2765] | 826 | def deleteManyUsers(self, users) : |
---|
| 827 | """Deletes many users.""" |
---|
| 828 | userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) |
---|
[2771] | 829 | self.multipleQueriesInTransaction([ |
---|
[2765] | 830 | "DELETE FROM payments WHERE userid IN (%s)" % userids, |
---|
| 831 | "DELETE FROM groupsmembers WHERE userid IN (%s)" % userids, |
---|
| 832 | "DELETE FROM jobhistory WHERE userid IN (%s)" % userids, |
---|
| 833 | "DELETE FROM userpquota WHERE userid IN (%s)" % userids, |
---|
| 834 | "DELETE FROM users WHERE id IN (%s)" % userids,]) |
---|
| 835 | |
---|
| 836 | def deleteManyGroups(self, groups) : |
---|
| 837 | """Deletes many groups.""" |
---|
| 838 | groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) |
---|
[2771] | 839 | self.multipleQueriesInTransaction([ |
---|
[2765] | 840 | "DELETE FROM groupsmembers WHERE groupid IN (%s)" % groupids, |
---|
| 841 | "DELETE FROM grouppquota WHERE groupid IN (%s)" % groupids, |
---|
| 842 | "DELETE FROM groups WHERE id IN (%s)" % groupids,]) |
---|
[2763] | 843 | |
---|
[2765] | 844 | def deleteManyPrinters(self, printers) : |
---|
| 845 | """Deletes many printers.""" |
---|
| 846 | printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) |
---|
[2771] | 847 | self.multipleQueriesInTransaction([ |
---|
[2768] | 848 | "DELETE FROM printergroupsmembers WHERE groupid IN (%s) OR printerid IN (%s)" % (printerids, printerids), |
---|
[2765] | 849 | "DELETE FROM jobhistory WHERE printerid IN (%s)" % printerids, |
---|
| 850 | "DELETE FROM grouppquota WHERE printerid IN (%s)" % printerids, |
---|
| 851 | "DELETE FROM userpquota WHERE printerid IN (%s)" % printerids, |
---|
| 852 | "DELETE FROM printers WHERE id IN (%s)" % printerids,]) |
---|
[2763] | 853 | |
---|
[2749] | 854 | def deleteManyUserPQuotas(self, printers, users) : |
---|
| 855 | """Deletes many user print quota entries.""" |
---|
| 856 | printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) |
---|
| 857 | userids = ", ".join(["%s" % self.doQuote(u.ident) for u in users]) |
---|
[2771] | 858 | self.multipleQueriesInTransaction([ |
---|
[2749] | 859 | "DELETE FROM jobhistory WHERE userid IN (%s) AND printerid IN (%s)" \ |
---|
| 860 | % (userids, printerids), |
---|
| 861 | "DELETE FROM userpquota WHERE userid IN (%s) AND printerid IN (%s)" \ |
---|
[2763] | 862 | % (userids, printerids),]) |
---|
[2749] | 863 | |
---|
| 864 | def deleteManyGroupPQuotas(self, printers, groups) : |
---|
| 865 | """Deletes many group print quota entries.""" |
---|
| 866 | printerids = ", ".join(["%s" % self.doQuote(p.ident) for p in printers]) |
---|
| 867 | groupids = ", ".join(["%s" % self.doQuote(g.ident) for g in groups]) |
---|
[2771] | 868 | self.multipleQueriesInTransaction([ |
---|
[2749] | 869 | "DELETE FROM grouppquota WHERE groupid IN (%s) AND printerid IN (%s)" \ |
---|
[2763] | 870 | % (groupids, printerids),]) |
---|
[2749] | 871 | |
---|
[2717] | 872 | def deleteUserPQuota(self, upquota) : |
---|
| 873 | """Completely deletes an user print quota entry from the database.""" |
---|
| 874 | for q in [ |
---|
[2718] | 875 | "DELETE FROM jobhistory WHERE userid=%s AND printerid=%s" \ |
---|
| 876 | % (self.doQuote(upquota.User.ident), self.doQuote(upquota.Printer.ident)), |
---|
| 877 | "DELETE FROM userpquota WHERE id=%s" % self.doQuote(upquota.ident), |
---|
[2717] | 878 | ] : |
---|
| 879 | self.doModify(q) |
---|
[1327] | 880 | |
---|
[2717] | 881 | def deleteGroupPQuota(self, gpquota) : |
---|
| 882 | """Completely deletes a group print quota entry from the database.""" |
---|
| 883 | for q in [ |
---|
[2718] | 884 | "DELETE FROM grouppquota WHERE id=%s" % self.doQuote(gpquota.ident), |
---|
[2717] | 885 | ] : |
---|
| 886 | self.doModify(q) |
---|
| 887 | |
---|
[1327] | 888 | def deleteGroup(self, group) : |
---|
[2717] | 889 | """Completely deletes a group from the database.""" |
---|
[1327] | 890 | for q in [ |
---|
| 891 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 892 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 893 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
| 894 | ] : |
---|
| 895 | self.doModify(q) |
---|
[1330] | 896 | |
---|
| 897 | def deletePrinter(self, printer) : |
---|
[2717] | 898 | """Completely deletes a printer from the database.""" |
---|
[1330] | 899 | for q in [ |
---|
| 900 | "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), |
---|
| 901 | "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 902 | "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 903 | "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 904 | "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident), |
---|
| 905 | ] : |
---|
| 906 | self.doModify(q) |
---|
[2342] | 907 | |
---|
| 908 | def deleteBillingCode(self, code) : |
---|
[2717] | 909 | """Completely deletes a billing code from the database.""" |
---|
[2342] | 910 | for q in [ |
---|
| 911 | "DELETE FROM billingcodes WHERE id=%s" % self.doQuote(code.ident), |
---|
| 912 | ] : |
---|
| 913 | self.doModify(q) |
---|
[1327] | 914 | |
---|