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