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