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