[1327] | 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 | # |
---|
[2066] | 23 | # |
---|
[1327] | 24 | |
---|
[1762] | 25 | from types import StringType |
---|
[1327] | 26 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageJob,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
| 27 | |
---|
| 28 | class SQLStorage : |
---|
[1717] | 29 | def prepareRawResult(self, result) : |
---|
| 30 | """Prepares a raw result by including the headers.""" |
---|
| 31 | if result.ntuples() > 0 : |
---|
| 32 | entries = [result.listfields()] |
---|
| 33 | entries.extend(result.getresult()) |
---|
[1773] | 34 | nbfields = len(entries[0]) |
---|
| 35 | for i in range(1, len(entries)) : |
---|
| 36 | fields = list(entries[i]) |
---|
| 37 | for j in range(nbfields) : |
---|
| 38 | field = fields[j] |
---|
| 39 | if type(field) == StringType : |
---|
[1790] | 40 | fields[j] = self.databaseToUserCharset(field) |
---|
[1773] | 41 | entries[i] = tuple(fields) |
---|
[1717] | 42 | return entries |
---|
| 43 | |
---|
[1991] | 44 | def createFilter(self, only) : |
---|
| 45 | """Returns the appropriate SQL filter.""" |
---|
| 46 | if only : |
---|
| 47 | expressions = [] |
---|
| 48 | for (k, v) in only.items() : |
---|
| 49 | expressions.append("%s=%s" % (k, self.doQuote(v))) |
---|
| 50 | return " AND ".join(expressions) |
---|
| 51 | return "" |
---|
| 52 | |
---|
[1990] | 53 | def extractPrinters(self, extractonly={}) : |
---|
[1717] | 54 | """Extracts all printer records.""" |
---|
[1991] | 55 | thefilter = self.createFilter(extractonly) |
---|
| 56 | if thefilter : |
---|
| 57 | thefilter = "WHERE %s" % thefilter |
---|
| 58 | result = self.doRawSearch("SELECT * FROM printers %s ORDER BY id ASC" % thefilter) |
---|
[1717] | 59 | return self.prepareRawResult(result) |
---|
| 60 | |
---|
[1990] | 61 | def extractUsers(self, extractonly={}) : |
---|
[1717] | 62 | """Extracts all user records.""" |
---|
[1991] | 63 | thefilter = self.createFilter(extractonly) |
---|
| 64 | if thefilter : |
---|
| 65 | thefilter = "WHERE %s" % thefilter |
---|
| 66 | result = self.doRawSearch("SELECT * FROM users %s ORDER BY id ASC" % thefilter) |
---|
[1717] | 67 | return self.prepareRawResult(result) |
---|
| 68 | |
---|
[1990] | 69 | def extractGroups(self, extractonly={}) : |
---|
[1717] | 70 | """Extracts all group records.""" |
---|
[1991] | 71 | thefilter = self.createFilter(extractonly) |
---|
| 72 | if thefilter : |
---|
| 73 | thefilter = "WHERE %s" % thefilter |
---|
| 74 | 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) |
---|
[1717] | 75 | return self.prepareRawResult(result) |
---|
| 76 | |
---|
[1990] | 77 | def extractPayments(self, extractonly={}) : |
---|
[1717] | 78 | """Extracts all payment records.""" |
---|
[1991] | 79 | thefilter = self.createFilter(extractonly) |
---|
| 80 | if thefilter : |
---|
| 81 | thefilter = "AND %s" % thefilter |
---|
| 82 | result = self.doRawSearch("SELECT username,payments.* FROM users,payments WHERE users.id=payments.userid %s ORDER BY payments.id ASC" % thefilter) |
---|
[1717] | 83 | return self.prepareRawResult(result) |
---|
| 84 | |
---|
[1990] | 85 | def extractUpquotas(self, extractonly={}) : |
---|
[1717] | 86 | """Extracts all userpquota records.""" |
---|
[1991] | 87 | thefilter = self.createFilter(extractonly) |
---|
| 88 | if thefilter : |
---|
| 89 | thefilter = "AND %s" % thefilter |
---|
| 90 | 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] | 91 | return self.prepareRawResult(result) |
---|
| 92 | |
---|
[1990] | 93 | def extractGpquotas(self, extractonly={}) : |
---|
[1717] | 94 | """Extracts all grouppquota records.""" |
---|
[1991] | 95 | thefilter = self.createFilter(extractonly) |
---|
| 96 | if thefilter : |
---|
| 97 | thefilter = "AND %s" % thefilter |
---|
| 98 | 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) |
---|
[1717] | 99 | return self.prepareRawResult(result) |
---|
| 100 | |
---|
[1990] | 101 | def extractUmembers(self, extractonly={}) : |
---|
[1718] | 102 | """Extracts all user groups members.""" |
---|
[1991] | 103 | thefilter = self.createFilter(extractonly) |
---|
| 104 | if thefilter : |
---|
| 105 | thefilter = "AND %s" % thefilter |
---|
| 106 | 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] | 107 | return self.prepareRawResult(result) |
---|
| 108 | |
---|
[1990] | 109 | def extractPmembers(self, extractonly={}) : |
---|
[1718] | 110 | """Extracts all printer groups members.""" |
---|
[1992] | 111 | for (k, v) in extractonly.items() : |
---|
| 112 | if k == "pgroupname" : |
---|
| 113 | del extractonly[k] |
---|
| 114 | extractonly["p1.printername"] = v |
---|
| 115 | elif k == "printername" : |
---|
| 116 | del extractonly[k] |
---|
| 117 | extractonly["p2.printername"] = v |
---|
[1991] | 118 | thefilter = self.createFilter(extractonly) |
---|
| 119 | if thefilter : |
---|
| 120 | thefilter = "AND %s" % thefilter |
---|
| 121 | 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] | 122 | return self.prepareRawResult(result) |
---|
| 123 | |
---|
[1990] | 124 | def extractHistory(self, extractonly={}) : |
---|
[1717] | 125 | """Extracts all jobhistory records.""" |
---|
[1991] | 126 | thefilter = self.createFilter(extractonly) |
---|
| 127 | if thefilter : |
---|
| 128 | thefilter = "AND %s" % thefilter |
---|
| 129 | 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] | 130 | return self.prepareRawResult(result) |
---|
| 131 | |
---|
[1327] | 132 | def getAllUsersNames(self) : |
---|
| 133 | """Extracts all user names.""" |
---|
| 134 | usernames = [] |
---|
| 135 | result = self.doSearch("SELECT username FROM users") |
---|
| 136 | if result : |
---|
| 137 | usernames = [record["username"] for record in result] |
---|
| 138 | return usernames |
---|
| 139 | |
---|
| 140 | def getAllGroupsNames(self) : |
---|
| 141 | """Extracts all group names.""" |
---|
| 142 | groupnames = [] |
---|
| 143 | result = self.doSearch("SELECT groupname FROM groups") |
---|
| 144 | if result : |
---|
| 145 | groupnames = [record["groupname"] for record in result] |
---|
| 146 | return groupnames |
---|
| 147 | |
---|
[1806] | 148 | def getUserNbJobsFromHistory(self, user) : |
---|
| 149 | """Returns the number of jobs the user has in history.""" |
---|
| 150 | result = self.doSearch("SELECT COUNT(*) FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 151 | if result : |
---|
| 152 | return result[0]["count"] |
---|
| 153 | return 0 |
---|
| 154 | |
---|
[1327] | 155 | def getUserFromBackend(self, username) : |
---|
| 156 | """Extracts user information given its name.""" |
---|
| 157 | user = StorageUser(self, username) |
---|
| 158 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) |
---|
| 159 | if result : |
---|
| 160 | fields = result[0] |
---|
| 161 | user.ident = fields.get("id") |
---|
[1451] | 162 | user.Name = fields.get("username", username) |
---|
[1999] | 163 | user.LimitBy = fields.get("limitby") or "quota" |
---|
[1327] | 164 | user.AccountBalance = fields.get("balance") |
---|
| 165 | user.LifeTimePaid = fields.get("lifetimepaid") |
---|
| 166 | user.Email = fields.get("email") |
---|
[2054] | 167 | user.OverCharge = fields.get("overcharge", 1.0) |
---|
[1327] | 168 | user.Exists = 1 |
---|
| 169 | return user |
---|
| 170 | |
---|
| 171 | def getGroupFromBackend(self, groupname) : |
---|
| 172 | """Extracts group information given its name.""" |
---|
| 173 | group = StorageGroup(self, groupname) |
---|
[1792] | 174 | 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)) |
---|
[1327] | 175 | if result : |
---|
| 176 | fields = result[0] |
---|
| 177 | group.ident = fields.get("id") |
---|
[1451] | 178 | group.Name = fields.get("groupname", groupname) |
---|
[1999] | 179 | group.LimitBy = fields.get("limitby") or "quota" |
---|
[1792] | 180 | group.AccountBalance = fields.get("balance") |
---|
| 181 | group.LifeTimePaid = fields.get("lifetimepaid") |
---|
[1327] | 182 | group.Exists = 1 |
---|
| 183 | return group |
---|
| 184 | |
---|
| 185 | def getPrinterFromBackend(self, printername) : |
---|
| 186 | """Extracts printer information given its name.""" |
---|
| 187 | printer = StoragePrinter(self, printername) |
---|
| 188 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) |
---|
| 189 | if result : |
---|
| 190 | fields = result[0] |
---|
| 191 | printer.ident = fields.get("id") |
---|
[1451] | 192 | printer.Name = fields.get("printername", printername) |
---|
[1582] | 193 | printer.PricePerJob = fields.get("priceperjob") or 0.0 |
---|
| 194 | printer.PricePerPage = fields.get("priceperpage") or 0.0 |
---|
[1790] | 195 | printer.Description = self.databaseToUserCharset(fields.get("description") or "") |
---|
[1327] | 196 | printer.Exists = 1 |
---|
| 197 | return printer |
---|
| 198 | |
---|
| 199 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
| 200 | """Extracts a user print quota.""" |
---|
| 201 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 202 | if printer.Exists and user.Exists : |
---|
[2066] | 203 | result = self.doSearch("SELECT * FROM userpquota WHERE userid=%s AND printerid=%s" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
[1327] | 204 | if result : |
---|
| 205 | fields = result[0] |
---|
| 206 | userpquota.ident = fields.get("id") |
---|
| 207 | userpquota.PageCounter = fields.get("pagecounter") |
---|
| 208 | userpquota.LifePageCounter = fields.get("lifepagecounter") |
---|
| 209 | userpquota.SoftLimit = fields.get("softlimit") |
---|
| 210 | userpquota.HardLimit = fields.get("hardlimit") |
---|
| 211 | userpquota.DateLimit = fields.get("datelimit") |
---|
[2054] | 212 | userpquota.WarnCount = fields.get("warncount") |
---|
[1327] | 213 | userpquota.Exists = 1 |
---|
| 214 | return userpquota |
---|
| 215 | |
---|
| 216 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
| 217 | """Extracts a group print quota.""" |
---|
| 218 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 219 | if group.Exists : |
---|
[2066] | 220 | result = self.doSearch("SELECT * FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
[1327] | 221 | if result : |
---|
| 222 | fields = result[0] |
---|
| 223 | grouppquota.ident = fields.get("id") |
---|
| 224 | grouppquota.SoftLimit = fields.get("softlimit") |
---|
| 225 | grouppquota.HardLimit = fields.get("hardlimit") |
---|
| 226 | grouppquota.DateLimit = fields.get("datelimit") |
---|
[1787] | 227 | 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))) |
---|
| 228 | if result : |
---|
| 229 | fields = result[0] |
---|
| 230 | grouppquota.PageCounter = fields.get("pagecounter") or 0 |
---|
| 231 | grouppquota.LifePageCounter = fields.get("lifepagecounter") or 0 |
---|
[1327] | 232 | grouppquota.Exists = 1 |
---|
| 233 | return grouppquota |
---|
| 234 | |
---|
| 235 | def getPrinterLastJobFromBackend(self, printer) : |
---|
| 236 | """Extracts a printer's last job information.""" |
---|
| 237 | lastjob = StorageLastJob(self, printer) |
---|
[2066] | 238 | 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)) |
---|
[1327] | 239 | if result : |
---|
| 240 | fields = result[0] |
---|
| 241 | lastjob.ident = fields.get("id") |
---|
| 242 | lastjob.JobId = fields.get("jobid") |
---|
[1358] | 243 | lastjob.UserName = fields.get("username") |
---|
[1327] | 244 | lastjob.PrinterPageCounter = fields.get("pagecounter") |
---|
| 245 | lastjob.JobSize = fields.get("jobsize") |
---|
| 246 | lastjob.JobPrice = fields.get("jobprice") |
---|
| 247 | lastjob.JobAction = fields.get("action") |
---|
[1790] | 248 | lastjob.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") |
---|
| 249 | lastjob.JobTitle = self.databaseToUserCharset(fields.get("title") or "") |
---|
[1327] | 250 | lastjob.JobCopies = fields.get("copies") |
---|
[1790] | 251 | lastjob.JobOptions = self.databaseToUserCharset(fields.get("options") or "") |
---|
[1327] | 252 | lastjob.JobDate = fields.get("jobdate") |
---|
[1502] | 253 | lastjob.JobHostName = fields.get("hostname") |
---|
[1520] | 254 | lastjob.JobSizeBytes = fields.get("jobsizebytes") |
---|
[2054] | 255 | lastjob.JobMD5Sum = fields.get("md5sum") |
---|
| 256 | lastjob.JobPages = fields.get("pages") |
---|
| 257 | lastjob.JobBillingCode = fields.get("billingcode") |
---|
[1327] | 258 | lastjob.Exists = 1 |
---|
| 259 | return lastjob |
---|
| 260 | |
---|
| 261 | def getGroupMembersFromBackend(self, group) : |
---|
| 262 | """Returns the group's members list.""" |
---|
| 263 | groupmembers = [] |
---|
| 264 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
| 265 | if result : |
---|
| 266 | for record in result : |
---|
| 267 | user = StorageUser(self, record.get("username")) |
---|
| 268 | user.ident = record.get("userid") |
---|
[1999] | 269 | user.LimitBy = record.get("limitby") or "quota" |
---|
[1327] | 270 | user.AccountBalance = record.get("balance") |
---|
| 271 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 272 | user.Email = record.get("email") |
---|
[2054] | 273 | user.OverCharge = record.get("overcharge") |
---|
[1327] | 274 | user.Exists = 1 |
---|
| 275 | groupmembers.append(user) |
---|
| 276 | self.cacheEntry("USERS", user.Name, user) |
---|
| 277 | return groupmembers |
---|
| 278 | |
---|
| 279 | def getUserGroupsFromBackend(self, user) : |
---|
| 280 | """Returns the user's groups list.""" |
---|
| 281 | groups = [] |
---|
| 282 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 283 | if result : |
---|
| 284 | for record in result : |
---|
| 285 | groups.append(self.getGroup(record.get("groupname"))) |
---|
| 286 | return groups |
---|
| 287 | |
---|
| 288 | def getParentPrintersFromBackend(self, printer) : |
---|
| 289 | """Get all the printer groups this printer is a member of.""" |
---|
| 290 | pgroups = [] |
---|
| 291 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s" % self.doQuote(printer.ident)) |
---|
| 292 | if result : |
---|
| 293 | for record in result : |
---|
| 294 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
| 295 | parentprinter = self.getPrinter(record.get("printername")) |
---|
| 296 | if parentprinter.Exists : |
---|
| 297 | pgroups.append(parentprinter) |
---|
| 298 | return pgroups |
---|
| 299 | |
---|
| 300 | def getMatchingPrinters(self, printerpattern) : |
---|
| 301 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 302 | printers = [] |
---|
| 303 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
| 304 | # but we don't because other storages semantics may be different, so every |
---|
| 305 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
| 306 | result = self.doSearch("SELECT * FROM printers") |
---|
| 307 | if result : |
---|
| 308 | for record in result : |
---|
| 309 | if self.tool.matchString(record["printername"], printerpattern.split(",")) : |
---|
| 310 | printer = StoragePrinter(self, record["printername"]) |
---|
| 311 | printer.ident = record.get("id") |
---|
[1582] | 312 | printer.PricePerJob = record.get("priceperjob") or 0.0 |
---|
| 313 | printer.PricePerPage = record.get("priceperpage") or 0.0 |
---|
[1790] | 314 | printer.Description = self.databaseToUserCharset(record.get("description") or "") |
---|
[1327] | 315 | printer.Exists = 1 |
---|
| 316 | printers.append(printer) |
---|
| 317 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
| 318 | return printers |
---|
| 319 | |
---|
| 320 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
| 321 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 322 | usersandquotas = [] |
---|
[2054] | 323 | 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)) |
---|
[1327] | 324 | if result : |
---|
| 325 | for record in result : |
---|
| 326 | if self.tool.matchString(record.get("username"), names) : |
---|
| 327 | user = StorageUser(self, record.get("username")) |
---|
| 328 | user.ident = record.get("uid") |
---|
[1999] | 329 | user.LimitBy = record.get("limitby") or "quota" |
---|
[1327] | 330 | user.AccountBalance = record.get("balance") |
---|
| 331 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 332 | user.Email = record.get("email") |
---|
[2054] | 333 | user.OverCharge = record.get("overcharge") |
---|
[1327] | 334 | user.Exists = 1 |
---|
| 335 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 336 | userpquota.ident = record.get("id") |
---|
| 337 | userpquota.PageCounter = record.get("pagecounter") |
---|
| 338 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
| 339 | userpquota.SoftLimit = record.get("softlimit") |
---|
| 340 | userpquota.HardLimit = record.get("hardlimit") |
---|
| 341 | userpquota.DateLimit = record.get("datelimit") |
---|
[2054] | 342 | userpquota.WarnCount = record.get("warncount") |
---|
[1327] | 343 | userpquota.Exists = 1 |
---|
| 344 | usersandquotas.append((user, userpquota)) |
---|
| 345 | self.cacheEntry("USERS", user.Name, user) |
---|
| 346 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
| 347 | return usersandquotas |
---|
| 348 | |
---|
| 349 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
| 350 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 351 | groupsandquotas = [] |
---|
| 352 | 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)) |
---|
| 353 | if result : |
---|
| 354 | for record in result : |
---|
| 355 | if self.tool.matchString(record.get("groupname"), names) : |
---|
| 356 | group = self.getGroup(record.get("groupname")) |
---|
| 357 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 358 | groupsandquotas.append((group, grouppquota)) |
---|
| 359 | return groupsandquotas |
---|
| 360 | |
---|
| 361 | def addPrinter(self, printername) : |
---|
| 362 | """Adds a printer to the quota storage, returns it.""" |
---|
| 363 | self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
| 364 | return self.getPrinter(printername) |
---|
| 365 | |
---|
| 366 | def addUser(self, user) : |
---|
| 367 | """Adds a user to the quota storage, returns its id.""" |
---|
[2054] | 368 | 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))) |
---|
[1327] | 369 | return self.getUser(user.Name) |
---|
| 370 | |
---|
| 371 | def addGroup(self, group) : |
---|
| 372 | """Adds a group to the quota storage, returns its id.""" |
---|
[1711] | 373 | self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy or "quota"))) |
---|
[1327] | 374 | return self.getGroup(group.Name) |
---|
| 375 | |
---|
| 376 | def addUserToGroup(self, user, group) : |
---|
| 377 | """Adds an user to a group.""" |
---|
| 378 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 379 | try : |
---|
| 380 | mexists = int(result[0].get("mexists")) |
---|
| 381 | except (IndexError, TypeError) : |
---|
| 382 | mexists = 0 |
---|
| 383 | if not mexists : |
---|
| 384 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 385 | |
---|
| 386 | def addUserPQuota(self, user, printer) : |
---|
| 387 | """Initializes a user print quota on a printer.""" |
---|
| 388 | self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
| 389 | return self.getUserPQuota(user, printer) |
---|
| 390 | |
---|
| 391 | def addGroupPQuota(self, group, printer) : |
---|
| 392 | """Initializes a group print quota on a printer.""" |
---|
| 393 | self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
| 394 | return self.getGroupPQuota(group, printer) |
---|
| 395 | |
---|
| 396 | def writePrinterPrices(self, printer) : |
---|
| 397 | """Write the printer's prices back into the storage.""" |
---|
| 398 | self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident))) |
---|
| 399 | |
---|
[1582] | 400 | def writePrinterDescription(self, printer) : |
---|
| 401 | """Write the printer's description back into the storage.""" |
---|
[1790] | 402 | description = self.userCharsetToDatabase(printer.Description) |
---|
[1761] | 403 | self.doModify("UPDATE printers SET description=%s WHERE id=%s" % (self.doQuote(description), self.doQuote(printer.ident))) |
---|
[1582] | 404 | |
---|
[2054] | 405 | def writeUserOverCharge(self, user, factor) : |
---|
| 406 | """Sets the user's overcharging coefficient.""" |
---|
| 407 | self.doModify("UPDATE users SET overcharge=%s WHERE id=%s" % (self.doQuote(factor), self.doQuote(user.ident))) |
---|
| 408 | |
---|
[1327] | 409 | def writeUserLimitBy(self, user, limitby) : |
---|
| 410 | """Sets the user's limiting factor.""" |
---|
| 411 | self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident))) |
---|
| 412 | |
---|
| 413 | def writeGroupLimitBy(self, group, limitby) : |
---|
| 414 | """Sets the group's limiting factor.""" |
---|
| 415 | self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident))) |
---|
| 416 | |
---|
| 417 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 418 | """Sets the date limit permanently for a user print quota.""" |
---|
| 419 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
| 420 | |
---|
| 421 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 422 | """Sets the date limit permanently for a group print quota.""" |
---|
| 423 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
| 424 | |
---|
| 425 | def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : |
---|
| 426 | """Increase page counters for a user print quota.""" |
---|
[2054] | 427 | 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] | 428 | |
---|
| 429 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
| 430 | """Sets the new page counters permanently for a user print quota.""" |
---|
[2054] | 431 | 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))) |
---|
[1327] | 432 | |
---|
| 433 | def decreaseUserAccountBalance(self, user, amount) : |
---|
| 434 | """Decreases user's account balance from an amount.""" |
---|
[2054] | 435 | self.doModify("UPDATE users SET balance=balance - %s WHERE id=%s" % (self.doQuote(amount), self.doQuote(user.ident))) |
---|
[1327] | 436 | |
---|
| 437 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
| 438 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
| 439 | if newlifetimepaid is not None : |
---|
| 440 | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
---|
| 441 | else : |
---|
| 442 | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
---|
| 443 | |
---|
[1522] | 444 | def writeNewPayment(self, user, amount) : |
---|
| 445 | """Adds a new payment to the payments history.""" |
---|
| 446 | self.doModify("INSERT INTO payments (userid, amount) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(amount))) |
---|
| 447 | |
---|
[1327] | 448 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
| 449 | """Sets the last job's size permanently.""" |
---|
| 450 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
| 451 | |
---|
[2057] | 452 | 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) : |
---|
[1327] | 453 | """Adds a job in a printer's history.""" |
---|
[1875] | 454 | if self.privacy : |
---|
| 455 | # For legal reasons, we want to hide the title, filename and options |
---|
| 456 | title = filename = options = "Hidden because of privacy concerns" |
---|
[1790] | 457 | filename = self.userCharsetToDatabase(filename) |
---|
| 458 | title = self.userCharsetToDatabase(title) |
---|
| 459 | options = self.userCharsetToDatabase(options) |
---|
[1327] | 460 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 461 | if jobsize is not None : |
---|
[2057] | 462 | 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))) |
---|
[1327] | 463 | else : |
---|
[2057] | 464 | 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))) |
---|
[1327] | 465 | else : |
---|
| 466 | # here we explicitly want to reset jobsize to NULL if needed |
---|
[2057] | 467 | 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))) |
---|
[1327] | 468 | |
---|
| 469 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
| 470 | """Sets soft and hard limits for a user quota.""" |
---|
[2054] | 471 | 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))) |
---|
[1327] | 472 | |
---|
[2054] | 473 | def writeUserPQuotaWarnCount(self, userpquota, warncount) : |
---|
| 474 | """Sets the warn counter value for a user quota.""" |
---|
| 475 | self.doModify("UPDATE userpquota SET warncount=%s WHERE id=%s" % (self.doQuote(warncount), self.doQuote(userpquota.ident))) |
---|
| 476 | |
---|
| 477 | def increaseUserPQuotaWarnCount(self, userpquota) : |
---|
| 478 | """Increases the warn counter value for a user quota.""" |
---|
| 479 | self.doModify("UPDATE userpquota SET warncount=warncount+1 WHERE id=%s" % self.doQuote(userpquota.ident)) |
---|
| 480 | |
---|
[1327] | 481 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
| 482 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
| 483 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident))) |
---|
| 484 | |
---|
| 485 | def writePrinterToGroup(self, pgroup, printer) : |
---|
| 486 | """Puts a printer into a printer group.""" |
---|
| 487 | children = [] |
---|
| 488 | result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s" % self.doQuote(pgroup.ident)) |
---|
| 489 | if result : |
---|
| 490 | for record in result : |
---|
| 491 | children.append(record.get("printerid")) # TODO : put this into the database integrity rules |
---|
| 492 | if printer.ident not in children : |
---|
| 493 | self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 494 | |
---|
[1332] | 495 | def removePrinterFromGroup(self, pgroup, printer) : |
---|
| 496 | """Removes a printer from a printer group.""" |
---|
| 497 | self.doModify("DELETE FROM printergroupsmembers WHERE groupid=%s AND printerid=%s" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
| 498 | |
---|
[1502] | 499 | def retrieveHistory(self, user=None, printer=None, datelimit=None, hostname=None, limit=100) : |
---|
[1327] | 500 | """Retrieves all print jobs for user on printer (or all) before date, limited to first 100 results.""" |
---|
| 501 | query = "SELECT jobhistory.*,username,printername FROM jobhistory,users,printers WHERE users.id=userid AND printers.id=printerid" |
---|
| 502 | where = [] |
---|
| 503 | if (user is not None) and user.Exists : |
---|
| 504 | where.append("userid=%s" % self.doQuote(user.ident)) |
---|
| 505 | if (printer is not None) and printer.Exists : |
---|
| 506 | where.append("printerid=%s" % self.doQuote(printer.ident)) |
---|
[1502] | 507 | if hostname is not None : |
---|
| 508 | where.append("hostname=%s" % self.doQuote(hostname)) |
---|
[1327] | 509 | if datelimit is not None : |
---|
| 510 | where.append("jobdate<=%s" % self.doQuote(datelimit)) |
---|
| 511 | if where : |
---|
| 512 | query += " AND %s" % " AND ".join(where) |
---|
| 513 | query += " ORDER BY id DESC" |
---|
| 514 | if limit : |
---|
| 515 | query += " LIMIT %s" % self.doQuote(int(limit)) |
---|
| 516 | jobs = [] |
---|
| 517 | result = self.doSearch(query) |
---|
| 518 | if result : |
---|
| 519 | for fields in result : |
---|
| 520 | job = StorageJob(self) |
---|
| 521 | job.ident = fields.get("id") |
---|
| 522 | job.JobId = fields.get("jobid") |
---|
| 523 | job.PrinterPageCounter = fields.get("pagecounter") |
---|
| 524 | job.JobSize = fields.get("jobsize") |
---|
| 525 | job.JobPrice = fields.get("jobprice") |
---|
| 526 | job.JobAction = fields.get("action") |
---|
[1790] | 527 | job.JobFileName = self.databaseToUserCharset(fields.get("filename") or "") |
---|
| 528 | job.JobTitle = self.databaseToUserCharset(fields.get("title") or "") |
---|
[1327] | 529 | job.JobCopies = fields.get("copies") |
---|
[1790] | 530 | job.JobOptions = self.databaseToUserCharset(fields.get("options") or "") |
---|
[1327] | 531 | job.JobDate = fields.get("jobdate") |
---|
[1502] | 532 | job.JobHostName = fields.get("hostname") |
---|
[1520] | 533 | job.JobSizeBytes = fields.get("jobsizebytes") |
---|
[2054] | 534 | job.JobMD5Sum = fields.get("md5sum") |
---|
| 535 | job.JobPages = fields.get("pages") |
---|
| 536 | job.JobBillingCode = fields.get("billingcode") |
---|
[1358] | 537 | job.UserName = fields.get("username") |
---|
| 538 | job.PrinterName = fields.get("printername") |
---|
[1327] | 539 | job.Exists = 1 |
---|
| 540 | jobs.append(job) |
---|
| 541 | return jobs |
---|
| 542 | |
---|
| 543 | def deleteUser(self, user) : |
---|
| 544 | """Completely deletes an user from the Quota Storage.""" |
---|
| 545 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
| 546 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 547 | # TODO : incorrectly charged (overcharged). |
---|
| 548 | for q in [ |
---|
[1531] | 549 | "DELETE FROM payments WHERE userid=%s" % self.doQuote(user.ident), |
---|
[1327] | 550 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 551 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 552 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 553 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
| 554 | ] : |
---|
| 555 | self.doModify(q) |
---|
| 556 | |
---|
| 557 | def deleteGroup(self, group) : |
---|
| 558 | """Completely deletes a group from the Quota Storage.""" |
---|
| 559 | for q in [ |
---|
| 560 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 561 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 562 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
| 563 | ] : |
---|
| 564 | self.doModify(q) |
---|
[1330] | 565 | |
---|
| 566 | def deletePrinter(self, printer) : |
---|
| 567 | """Completely deletes a printer from the Quota Storage.""" |
---|
| 568 | for q in [ |
---|
| 569 | "DELETE FROM printergroupsmembers WHERE groupid=%s OR printerid=%s" % (self.doQuote(printer.ident), self.doQuote(printer.ident)), |
---|
| 570 | "DELETE FROM jobhistory WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 571 | "DELETE FROM grouppquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 572 | "DELETE FROM userpquota WHERE printerid=%s" % self.doQuote(printer.ident), |
---|
| 573 | "DELETE FROM printers WHERE id=%s" % self.doQuote(printer.ident), |
---|
| 574 | ] : |
---|
| 575 | self.doModify(q) |
---|
[1327] | 576 | |
---|