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