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