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