[1021] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1021] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[1021] | 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$ |
---|
[1259] | 24 | # Revision 1.30 2004/01/08 16:33:27 jalet |
---|
| 25 | # Additionnal check to not create a circular printers group. |
---|
| 26 | # |
---|
[1258] | 27 | # Revision 1.29 2004/01/08 16:24:49 jalet |
---|
| 28 | # edpykota now supports adding printers to printer groups. |
---|
| 29 | # |
---|
[1257] | 30 | # Revision 1.28 2004/01/08 14:10:33 jalet |
---|
| 31 | # Copyright year changed. |
---|
| 32 | # |
---|
[1249] | 33 | # Revision 1.27 2004/01/06 14:24:59 jalet |
---|
| 34 | # Printer groups should be cached now, if caching is enabled. |
---|
| 35 | # |
---|
[1244] | 36 | # Revision 1.26 2003/12/29 14:12:48 uid67467 |
---|
| 37 | # Tries to workaround possible integrity violations when retrieving printer groups |
---|
| 38 | # |
---|
[1240] | 39 | # Revision 1.25 2003/12/27 16:49:25 uid67467 |
---|
| 40 | # Should be ok now. |
---|
| 41 | # |
---|
[1228] | 42 | # Revision 1.24 2003/11/29 22:02:14 jalet |
---|
| 43 | # Don't try to retrieve the user print quota information if current printer |
---|
| 44 | # doesn't exist. |
---|
| 45 | # |
---|
[1203] | 46 | # Revision 1.23 2003/11/23 19:01:37 jalet |
---|
| 47 | # Job price added to history |
---|
| 48 | # |
---|
[1200] | 49 | # Revision 1.22 2003/11/21 14:28:46 jalet |
---|
| 50 | # More complete job history. |
---|
| 51 | # |
---|
[1179] | 52 | # Revision 1.21 2003/11/12 13:06:38 jalet |
---|
| 53 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
| 54 | # |
---|
[1156] | 55 | # Revision 1.20 2003/10/09 21:25:26 jalet |
---|
| 56 | # Multiple printer names or wildcards can be passed on the command line |
---|
| 57 | # separated with commas. |
---|
| 58 | # Beta phase. |
---|
| 59 | # |
---|
[1149] | 60 | # Revision 1.19 2003/10/08 07:01:20 jalet |
---|
| 61 | # Job history can be disabled. |
---|
| 62 | # Some typos in README. |
---|
| 63 | # More messages in setup script. |
---|
| 64 | # |
---|
[1144] | 65 | # Revision 1.18 2003/10/07 09:07:30 jalet |
---|
| 66 | # Character encoding added to please latest version of Python |
---|
| 67 | # |
---|
[1137] | 68 | # Revision 1.17 2003/10/06 13:12:28 jalet |
---|
| 69 | # More work on caching |
---|
| 70 | # |
---|
[1136] | 71 | # Revision 1.16 2003/10/03 18:01:49 jalet |
---|
| 72 | # Nothing interesting... |
---|
| 73 | # |
---|
[1133] | 74 | # Revision 1.15 2003/10/03 12:27:03 jalet |
---|
| 75 | # Several optimizations, especially with LDAP backend |
---|
| 76 | # |
---|
[1131] | 77 | # Revision 1.14 2003/10/03 08:57:55 jalet |
---|
| 78 | # Caching mechanism now caches all that's cacheable. |
---|
| 79 | # |
---|
[1130] | 80 | # Revision 1.13 2003/10/02 20:23:18 jalet |
---|
| 81 | # Storage caching mechanism added. |
---|
| 82 | # |
---|
[1115] | 83 | # Revision 1.12 2003/08/17 14:20:25 jalet |
---|
| 84 | # Bug fix by Oleg Biteryakov |
---|
| 85 | # |
---|
[1113] | 86 | # Revision 1.11 2003/07/29 20:55:17 jalet |
---|
| 87 | # 1.14 is out ! |
---|
| 88 | # |
---|
[1087] | 89 | # Revision 1.10 2003/07/16 21:53:08 jalet |
---|
| 90 | # Really big modifications wrt new configuration file's location and content. |
---|
| 91 | # |
---|
[1085] | 92 | # Revision 1.9 2003/07/14 17:20:15 jalet |
---|
| 93 | # Bug in postgresql storage when modifying the prices for a printer |
---|
| 94 | # |
---|
[1084] | 95 | # Revision 1.8 2003/07/14 14:18:17 jalet |
---|
| 96 | # Wrong documentation strings |
---|
| 97 | # |
---|
[1079] | 98 | # Revision 1.7 2003/07/09 20:17:07 jalet |
---|
| 99 | # Email field added to PostgreSQL schema |
---|
| 100 | # |
---|
[1068] | 101 | # Revision 1.6 2003/07/07 11:49:24 jalet |
---|
| 102 | # Lots of small fixes with the help of PyChecker |
---|
| 103 | # |
---|
[1067] | 104 | # Revision 1.5 2003/07/07 08:33:19 jalet |
---|
| 105 | # Bug fix due to a typo in LDAP code |
---|
| 106 | # |
---|
[1051] | 107 | # Revision 1.4 2003/06/30 13:54:21 jalet |
---|
| 108 | # Sorts by user / group name |
---|
| 109 | # |
---|
[1041] | 110 | # Revision 1.3 2003/06/25 14:10:01 jalet |
---|
| 111 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 112 | # |
---|
[1024] | 113 | # Revision 1.2 2003/06/12 21:09:57 jalet |
---|
| 114 | # wrongly placed code. |
---|
| 115 | # |
---|
[1021] | 116 | # Revision 1.1 2003/06/10 16:37:54 jalet |
---|
| 117 | # Deletion of the second user which is not needed anymore. |
---|
| 118 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 119 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 120 | # greatly help improve performance when time for this will come. |
---|
| 121 | # |
---|
| 122 | # |
---|
| 123 | # |
---|
| 124 | # |
---|
| 125 | |
---|
[1130] | 126 | from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota |
---|
[1021] | 127 | |
---|
| 128 | try : |
---|
| 129 | import pg |
---|
| 130 | except ImportError : |
---|
| 131 | import sys |
---|
| 132 | # TODO : to translate or not to translate ? |
---|
| 133 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] |
---|
| 134 | |
---|
[1130] | 135 | class Storage(BaseStorage) : |
---|
[1021] | 136 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
| 137 | """Opens the PostgreSQL database connection.""" |
---|
[1130] | 138 | BaseStorage.__init__(self, pykotatool) |
---|
[1021] | 139 | try : |
---|
| 140 | (host, port) = host.split(":") |
---|
| 141 | port = int(port) |
---|
| 142 | except ValueError : |
---|
| 143 | port = -1 # Use PostgreSQL's default tcp/ip port (5432). |
---|
| 144 | |
---|
| 145 | try : |
---|
| 146 | self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) |
---|
| 147 | except pg.error, msg : |
---|
| 148 | raise PyKotaStorageError, msg |
---|
| 149 | else : |
---|
[1024] | 150 | self.closed = 0 |
---|
[1130] | 151 | self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) |
---|
[1021] | 152 | |
---|
[1113] | 153 | def close(self) : |
---|
[1021] | 154 | """Closes the database connection.""" |
---|
| 155 | if not self.closed : |
---|
| 156 | self.database.close() |
---|
| 157 | self.closed = 1 |
---|
[1130] | 158 | self.tool.logdebug("Database closed.") |
---|
[1021] | 159 | |
---|
[1041] | 160 | def beginTransaction(self) : |
---|
| 161 | """Starts a transaction.""" |
---|
| 162 | self.database.query("BEGIN;") |
---|
[1130] | 163 | self.tool.logdebug("Transaction begins...") |
---|
[1041] | 164 | |
---|
| 165 | def commitTransaction(self) : |
---|
| 166 | """Commits a transaction.""" |
---|
| 167 | self.database.query("COMMIT;") |
---|
[1130] | 168 | self.tool.logdebug("Transaction committed.") |
---|
[1041] | 169 | |
---|
| 170 | def rollbackTransaction(self) : |
---|
| 171 | """Rollbacks a transaction.""" |
---|
| 172 | self.database.query("ROLLBACK;") |
---|
[1130] | 173 | self.tool.logdebug("Transaction aborted.") |
---|
[1041] | 174 | |
---|
| 175 | def doSearch(self, query) : |
---|
| 176 | """Does a search query.""" |
---|
[1021] | 177 | query = query.strip() |
---|
| 178 | if not query.endswith(';') : |
---|
| 179 | query += ';' |
---|
| 180 | try : |
---|
[1130] | 181 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1021] | 182 | result = self.database.query(query) |
---|
| 183 | except pg.error, msg : |
---|
| 184 | raise PyKotaStorageError, msg |
---|
| 185 | else : |
---|
[1041] | 186 | if (result is not None) and (result.ntuples() > 0) : |
---|
| 187 | return result.dictresult() |
---|
| 188 | |
---|
| 189 | def doModify(self, query) : |
---|
| 190 | """Does a (possibly multiple) modify query.""" |
---|
| 191 | query = query.strip() |
---|
| 192 | if not query.endswith(';') : |
---|
| 193 | query += ';' |
---|
| 194 | try : |
---|
[1130] | 195 | self.tool.logdebug("QUERY : %s" % query) |
---|
[1041] | 196 | result = self.database.query(query) |
---|
| 197 | except pg.error, msg : |
---|
| 198 | raise PyKotaStorageError, msg |
---|
[1068] | 199 | else : |
---|
| 200 | return result |
---|
[1041] | 201 | |
---|
[1021] | 202 | def doQuote(self, field) : |
---|
| 203 | """Quotes a field for use as a string in SQL queries.""" |
---|
| 204 | if type(field) == type(0.0) : |
---|
| 205 | typ = "decimal" |
---|
| 206 | elif type(field) == type(0) : |
---|
| 207 | typ = "int" |
---|
| 208 | else : |
---|
| 209 | typ = "text" |
---|
| 210 | return pg._quote(field, typ) |
---|
| 211 | |
---|
[1179] | 212 | def getAllUsersNames(self) : |
---|
| 213 | """Extracts all user names.""" |
---|
| 214 | usernames = [] |
---|
| 215 | result = self.doSearch("SELECT username FROM users;") |
---|
| 216 | if result : |
---|
| 217 | usernames = [record["username"] for record in result] |
---|
| 218 | return usernames |
---|
| 219 | |
---|
| 220 | def getAllGroupsNames(self) : |
---|
| 221 | """Extracts all group names.""" |
---|
| 222 | groupnames = [] |
---|
| 223 | result = self.doSearch("SELECT groupname FROM groups;") |
---|
| 224 | if result : |
---|
| 225 | groupnames = [record["groupname"] for record in result] |
---|
| 226 | return groupnames |
---|
| 227 | |
---|
[1130] | 228 | def getUserFromBackend(self, username) : |
---|
[1041] | 229 | """Extracts user information given its name.""" |
---|
| 230 | user = StorageUser(self, username) |
---|
| 231 | result = self.doSearch("SELECT * FROM users WHERE username=%s LIMIT 1" % self.doQuote(username)) |
---|
| 232 | if result : |
---|
| 233 | fields = result[0] |
---|
| 234 | user.ident = fields.get("id") |
---|
| 235 | user.LimitBy = fields.get("limitby") |
---|
| 236 | user.AccountBalance = fields.get("balance") |
---|
| 237 | user.LifeTimePaid = fields.get("lifetimepaid") |
---|
[1067] | 238 | user.Email = fields.get("email") |
---|
[1041] | 239 | user.Exists = 1 |
---|
| 240 | return user |
---|
| 241 | |
---|
[1130] | 242 | def getGroupFromBackend(self, groupname) : |
---|
[1041] | 243 | """Extracts group information given its name.""" |
---|
| 244 | group = StorageGroup(self, groupname) |
---|
| 245 | result = self.doSearch("SELECT * FROM groups WHERE groupname=%s LIMIT 1" % self.doQuote(groupname)) |
---|
| 246 | if result : |
---|
| 247 | fields = result[0] |
---|
| 248 | group.ident = fields.get("id") |
---|
| 249 | group.LimitBy = fields.get("limitby") |
---|
| 250 | result = self.doSearch("SELECT SUM(balance) AS balance, SUM(lifetimepaid) AS lifetimepaid FROM users WHERE id IN (SELECT userid FROM groupsmembers WHERE groupid=%s)" % self.doQuote(group.ident)) |
---|
| 251 | if result : |
---|
| 252 | fields = result[0] |
---|
| 253 | group.AccountBalance = fields.get("balance") |
---|
| 254 | group.LifeTimePaid = fields.get("lifetimepaid") |
---|
| 255 | group.Exists = 1 |
---|
| 256 | return group |
---|
| 257 | |
---|
[1130] | 258 | def getPrinterFromBackend(self, printername) : |
---|
[1041] | 259 | """Extracts printer information given its name.""" |
---|
| 260 | printer = StoragePrinter(self, printername) |
---|
| 261 | result = self.doSearch("SELECT * FROM printers WHERE printername=%s LIMIT 1" % self.doQuote(printername)) |
---|
| 262 | if result : |
---|
| 263 | fields = result[0] |
---|
| 264 | printer.ident = fields.get("id") |
---|
| 265 | printer.PricePerJob = fields.get("priceperjob") |
---|
| 266 | printer.PricePerPage = fields.get("priceperpage") |
---|
| 267 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 268 | printer.Exists = 1 |
---|
| 269 | return printer |
---|
| 270 | |
---|
[1130] | 271 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
[1041] | 272 | """Extracts a user print quota.""" |
---|
| 273 | userpquota = StorageUserPQuota(self, user, printer) |
---|
[1228] | 274 | if printer.Exists and user.Exists : |
---|
[1041] | 275 | 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))) |
---|
| 276 | if result : |
---|
| 277 | fields = result[0] |
---|
| 278 | userpquota.ident = fields.get("id") |
---|
| 279 | userpquota.PageCounter = fields.get("pagecounter") |
---|
| 280 | userpquota.LifePageCounter = fields.get("lifepagecounter") |
---|
| 281 | userpquota.SoftLimit = fields.get("softlimit") |
---|
| 282 | userpquota.HardLimit = fields.get("hardlimit") |
---|
| 283 | userpquota.DateLimit = fields.get("datelimit") |
---|
| 284 | userpquota.Exists = 1 |
---|
| 285 | return userpquota |
---|
| 286 | |
---|
[1130] | 287 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
[1041] | 288 | """Extracts a group print quota.""" |
---|
| 289 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 290 | if group.Exists : |
---|
| 291 | result = self.doSearch("SELECT id, softlimit, hardlimit, datelimit FROM grouppquota WHERE groupid=%s AND printerid=%s" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
| 292 | if result : |
---|
| 293 | fields = result[0] |
---|
| 294 | grouppquota.ident = fields.get("id") |
---|
| 295 | grouppquota.SoftLimit = fields.get("softlimit") |
---|
| 296 | grouppquota.HardLimit = fields.get("hardlimit") |
---|
| 297 | grouppquota.DateLimit = fields.get("datelimit") |
---|
| 298 | 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))) |
---|
| 299 | if result : |
---|
| 300 | fields = result[0] |
---|
| 301 | grouppquota.PageCounter = fields.get("pagecounter") |
---|
| 302 | grouppquota.LifePageCounter = fields.get("lifepagecounter") |
---|
| 303 | grouppquota.Exists = 1 |
---|
| 304 | return grouppquota |
---|
| 305 | |
---|
[1130] | 306 | def getPrinterLastJobFromBackend(self, printer) : |
---|
[1041] | 307 | """Extracts a printer's last job information.""" |
---|
| 308 | lastjob = StorageLastJob(self, printer) |
---|
[1203] | 309 | result = self.doSearch("SELECT jobhistory.id, jobid, userid, username, pagecounter, jobsize, jobprice, filename, title, copies, options, jobdate FROM jobhistory, users WHERE printerid=%s AND userid=users.id ORDER BY jobdate DESC LIMIT 1" % self.doQuote(printer.ident)) |
---|
[1041] | 310 | if result : |
---|
| 311 | fields = result[0] |
---|
| 312 | lastjob.ident = fields.get("id") |
---|
| 313 | lastjob.JobId = fields.get("jobid") |
---|
| 314 | lastjob.User = self.getUser(fields.get("username")) |
---|
| 315 | lastjob.PrinterPageCounter = fields.get("pagecounter") |
---|
| 316 | lastjob.JobSize = fields.get("jobsize") |
---|
[1203] | 317 | lastjob.JobPrice = fields.get("jobprice") |
---|
[1041] | 318 | lastjob.JobAction = fields.get("action") |
---|
[1203] | 319 | lastjob.JobFileName = fields.get("filename") |
---|
| 320 | lastjob.JobTitle = fields.get("title") |
---|
| 321 | lastjob.JobCopies = fields.get("copies") |
---|
| 322 | lastjob.JobOptions = fields.get("options") |
---|
[1041] | 323 | lastjob.JobDate = fields.get("jobdate") |
---|
| 324 | lastjob.Exists = 1 |
---|
| 325 | return lastjob |
---|
[1130] | 326 | |
---|
[1137] | 327 | def getGroupMembersFromBackend(self, group) : |
---|
[1130] | 328 | """Returns the group's members list.""" |
---|
| 329 | groupmembers = [] |
---|
| 330 | result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident)) |
---|
| 331 | if result : |
---|
| 332 | for record in result : |
---|
| 333 | user = StorageUser(self, record.get("username")) |
---|
| 334 | user.ident = record.get("userid") |
---|
| 335 | user.LimitBy = record.get("limitby") |
---|
| 336 | user.AccountBalance = record.get("balance") |
---|
| 337 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
| 338 | user.Email = record.get("email") |
---|
| 339 | user.Exists = 1 |
---|
| 340 | groupmembers.append(user) |
---|
[1131] | 341 | self.cacheEntry("USERS", user.Name, user) |
---|
[1130] | 342 | return groupmembers |
---|
| 343 | |
---|
[1137] | 344 | def getUserGroupsFromBackend(self, user) : |
---|
| 345 | """Returns the user's groups list.""" |
---|
| 346 | groups = [] |
---|
| 347 | result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident)) |
---|
| 348 | if result : |
---|
| 349 | for record in result : |
---|
| 350 | groups.append(self.getGroup(record.get("groupname"))) |
---|
| 351 | return groups |
---|
| 352 | |
---|
[1249] | 353 | def getParentPrintersFromBackend(self, printer) : |
---|
| 354 | """Get all the printer groups this printer is a member of.""" |
---|
| 355 | pgroups = [] |
---|
| 356 | result = self.doSearch("SELECT groupid,printername FROM printergroupsmembers JOIN printers ON groupid=id WHERE printerid=%s;" % self.doQuote(printer.ident)) |
---|
| 357 | if result : |
---|
| 358 | for record in result : |
---|
| 359 | if record["groupid"] != printer.ident : # in case of integrity violation |
---|
| 360 | parentprinter = self.getPrinter(record.get("printername")) |
---|
| 361 | if parentprinter.Exists : |
---|
| 362 | pgroups.append(parentprinter) |
---|
| 363 | return pgroups |
---|
| 364 | |
---|
[1021] | 365 | def getMatchingPrinters(self, printerpattern) : |
---|
[1041] | 366 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 367 | printers = [] |
---|
[1021] | 368 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
| 369 | # but we don't because other storages semantics may be different, so every |
---|
| 370 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
[1041] | 371 | result = self.doSearch("SELECT * FROM printers") |
---|
| 372 | if result : |
---|
| 373 | for record in result : |
---|
[1156] | 374 | if self.tool.matchString(record["printername"], printerpattern.split(",")) : |
---|
[1041] | 375 | printer = StoragePrinter(self, record["printername"]) |
---|
| 376 | printer.ident = record.get("id") |
---|
| 377 | printer.PricePerJob = record.get("priceperjob") |
---|
| 378 | printer.PricePerPage = record.get("priceperpage") |
---|
| 379 | printer.LastJob = self.getPrinterLastJob(printer) |
---|
| 380 | printer.Exists = 1 |
---|
| 381 | printers.append(printer) |
---|
[1131] | 382 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
[1041] | 383 | return printers |
---|
[1021] | 384 | |
---|
[1136] | 385 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 386 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 387 | usersandquotas = [] |
---|
[1079] | 388 | 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)) |
---|
[1041] | 389 | if result : |
---|
| 390 | for record in result : |
---|
[1136] | 391 | if self.tool.matchString(record.get("username"), names) : |
---|
[1133] | 392 | user = StorageUser(self, record.get("username")) |
---|
[1041] | 393 | user.ident = record.get("uid") |
---|
| 394 | user.LimitBy = record.get("limitby") |
---|
| 395 | user.AccountBalance = record.get("balance") |
---|
| 396 | user.LifeTimePaid = record.get("lifetimepaid") |
---|
[1079] | 397 | user.Email = record.get("email") |
---|
[1041] | 398 | user.Exists = 1 |
---|
| 399 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 400 | userpquota.ident = record.get("id") |
---|
| 401 | userpquota.PageCounter = record.get("pagecounter") |
---|
| 402 | userpquota.LifePageCounter = record.get("lifepagecounter") |
---|
| 403 | userpquota.SoftLimit = record.get("softlimit") |
---|
| 404 | userpquota.HardLimit = record.get("hardlimit") |
---|
| 405 | userpquota.DateLimit = record.get("datelimit") |
---|
| 406 | userpquota.Exists = 1 |
---|
| 407 | usersandquotas.append((user, userpquota)) |
---|
[1131] | 408 | self.cacheEntry("USERS", user.Name, user) |
---|
| 409 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
[1041] | 410 | return usersandquotas |
---|
| 411 | |
---|
[1136] | 412 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 413 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 414 | groupsandquotas = [] |
---|
[1051] | 415 | 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)) |
---|
[1041] | 416 | if result : |
---|
| 417 | for record in result : |
---|
[1136] | 418 | if self.tool.matchString(record.get("groupname"), names) : |
---|
[1133] | 419 | group = self.getGroup(record.get("groupname")) |
---|
[1041] | 420 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 421 | groupsandquotas.append((group, grouppquota)) |
---|
| 422 | return groupsandquotas |
---|
[1021] | 423 | |
---|
| 424 | def addPrinter(self, printername) : |
---|
[1041] | 425 | """Adds a printer to the quota storage, returns it.""" |
---|
| 426 | self.doModify("INSERT INTO printers (printername) VALUES (%s)" % self.doQuote(printername)) |
---|
| 427 | return self.getPrinter(printername) |
---|
[1021] | 428 | |
---|
[1041] | 429 | def addUser(self, user) : |
---|
[1021] | 430 | """Adds a user to the quota storage, returns its id.""" |
---|
[1087] | 431 | self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email) VALUES (%s, %s, %s, %s, %s)" % (self.doQuote(user.Name), self.doQuote(user.LimitBy), self.doQuote(user.AccountBalance), self.doQuote(user.LifeTimePaid), self.doQuote(user.Email))) |
---|
[1041] | 432 | return self.getUser(user.Name) |
---|
[1021] | 433 | |
---|
[1041] | 434 | def addGroup(self, group) : |
---|
[1021] | 435 | """Adds a group to the quota storage, returns its id.""" |
---|
[1041] | 436 | self.doModify("INSERT INTO groups (groupname, limitby) VALUES (%s, %s)" % (self.doQuote(group.Name), self.doQuote(group.LimitBy))) |
---|
| 437 | return self.getGroup(group.Name) |
---|
| 438 | |
---|
| 439 | def addUserToGroup(self, user, group) : |
---|
| 440 | """Adds an user to a group.""" |
---|
[1068] | 441 | result = self.doSearch("SELECT COUNT(*) AS mexists FROM groupsmembers WHERE groupid=%s AND userid=%s" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
[1021] | 442 | try : |
---|
[1068] | 443 | mexists = int(result[0].get("mexists")) |
---|
| 444 | except (IndexError, TypeError) : |
---|
[1041] | 445 | mexists = 0 |
---|
| 446 | if not mexists : |
---|
| 447 | self.doModify("INSERT INTO groupsmembers (groupid, userid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(user.ident))) |
---|
| 448 | |
---|
| 449 | def addUserPQuota(self, user, printer) : |
---|
| 450 | """Initializes a user print quota on a printer.""" |
---|
| 451 | self.doModify("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s)" % (self.doQuote(user.ident), self.doQuote(printer.ident))) |
---|
| 452 | return self.getUserPQuota(user, printer) |
---|
[1021] | 453 | |
---|
[1041] | 454 | def addGroupPQuota(self, group, printer) : |
---|
| 455 | """Initializes a group print quota on a printer.""" |
---|
| 456 | self.doModify("INSERT INTO grouppquota (groupid, printerid) VALUES (%s, %s)" % (self.doQuote(group.ident), self.doQuote(printer.ident))) |
---|
| 457 | return self.getGroupPQuota(group, printer) |
---|
[1021] | 458 | |
---|
[1041] | 459 | def writePrinterPrices(self, printer) : |
---|
| 460 | """Write the printer's prices back into the storage.""" |
---|
[1085] | 461 | self.doModify("UPDATE printers SET priceperpage=%s, priceperjob=%s WHERE id=%s" % (self.doQuote(printer.PricePerPage), self.doQuote(printer.PricePerJob), self.doQuote(printer.ident))) |
---|
[1021] | 462 | |
---|
[1041] | 463 | def writeUserLimitBy(self, user, limitby) : |
---|
| 464 | """Sets the user's limiting factor.""" |
---|
| 465 | self.doModify("UPDATE users SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(user.ident))) |
---|
[1021] | 466 | |
---|
[1041] | 467 | def writeGroupLimitBy(self, group, limitby) : |
---|
| 468 | """Sets the group's limiting factor.""" |
---|
| 469 | self.doModify("UPDATE groups SET limitby=%s WHERE id=%s" % (self.doQuote(limitby), self.doQuote(group.ident))) |
---|
[1021] | 470 | |
---|
[1041] | 471 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 472 | """Sets the date limit permanently for a user print quota.""" |
---|
[1115] | 473 | self.doModify("UPDATE userpquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(userpquota.ident))) |
---|
[1041] | 474 | |
---|
| 475 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 476 | """Sets the date limit permanently for a group print quota.""" |
---|
[1115] | 477 | self.doModify("UPDATE grouppquota SET datelimit=%s WHERE id=%s" % (self.doQuote(datelimit), self.doQuote(grouppquota.ident))) |
---|
[1021] | 478 | |
---|
[1041] | 479 | def writeUserPQuotaPagesCounters(self, userpquota, newpagecounter, newlifepagecounter) : |
---|
| 480 | """Sets the new page counters permanently for a user print quota.""" |
---|
| 481 | self.doModify("UPDATE userpquota SET pagecounter=%s,lifepagecounter=%s WHERE id=%s" % (self.doQuote(newpagecounter), self.doQuote(newlifepagecounter), self.doQuote(userpquota.ident))) |
---|
| 482 | |
---|
| 483 | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
---|
| 484 | """Sets the new account balance and eventually new lifetime paid.""" |
---|
| 485 | if newlifetimepaid is not None : |
---|
| 486 | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
---|
| 487 | else : |
---|
| 488 | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
---|
| 489 | |
---|
[1203] | 490 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
[1041] | 491 | """Sets the last job's size permanently.""" |
---|
[1203] | 492 | self.doModify("UPDATE jobhistory SET jobsize=%s, jobprice=%s WHERE id=%s" % (self.doQuote(jobsize), self.doQuote(jobprice), self.doQuote(lastjob.ident))) |
---|
[1021] | 493 | |
---|
[1203] | 494 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) : |
---|
[1041] | 495 | """Adds a job in a printer's history.""" |
---|
[1149] | 496 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 497 | if jobsize is not None : |
---|
[1203] | 498 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options) 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(jobsize), self.doQuote(jobprice), self.doQuote(filename), self.doQuote(title), self.doQuote(copies), self.doQuote(options))) |
---|
[1149] | 499 | else : |
---|
[1240] | 500 | self.doModify("INSERT INTO jobhistory (userid, printerid, jobid, pagecounter, action, filename, title, copies, options) VALUES (%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))) |
---|
[1149] | 501 | else : |
---|
| 502 | # here we explicitly want to reset jobsize to NULL if needed |
---|
[1203] | 503 | 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, 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(printer.LastJob.ident))) |
---|
[1041] | 504 | |
---|
| 505 | def writeUserPQuotaLimits(self, userpquota, softlimit, hardlimit) : |
---|
| 506 | """Sets soft and hard limits for a user quota.""" |
---|
| 507 | self.doModify("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userpquota.ident))) |
---|
[1021] | 508 | |
---|
[1041] | 509 | def writeGroupPQuotaLimits(self, grouppquota, softlimit, hardlimit) : |
---|
[1084] | 510 | """Sets soft and hard limits for a group quota on a specific printer.""" |
---|
[1041] | 511 | self.doModify("UPDATE grouppquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE id=%s" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(grouppquota.ident))) |
---|
| 512 | |
---|
[1258] | 513 | def writePrinterToGroup(self, pgroup, printer) : |
---|
| 514 | """Puts a printer into a printer group.""" |
---|
[1259] | 515 | children = [] |
---|
| 516 | result = self.doSearch("SELECT printerid FROM printergroupsmembers WHERE groupid=%s;" % self.doQuote(pgroup.ident)) |
---|
| 517 | if result : |
---|
| 518 | for record in result : |
---|
| 519 | children.append(record.get("printerid")) # TODO : put this into the database integrity rules |
---|
| 520 | if printer.ident not in children : |
---|
| 521 | self.doModify("INSERT INTO printergroupsmembers (groupid, printerid) VALUES (%s, %s);" % (self.doQuote(pgroup.ident), self.doQuote(printer.ident))) |
---|
[1258] | 522 | |
---|
[1041] | 523 | def deleteUser(self, user) : |
---|
[1021] | 524 | """Completely deletes an user from the Quota Storage.""" |
---|
| 525 | # TODO : What should we do if we delete the last person who used a given printer ? |
---|
[1041] | 526 | # TODO : we can't reassign the last job to the previous one, because next user would be |
---|
| 527 | # TODO : incorrectly charged (overcharged). |
---|
| 528 | for q in [ |
---|
| 529 | "DELETE FROM groupsmembers WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 530 | "DELETE FROM jobhistory WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 531 | "DELETE FROM userpquota WHERE userid=%s" % self.doQuote(user.ident), |
---|
| 532 | "DELETE FROM users WHERE id=%s" % self.doQuote(user.ident), |
---|
| 533 | ] : |
---|
| 534 | self.doModify(q) |
---|
[1021] | 535 | |
---|
[1041] | 536 | def deleteGroup(self, group) : |
---|
| 537 | """Completely deletes a group from the Quota Storage.""" |
---|
| 538 | for q in [ |
---|
| 539 | "DELETE FROM groupsmembers WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 540 | "DELETE FROM grouppquota WHERE groupid=%s" % self.doQuote(group.ident), |
---|
| 541 | "DELETE FROM groups WHERE id=%s" % self.doQuote(group.ident), |
---|
| 542 | ] : |
---|
| 543 | self.doModify(q) |
---|
[1021] | 544 | |
---|