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