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