- Timestamp:
- 06/19/06 00:17:46 (18 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/sql.py
r2857 r2948 28 28 29 29 class SQLStorage : 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 30 146 def createFilter(self, only) : 31 147 """Returns the appropriate SQL filter.""" … … 198 314 def getUserFromBackend(self, username) : 199 315 """Extracts user information given its name.""" 200 user = StorageUser(self, username) 201 username = self.userCharsetToDatabase(username) 202 result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) 203 if result : 204 fields = result[0] 205 user.ident = fields.get("id") 206 user.LimitBy = fields.get("limitby") or "quota" 207 user.AccountBalance = fields.get("balance") 208 user.LifeTimePaid = fields.get("lifetimepaid") 209 user.Email = fields.get("email") 210 user.Description = self.databaseToUserCharset(fields.get("description")) 211 user.OverCharge = fields.get("overcharge", 1.0) 212 user.Exists = 1 213 return user 316 result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1"\ 317 % self.doQuote(self.userCharsetToDatabase(username))) 318 if result : 319 return self.storageUserFromRecord(username, result[0]) 320 else : 321 return StorageUser(self, username) 214 322 215 323 def getGroupFromBackend(self, groupname) : 216 324 """Extracts group information given its name.""" 217 group = StorageGroup(self, groupname) 218 groupname = self.userCharsetToDatabase(groupname) 219 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" % self.doQuote(groupname)) 220 if result : 221 fields = result[0] 222 group.ident = fields.get("id") 223 group.LimitBy = fields.get("limitby") or "quota" 224 group.AccountBalance = fields.get("balance") 225 group.LifeTimePaid = fields.get("lifetimepaid") 226 group.Description = self.databaseToUserCharset(fields.get("description")) 227 group.Exists = 1 228 return group 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))) 327 if result : 328 return self.storageGroupFromRecord(groupname, result[0]) 329 else : 330 return StorageGroup(self, groupname) 229 331 230 332 def getPrinterFromBackend(self, printername) : 231 333 """Extracts printer information given its name.""" 232 printer = StoragePrinter(self, printername) 233 printername = self.userCharsetToDatabase(printername) 234 result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) 235 if result : 236 fields = result[0] 237 printer.ident = fields.get("id") 238 printer.PricePerJob = fields.get("priceperjob") or 0.0 239 printer.PricePerPage = fields.get("priceperpage") or 0.0 240 printer.MaxJobSize = fields.get("maxjobsize") or 0 241 printer.PassThrough = fields.get("passthrough") or 0 242 if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : 243 printer.PassThrough = 1 244 else : 245 printer.PassThrough = 0 246 printer.Description = self.databaseToUserCharset(fields.get("description") or "") 247 printer.Exists = 1 248 return printer 334 result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" \ 335 % self.doQuote(self.userCharsetToDatabase(printername))) 336 if result : 337 return self.storagePrinterFromRecord(printername, result[0]) 338 else : 339 return StoragePrinter(self, printername) 249 340 250 341 def getBillingCodeFromBackend(self, label) : 251 342 """Extracts a billing code information given its name.""" 252 code = StorageBillingCode(self, label) 253 result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s LIMIT 1" % self.doQuote(self.userCharsetToDatabase(label))) 254 if result : 255 fields = result[0] 256 code.ident = fields.get("id") 257 code.Description = self.databaseToUserCharset(fields.get("description") or "") 258 code.Balance = fields.get("balance") or 0.0 259 code.PageCounter = fields.get("pagecounter") or 0 260 code.Exists = 1 261 return code 343 result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s LIMIT 1" \ 344 % self.doQuote(self.userCharsetToDatabase(label))) 345 if result : 346 return self.storageBillingCodeFromRecord(label, result[0]) 347 else : 348 return StorageBillingCode(self, label) 262 349 263 350 def getUserPQuotaFromBackend(self, user, printer) : 264 351 """Extracts a user print quota.""" 265 userpquota = StorageUserPQuota(self, user, printer)266 352 if printer.Exists and user.Exists : 267 result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(user.ident), self.doQuote(printer.ident))) 353 result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s;" \ 354 % (self.doQuote(user.ident), self.doQuote(printer.ident))) 268 355 if result : 269 fields = result[0] 270 userpquota.ident = fields.get("id") 271 userpquota.PageCounter = fields.get("pagecounter") 272 userpquota.LifePageCounter = fields.get("lifepagecounter") 273 userpquota.SoftLimit = fields.get("softlimit") 274 userpquota.HardLimit = fields.get("hardlimit") 275 userpquota.DateLimit = fields.get("datelimit") 276 userpquota.WarnCount = fields.get("warncount") or 0 277 userpquota.Exists = 1 278 return userpquota 356 return self.storageUserPQuotaFromRecord(user, printer, result[0]) 357 return StorageUserPQuota(self, user, printer) 279 358 280 359 def getGroupPQuotaFromBackend(self, group, printer) : 281 360 """Extracts a group print quota.""" 282 grouppquota = StorageGroupPQuota(self, group, printer)283 if group.Exists :284 result = self.doSearch("SELECT * FROM grouppquota WHERE groupid=%s AND printerid=%s"% (self.doQuote(group.ident), self.doQuote(printer.ident)))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))) 285 364 if result : 286 fields = result[0] 287 grouppquota.ident = fields.get("id") 288 grouppquota.SoftLimit = fields.get("softlimit") 289 grouppquota.HardLimit = fields.get("hardlimit") 290 grouppquota.DateLimit = fields.get("datelimit") 291 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))) 292 if result : 293 fields = result[0] 294 grouppquota.PageCounter = fields.get("pagecounter") or 0 295 grouppquota.LifePageCounter = fields.get("lifepagecounter") or 0 296 grouppquota.Exists = 1 297 return grouppquota 365 return self.storageGroupPQuotaFromRecord(group, printer, result[0]) 366 return StorageGroupPQuota(self, group, printer) 298 367 299 368 def getPrinterLastJobFromBackend(self, printer) : 300 369 """Extracts a printer's last job information.""" 301 lastjob = StorageLastJob(self, printer)302 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)) 303 371 if result : 304 fields = result[0] 305 lastjob.ident = fields.get("id") 306 lastjob.JobId = fields.get("jobid") 307 lastjob.UserName = self.databaseToUserCharset(fields.get("username")) 308 lastjob.PrinterPageCounter = fields.get("pagecounter") 309 lastjob.JobSize = fields.get("jobsize") 310 lastjob.JobPrice = fields.get("jobprice") 311 lastjob.JobAction = fields.get("action") 312 lastjob.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") 313 lastjob.JobTitle = self.databaseToUserCharset(fields.get("title") or "") 314 lastjob.JobCopies = fields.get("copies") 315 lastjob.JobOptions = self.databaseToUserCharset(fields.get("options") or "") 316 lastjob.JobDate = fields.get("jobdate") 317 lastjob.JobHostName = fields.get("hostname") 318 lastjob.JobSizeBytes = fields.get("jobsizebytes") 319 lastjob.JobMD5Sum = fields.get("md5sum") 320 lastjob.JobPages = fields.get("pages") 321 lastjob.JobBillingCode = self.databaseToUserCharset(fields.get("billingcode")) 322 lastjob.PrecomputedJobSize = fields.get("precomputedjobsize") 323 lastjob.PrecomputedJobPrice = fields.get("precomputedjobprice") 324 if lastjob.JobTitle == lastjob.JobFileName == lastjob.JobOptions == "hidden" : 325 (lastjob.JobTitle, lastjob.JobFileName, lastjob.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 326 lastjob.Exists = 1 327 return lastjob 372 return self.storageLastJobFromRecord(printer, result[0]) 373 else : 374 return StorageLastJob(self, printer) 328 375 329 376 def getGroupMembersFromBackend(self, group) : … … 333 380 if result : 334 381 for record in result : 335 user = StorageUser(self, self.databaseToUserCharset(record.get("username"))) 336 user.ident = record.get("userid") 337 user.LimitBy = record.get("limitby") or "quota" 338 user.AccountBalance = record.get("balance") 339 user.LifeTimePaid = record.get("lifetimepaid") 340 user.Email = record.get("email") 341 user.OverCharge = record.get("overcharge") 342 user.Exists = 1 382 user = self.storageUserFromRecord(self.databaseToUserCharset(record.get("username")), \ 383 record) 343 384 groupmembers.append(user) 344 385 self.cacheEntry("USERS", user.Name, user) … … 385 426 pname = self.databaseToUserCharset(record["printername"]) 386 427 if patdict.has_key(pname) or self.tool.matchString(pname, patterns) : 387 printer = StoragePrinter(self, pname) 388 printer.ident = record.get("id") 389 printer.PricePerJob = record.get("priceperjob") or 0.0 390 printer.PricePerPage = record.get("priceperpage") or 0.0 391 printer.Description = self.databaseToUserCharset(record.get("description") or "") 392 printer.MaxJobSize = record.get("maxjobsize") or 0 393 printer.PassThrough = record.get("passthrough") or 0 394 if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : 395 printer.PassThrough = 1 396 else : 397 printer.PassThrough = 0 398 printer.Exists = 1 428 printer = self.storagePrinterFromRecord(pname, record) 399 429 printers.append(printer) 400 430 self.cacheEntry("PRINTERS", printer.Name, printer) … … 420 450 uname = self.databaseToUserCharset(record["username"]) 421 451 if patdict.has_key(uname) or self.tool.matchString(uname, patterns) : 422 user = StorageUser(self, uname) 423 user.ident = record.get("id") 424 user.LimitBy = record.get("limitby") or "quota" 425 user.AccountBalance = record.get("balance") 426 user.LifeTimePaid = record.get("lifetimepaid") 427 user.Email = record.get("email") 428 user.Description = self.databaseToUserCharset(record.get("description")) 429 user.OverCharge = record.get("overcharge", 1.0) 430 user.Exists = 1 452 user = self.storageUserFromRecord(uname, record) 431 453 users.append(user) 432 454 self.cacheEntry("USERS", user.Name, user) … … 452 474 gname = self.databaseToUserCharset(record["groupname"]) 453 475 if patdict.has_key(gname) or self.tool.matchString(gname, patterns) : 454 group = StorageGroup(self, gname) 455 group.ident = record.get("id") 456 group.LimitBy = record.get("limitby") or "quota" 457 group.AccountBalance = record.get("balance") 458 group.LifeTimePaid = record.get("lifetimepaid") 459 group.Description = self.databaseToUserCharset(record.get("description")) 460 group.Exists = 1 476 group = self.storageGroupFromRecord(gname, record) 461 477 groups.append(group) 462 478 self.cacheEntry("GROUPS", group.Name, group) … … 479 495 codename = self.databaseToUserCharset(record["billingcode"]) 480 496 if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : 481 code = StorageBillingCode(self, codename) 482 code.ident = record.get("id") 483 code.Balance = record.get("balance") or 0.0 484 code.PageCounter = record.get("pagecounter") or 0 485 code.Description = self.databaseToUserCharset(record.get("description") or "") 486 code.Exists = 1 497 code = self.storageBillingCodeFromRecord(codename, record) 487 498 codes.append(code) 488 499 self.cacheEntry("BILLINGCODES", code.BillingCode, code) … … 497 508 uname = self.databaseToUserCharset(record.get("username")) 498 509 if self.tool.matchString(uname, names) : 499 user = StorageUser(self, uname) 500 user.ident = record.get("uid") 501 user.LimitBy = record.get("limitby") or "quota" 502 user.AccountBalance = record.get("balance") 503 user.LifeTimePaid = record.get("lifetimepaid") 504 user.Email = record.get("email") 505 user.OverCharge = record.get("overcharge") 506 user.Description = self.databaseToUserCharset(record.get("description")) 507 user.Exists = 1 508 userpquota = StorageUserPQuota(self, user, printer) 509 userpquota.ident = record.get("id") 510 userpquota.PageCounter = record.get("pagecounter") 511 userpquota.LifePageCounter = record.get("lifepagecounter") 512 userpquota.SoftLimit = record.get("softlimit") 513 userpquota.HardLimit = record.get("hardlimit") 514 userpquota.DateLimit = record.get("datelimit") 515 userpquota.WarnCount = record.get("warncount") or 0 516 userpquota.Exists = 1 510 user = self.storageUserFromRecord(uname, record) 511 userpquota = self.storageUserPQuotaFromRecord(user, printer, record) 517 512 usersandquotas.append((user, userpquota)) 518 513 self.cacheEntry("USERS", user.Name, user) … … 793 788 if result : 794 789 for fields in result : 795 job = StorageJob(self) 796 job.ident = fields.get("id") 797 job.JobId = fields.get("jobid") 798 job.PrinterPageCounter = fields.get("pagecounter") 799 job.JobSize = fields.get("jobsize") 800 job.JobPrice = fields.get("jobprice") 801 job.JobAction = fields.get("action") 802 job.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") 803 job.JobTitle = self.databaseToUserCharset(fields.get("title") or "") 804 job.JobCopies = fields.get("copies") 805 job.JobOptions = self.databaseToUserCharset(fields.get("options") or "") 806 job.JobDate = fields.get("jobdate") 807 job.JobHostName = fields.get("hostname") 808 job.JobSizeBytes = fields.get("jobsizebytes") 809 job.JobMD5Sum = fields.get("md5sum") 810 job.JobPages = fields.get("pages") 811 job.JobBillingCode = self.databaseToUserCharset(fields.get("billingcode") or "") 812 job.PrecomputedJobSize = fields.get("precomputedjobsize") 813 job.PrecomputedJobPrice = fields.get("precomputedjobprice") 814 job.UserName = self.databaseToUserCharset(fields.get("username")) 815 job.PrinterName = self.databaseToUserCharset(fields.get("printername")) 816 if job.JobTitle == job.JobFileName == job.JobOptions == "hidden" : 817 (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 818 job.Exists = 1 790 job = storageJobFromRecord(fields) 819 791 jobs.append(job) 820 792 return jobs