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