[695] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | # |
---|
[952] | 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
[695] | 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 11 | # |
---|
[873] | 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. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # $Log$ |
---|
[1368] | 24 | # Revision 1.48 2004/02/27 13:50:12 jalet |
---|
| 25 | # Hopefully the final fix for groups (users and printers) |
---|
| 26 | # |
---|
[1367] | 27 | # Revision 1.47 2004/02/27 09:30:33 jalet |
---|
| 28 | # datelimit wasn't reset when modifying soft and hard limits with the LDAP backend |
---|
| 29 | # |
---|
[1365] | 30 | # Revision 1.46 2004/02/26 14:18:07 jalet |
---|
| 31 | # Should fix the remaining bugs wrt printers groups and users groups. |
---|
| 32 | # |
---|
[1364] | 33 | # Revision 1.45 2004/02/26 10:40:40 jalet |
---|
| 34 | # Fixed nested printer groups accounting. |
---|
| 35 | # |
---|
[1363] | 36 | # Revision 1.44 2004/02/25 19:09:24 jalet |
---|
| 37 | # Fix for LDAP problem when job price was 0. |
---|
| 38 | # |
---|
[1359] | 39 | # Revision 1.43 2004/02/25 12:36:34 jalet |
---|
| 40 | # Avoids a database query even if caching was disabled. |
---|
| 41 | # |
---|
[1358] | 42 | # Revision 1.42 2004/02/23 22:53:21 jalet |
---|
| 43 | # Don't retrieve data when it's not needed, to avoid database queries |
---|
| 44 | # |
---|
[1333] | 45 | # Revision 1.41 2004/02/04 17:12:33 jalet |
---|
| 46 | # Removing a printer from a printers group should work now. |
---|
| 47 | # |
---|
[1332] | 48 | # Revision 1.40 2004/02/04 13:24:41 jalet |
---|
| 49 | # pkprinters can now remove printers from printers groups. |
---|
| 50 | # |
---|
[1330] | 51 | # Revision 1.39 2004/02/04 11:16:59 jalet |
---|
| 52 | # pkprinters command line tool added. |
---|
| 53 | # |
---|
[1285] | 54 | # Revision 1.38 2004/01/12 22:43:40 jalet |
---|
| 55 | # New formula to compute a job's price |
---|
| 56 | # |
---|
[1274] | 57 | # Revision 1.37 2004/01/12 14:35:01 jalet |
---|
| 58 | # Printing history added to CGI script. |
---|
| 59 | # |
---|
[1269] | 60 | # Revision 1.36 2004/01/10 09:44:02 jalet |
---|
| 61 | # Fixed potential accuracy problem if a user printed on several printers at |
---|
| 62 | # the very same time. |
---|
| 63 | # |
---|
[1259] | 64 | # Revision 1.35 2004/01/08 16:33:27 jalet |
---|
| 65 | # Additionnal check to not create a circular printers group. |
---|
| 66 | # |
---|
[1258] | 67 | # Revision 1.34 2004/01/08 16:24:49 jalet |
---|
| 68 | # edpykota now supports adding printers to printer groups. |
---|
| 69 | # |
---|
[1257] | 70 | # Revision 1.33 2004/01/08 14:10:32 jalet |
---|
| 71 | # Copyright year changed. |
---|
| 72 | # |
---|
[1252] | 73 | # Revision 1.32 2004/01/06 16:02:57 jalet |
---|
| 74 | # This time printer groups caching works. |
---|
| 75 | # |
---|
[1250] | 76 | # Revision 1.31 2004/01/06 15:51:24 jalet |
---|
| 77 | # Fixed caching of printer groups |
---|
| 78 | # |
---|
[1249] | 79 | # Revision 1.30 2004/01/06 14:24:59 jalet |
---|
| 80 | # Printer groups should be cached now, if caching is enabled. |
---|
| 81 | # |
---|
[1240] | 82 | # Revision 1.29 2003/12/27 16:49:25 uid67467 |
---|
| 83 | # Should be ok now. |
---|
| 84 | # |
---|
[1219] | 85 | # Revision 1.28 2003/11/25 23:46:40 jalet |
---|
| 86 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
| 87 | # |
---|
[1203] | 88 | # Revision 1.27 2003/11/23 19:01:36 jalet |
---|
| 89 | # Job price added to history |
---|
| 90 | # |
---|
[1200] | 91 | # Revision 1.26 2003/11/21 14:28:45 jalet |
---|
| 92 | # More complete job history. |
---|
| 93 | # |
---|
[1151] | 94 | # Revision 1.25 2003/10/08 21:12:27 jalet |
---|
| 95 | # Do not cache anymore entries which don't exist. |
---|
| 96 | # |
---|
[1148] | 97 | # Revision 1.24 2003/10/07 22:06:05 jalet |
---|
| 98 | # Preliminary code to disable job history |
---|
| 99 | # |
---|
[1144] | 100 | # Revision 1.23 2003/10/07 09:07:28 jalet |
---|
| 101 | # Character encoding added to please latest version of Python |
---|
| 102 | # |
---|
[1137] | 103 | # Revision 1.22 2003/10/06 13:12:27 jalet |
---|
| 104 | # More work on caching |
---|
| 105 | # |
---|
[1132] | 106 | # Revision 1.21 2003/10/03 09:02:20 jalet |
---|
| 107 | # Logs cache store actions too |
---|
| 108 | # |
---|
[1130] | 109 | # Revision 1.20 2003/10/02 20:23:18 jalet |
---|
| 110 | # Storage caching mechanism added. |
---|
| 111 | # |
---|
[1087] | 112 | # Revision 1.19 2003/07/16 21:53:07 jalet |
---|
| 113 | # Really big modifications wrt new configuration file's location and content. |
---|
| 114 | # |
---|
[1067] | 115 | # Revision 1.18 2003/07/07 08:33:18 jalet |
---|
| 116 | # Bug fix due to a typo in LDAP code |
---|
| 117 | # |
---|
[1062] | 118 | # Revision 1.17 2003/07/05 07:46:50 jalet |
---|
| 119 | # The previous bug fix was incomplete. |
---|
| 120 | # |
---|
[1043] | 121 | # Revision 1.16 2003/06/25 19:52:31 jalet |
---|
| 122 | # Should be ready for testing :-) |
---|
| 123 | # |
---|
[1042] | 124 | # Revision 1.15 2003/06/25 14:10:58 jalet |
---|
| 125 | # Exception raising for now. |
---|
| 126 | # |
---|
[1041] | 127 | # Revision 1.14 2003/06/25 14:10:01 jalet |
---|
| 128 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 129 | # |
---|
[1021] | 130 | # Revision 1.13 2003/06/10 16:37:54 jalet |
---|
| 131 | # Deletion of the second user which is not needed anymore. |
---|
| 132 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 133 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 134 | # greatly help improve performance when time for this will come. |
---|
| 135 | # |
---|
[952] | 136 | # Revision 1.12 2003/04/23 22:13:57 jalet |
---|
| 137 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 138 | # |
---|
[900] | 139 | # Revision 1.11 2003/04/10 21:47:20 jalet |
---|
| 140 | # Job history added. Upgrade script neutralized for now ! |
---|
| 141 | # |
---|
[873] | 142 | # Revision 1.10 2003/03/29 13:45:27 jalet |
---|
| 143 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 144 | # Two README files were added. |
---|
| 145 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 146 | # |
---|
[802] | 147 | # Revision 1.9 2003/02/17 22:55:01 jalet |
---|
| 148 | # More options can now be set per printer or globally : |
---|
| 149 | # |
---|
[873] | 150 | # admin |
---|
| 151 | # adminmail |
---|
| 152 | # gracedelay |
---|
| 153 | # requester |
---|
[802] | 154 | # |
---|
| 155 | # the printer option has priority when both are defined. |
---|
| 156 | # |
---|
[800] | 157 | # Revision 1.8 2003/02/17 22:05:50 jalet |
---|
| 158 | # Storage backend now supports admin and user passwords (untested) |
---|
| 159 | # |
---|
[791] | 160 | # Revision 1.7 2003/02/10 12:07:31 jalet |
---|
| 161 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 162 | # |
---|
[773] | 163 | # Revision 1.6 2003/02/09 13:05:43 jalet |
---|
| 164 | # Internationalization continues... |
---|
| 165 | # |
---|
[768] | 166 | # Revision 1.5 2003/02/08 22:39:46 jalet |
---|
| 167 | # --reset command line option added |
---|
| 168 | # |
---|
[759] | 169 | # Revision 1.4 2003/02/08 09:59:59 jalet |
---|
| 170 | # Added preliminary base class for all storages |
---|
| 171 | # |
---|
[698] | 172 | # Revision 1.3 2003/02/05 22:10:29 jalet |
---|
| 173 | # Typos |
---|
| 174 | # |
---|
[697] | 175 | # Revision 1.2 2003/02/05 22:02:22 jalet |
---|
| 176 | # __import__ statement didn't work as expected |
---|
| 177 | # |
---|
[695] | 178 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 179 | # Initial import into CVS |
---|
| 180 | # |
---|
| 181 | # |
---|
| 182 | # |
---|
| 183 | |
---|
| 184 | class PyKotaStorageError(Exception): |
---|
| 185 | """An exception for Quota Storage related stuff.""" |
---|
| 186 | def __init__(self, message = ""): |
---|
| 187 | self.message = message |
---|
| 188 | Exception.__init__(self, message) |
---|
| 189 | def __repr__(self): |
---|
| 190 | return self.message |
---|
| 191 | __str__ = __repr__ |
---|
[759] | 192 | |
---|
[1041] | 193 | class StorageObject : |
---|
| 194 | """Object present in the Quota Storage.""" |
---|
| 195 | def __init__(self, parent) : |
---|
| 196 | "Initialize minimal data.""" |
---|
| 197 | self.parent = parent |
---|
| 198 | self.ident = None |
---|
| 199 | self.Exists = 0 |
---|
| 200 | |
---|
| 201 | class StorageUser(StorageObject) : |
---|
| 202 | """User class.""" |
---|
| 203 | def __init__(self, parent, name) : |
---|
| 204 | StorageObject.__init__(self, parent) |
---|
| 205 | self.Name = name |
---|
| 206 | self.LimitBy = None |
---|
| 207 | self.AccountBalance = None |
---|
| 208 | self.LifeTimePaid = None |
---|
[1067] | 209 | self.Email = None |
---|
[1041] | 210 | |
---|
| 211 | def consumeAccountBalance(self, amount) : |
---|
| 212 | """Consumes an amount of money from the user's account balance.""" |
---|
[1269] | 213 | self.parent.decreaseUserAccountBalance(self, amount) |
---|
| 214 | self.AccountBalance = float(self.AccountBalance or 0.0) - amount |
---|
[1041] | 215 | |
---|
| 216 | def setAccountBalance(self, balance, lifetimepaid) : |
---|
| 217 | """Sets the user's account balance in case he pays more money.""" |
---|
[1043] | 218 | self.parent.writeUserAccountBalance(self, balance, lifetimepaid) |
---|
| 219 | self.AccountBalance = balance |
---|
| 220 | self.LifeTimePaid = lifetimepaid |
---|
[1041] | 221 | |
---|
| 222 | def setLimitBy(self, limitby) : |
---|
| 223 | """Sets the user's limiting factor.""" |
---|
[1062] | 224 | try : |
---|
| 225 | limitby = limitby.lower() |
---|
| 226 | except AttributeError : |
---|
| 227 | limitby = "quota" |
---|
[1041] | 228 | if limitby in ["quota", "balance"] : |
---|
| 229 | self.parent.writeUserLimitBy(self, limitby) |
---|
| 230 | self.LimitBy = limitby |
---|
| 231 | |
---|
| 232 | def delete(self) : |
---|
| 233 | """Deletes an user from the Quota Storage.""" |
---|
| 234 | self.parent.beginTransaction() |
---|
| 235 | try : |
---|
| 236 | self.parent.deleteUser(self) |
---|
| 237 | except PyKotaStorageError, msg : |
---|
| 238 | self.parent.rollbackTransaction() |
---|
| 239 | raise PyKotaStorageError, msg |
---|
| 240 | else : |
---|
| 241 | self.parent.commitTransaction() |
---|
| 242 | |
---|
| 243 | class StorageGroup(StorageObject) : |
---|
| 244 | """User class.""" |
---|
| 245 | def __init__(self, parent, name) : |
---|
| 246 | StorageObject.__init__(self, parent) |
---|
| 247 | self.Name = name |
---|
| 248 | self.LimitBy = None |
---|
| 249 | self.AccountBalance = None |
---|
| 250 | self.LifeTimePaid = None |
---|
| 251 | |
---|
| 252 | def setLimitBy(self, limitby) : |
---|
| 253 | """Sets the user's limiting factor.""" |
---|
[1062] | 254 | try : |
---|
| 255 | limitby = limitby.lower() |
---|
| 256 | except AttributeError : |
---|
| 257 | limitby = "quota" |
---|
[1041] | 258 | if limitby in ["quota", "balance"] : |
---|
| 259 | self.parent.writeGroupLimitBy(self, limitby) |
---|
| 260 | self.LimitBy = limitby |
---|
| 261 | |
---|
| 262 | def delete(self) : |
---|
| 263 | """Deletes a group from the Quota Storage.""" |
---|
| 264 | self.parent.beginTransaction() |
---|
| 265 | try : |
---|
| 266 | self.parent.deleteGroup(self) |
---|
| 267 | except PyKotaStorageError, msg : |
---|
| 268 | self.parent.rollbackTransaction() |
---|
| 269 | raise PyKotaStorageError, msg |
---|
| 270 | else : |
---|
| 271 | self.parent.commitTransaction() |
---|
| 272 | |
---|
| 273 | class StoragePrinter(StorageObject) : |
---|
| 274 | """Printer class.""" |
---|
| 275 | def __init__(self, parent, name) : |
---|
| 276 | StorageObject.__init__(self, parent) |
---|
| 277 | self.Name = name |
---|
| 278 | self.PricePerPage = None |
---|
| 279 | self.PricePerJob = None |
---|
| 280 | |
---|
[1358] | 281 | def __getattr__(self, name) : |
---|
| 282 | """Delays data retrieval until it's really needed.""" |
---|
| 283 | if name == "LastJob" : |
---|
| 284 | self.LastJob = self.parent.getPrinterLastJob(self) |
---|
| 285 | return self.LastJob |
---|
| 286 | else : |
---|
| 287 | raise AttributeError, name |
---|
| 288 | |
---|
[1203] | 289 | def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) : |
---|
[1041] | 290 | """Adds a job to the printer's history.""" |
---|
[1203] | 291 | self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options) |
---|
[1041] | 292 | # TODO : update LastJob object ? Probably not needed. |
---|
| 293 | |
---|
[1258] | 294 | def addPrinterToGroup(self, printer) : |
---|
| 295 | """Adds a printer to a printer group.""" |
---|
[1259] | 296 | if (printer not in self.parent.getParentPrinters(self)) and (printer.ident != self.ident) : |
---|
[1258] | 297 | self.parent.writePrinterToGroup(self, printer) |
---|
[1269] | 298 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
[1332] | 299 | |
---|
| 300 | def delPrinterFromGroup(self, printer) : |
---|
| 301 | """Deletes a printer from a printer group.""" |
---|
[1333] | 302 | self.parent.removePrinterFromGroup(self, printer) |
---|
| 303 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
[1258] | 304 | |
---|
[1041] | 305 | def setPrices(self, priceperpage = None, priceperjob = None) : |
---|
| 306 | """Sets the printer's prices.""" |
---|
| 307 | if priceperpage is None : |
---|
| 308 | priceperpage = self.PricePerPage |
---|
| 309 | else : |
---|
| 310 | self.PricePerPage = float(priceperpage) |
---|
| 311 | if priceperjob is None : |
---|
| 312 | priceperjob = self.PricePerJob |
---|
| 313 | else : |
---|
| 314 | self.PricePerJob = float(priceperjob) |
---|
| 315 | self.parent.writePrinterPrices(self) |
---|
| 316 | |
---|
[1330] | 317 | def delete(self) : |
---|
| 318 | """Deletes a printer from the Quota Storage.""" |
---|
| 319 | self.parent.beginTransaction() |
---|
| 320 | try : |
---|
| 321 | self.parent.deletePrinter(self) |
---|
| 322 | except PyKotaStorageError, msg : |
---|
| 323 | self.parent.rollbackTransaction() |
---|
| 324 | raise PyKotaStorageError, msg |
---|
| 325 | else : |
---|
| 326 | self.parent.commitTransaction() |
---|
| 327 | |
---|
[1041] | 328 | class StorageUserPQuota(StorageObject) : |
---|
| 329 | """User Print Quota class.""" |
---|
| 330 | def __init__(self, parent, user, printer) : |
---|
| 331 | StorageObject.__init__(self, parent) |
---|
| 332 | self.User = user |
---|
| 333 | self.Printer = printer |
---|
| 334 | self.PageCounter = None |
---|
| 335 | self.LifePageCounter = None |
---|
| 336 | self.SoftLimit = None |
---|
| 337 | self.HardLimit = None |
---|
| 338 | self.DateLimit = None |
---|
| 339 | |
---|
[1358] | 340 | def __getattr__(self, name) : |
---|
| 341 | """Delays data retrieval until it's really needed.""" |
---|
| 342 | if name == "ParentPrintersUserPQuota" : |
---|
| 343 | self.ParentPrintersUserPQuota = (self.User.Exists and self.Printer.Exists and self.parent.getParentPrintersUserPQuota(self)) or [] |
---|
| 344 | return self.ParentPrintersUserPQuota |
---|
| 345 | else : |
---|
| 346 | raise AttributeError, name |
---|
| 347 | |
---|
[1041] | 348 | def setDateLimit(self, datelimit) : |
---|
| 349 | """Sets the date limit for this quota.""" |
---|
| 350 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 351 | self.parent.writeUserPQuotaDateLimit(self, date) |
---|
| 352 | self.DateLimit = date |
---|
| 353 | |
---|
| 354 | def setLimits(self, softlimit, hardlimit) : |
---|
| 355 | """Sets the soft and hard limit for this quota.""" |
---|
| 356 | self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit) |
---|
| 357 | self.SoftLimit = softlimit |
---|
| 358 | self.HardLimit = hardlimit |
---|
[1367] | 359 | self.DateLimit = None |
---|
[1041] | 360 | |
---|
| 361 | def reset(self) : |
---|
| 362 | """Resets page counter to 0.""" |
---|
| 363 | self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0)) |
---|
| 364 | self.PageCounter = 0 |
---|
| 365 | |
---|
[1285] | 366 | def computeJobPrice(self, jobsize) : |
---|
| 367 | """Computes the job price as the sum of all parent printers' prices + current printer's ones.""" |
---|
| 368 | totalprice = 0.0 |
---|
| 369 | if jobsize : |
---|
| 370 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
| 371 | price = (float(upq.Printer.PricePerPage or 0.0) * jobsize) + float(upq.Printer.PricePerJob or 0.0) |
---|
| 372 | totalprice += price |
---|
| 373 | return totalprice |
---|
| 374 | |
---|
| 375 | def increasePagesUsage(self, jobsize) : |
---|
[1041] | 376 | """Increase the value of used pages and money.""" |
---|
[1285] | 377 | jobprice = self.computeJobPrice(jobsize) |
---|
| 378 | if jobsize : |
---|
| 379 | self.parent.beginTransaction() |
---|
| 380 | try : |
---|
[1363] | 381 | if jobprice : |
---|
| 382 | self.User.consumeAccountBalance(jobprice) |
---|
[1240] | 383 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
[1285] | 384 | self.parent.increaseUserPQuotaPagesCounters(upq, jobsize) |
---|
| 385 | upq.PageCounter = int(upq.PageCounter or 0) + jobsize |
---|
| 386 | upq.LifePageCounter = int(upq.LifePageCounter or 0) + jobsize |
---|
| 387 | except PyKotaStorageError, msg : |
---|
| 388 | self.parent.rollbackTransaction() |
---|
| 389 | raise PyKotaStorageError, msg |
---|
| 390 | else : |
---|
| 391 | self.parent.commitTransaction() |
---|
| 392 | return jobprice |
---|
[1041] | 393 | |
---|
| 394 | class StorageGroupPQuota(StorageObject) : |
---|
| 395 | """Group Print Quota class.""" |
---|
| 396 | def __init__(self, parent, group, printer) : |
---|
| 397 | StorageObject.__init__(self, parent) |
---|
| 398 | self.Group = group |
---|
| 399 | self.Printer = printer |
---|
| 400 | self.PageCounter = None |
---|
| 401 | self.LifePageCounter = None |
---|
| 402 | self.SoftLimit = None |
---|
| 403 | self.HardLimit = None |
---|
| 404 | self.DateLimit = None |
---|
| 405 | |
---|
[1365] | 406 | def __getattr__(self, name) : |
---|
| 407 | """Delays data retrieval until it's really needed.""" |
---|
| 408 | if name == "ParentPrintersGroupPQuota" : |
---|
| 409 | self.ParentPrintersGroupPQuota = (self.Group.Exists and self.Printer.Exists and self.parent.getParentPrintersGroupPQuota(self)) or [] |
---|
| 410 | return self.ParentPrintersGroupPQuota |
---|
| 411 | else : |
---|
| 412 | raise AttributeError, name |
---|
| 413 | |
---|
[1041] | 414 | def setDateLimit(self, datelimit) : |
---|
| 415 | """Sets the date limit for this quota.""" |
---|
| 416 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 417 | self.parent.writeGroupPQuotaDateLimit(self, date) |
---|
| 418 | self.DateLimit = date |
---|
| 419 | |
---|
| 420 | def setLimits(self, softlimit, hardlimit) : |
---|
| 421 | """Sets the soft and hard limit for this quota.""" |
---|
| 422 | self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit) |
---|
| 423 | self.SoftLimit = softlimit |
---|
| 424 | self.HardLimit = hardlimit |
---|
[1367] | 425 | self.DateLimit = None |
---|
[1041] | 426 | |
---|
[1274] | 427 | class StorageJob(StorageObject) : |
---|
[1041] | 428 | """Printer's Last Job class.""" |
---|
[1274] | 429 | def __init__(self, parent) : |
---|
[1041] | 430 | StorageObject.__init__(self, parent) |
---|
[1358] | 431 | self.UserName = None |
---|
| 432 | self.PrinterName = None |
---|
[1041] | 433 | self.JobId = None |
---|
| 434 | self.PrinterPageCounter = None |
---|
| 435 | self.JobSize = None |
---|
| 436 | self.JobAction = None |
---|
| 437 | self.JobDate = None |
---|
[1203] | 438 | self.JobPrice = None |
---|
| 439 | self.JobFileName = None |
---|
| 440 | self.JobTitle = None |
---|
| 441 | self.JobCopies = None |
---|
| 442 | self.JobOptions = None |
---|
[1041] | 443 | |
---|
[1358] | 444 | def __getattr__(self, name) : |
---|
| 445 | """Delays data retrieval until it's really needed.""" |
---|
| 446 | if name == "User" : |
---|
| 447 | self.User = self.parent.getUser(self.UserName) |
---|
| 448 | return self.User |
---|
| 449 | elif name == "Printer" : |
---|
| 450 | self.Printer = self.parent.getPrinter(self.PrinterName) |
---|
| 451 | return self.Printer |
---|
| 452 | else : |
---|
| 453 | raise AttributeError, name |
---|
| 454 | |
---|
[1274] | 455 | class StorageLastJob(StorageJob) : |
---|
| 456 | """Printer's Last Job class.""" |
---|
| 457 | def __init__(self, parent, printer) : |
---|
| 458 | StorageJob.__init__(self, parent) |
---|
[1359] | 459 | self.PrinterName = printer.Name # not needed |
---|
[1274] | 460 | self.Printer = printer |
---|
| 461 | |
---|
[1358] | 462 | def __getattr__(self, name) : |
---|
| 463 | """Delays data retrieval until it's really needed.""" |
---|
| 464 | if name == "User" : |
---|
| 465 | self.User = self.parent.getUser(self.UserName) |
---|
| 466 | return self.User |
---|
| 467 | else : |
---|
| 468 | raise AttributeError, name |
---|
| 469 | |
---|
[1359] | 470 | def setSize(self, userpquota, jobsize) : |
---|
[1041] | 471 | """Sets the last job's size.""" |
---|
[1359] | 472 | jobprice = userpquota.computeJobPrice(jobsize) |
---|
[1203] | 473 | self.parent.writeLastJobSize(self, jobsize, jobprice) |
---|
[1041] | 474 | self.JobSize = jobsize |
---|
[1203] | 475 | self.JobPrice = jobprice |
---|
[1274] | 476 | |
---|
[1130] | 477 | class BaseStorage : |
---|
| 478 | def __init__(self, pykotatool) : |
---|
| 479 | """Opens the LDAP connection.""" |
---|
| 480 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
| 481 | self.closed = 1 |
---|
| 482 | self.tool = pykotatool |
---|
| 483 | self.usecache = pykotatool.config.getCaching() |
---|
[1148] | 484 | self.disablehistory = pykotatool.config.getDisableHistory() |
---|
[1130] | 485 | if self.usecache : |
---|
| 486 | self.tool.logdebug("Caching enabled.") |
---|
[1252] | 487 | self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} } |
---|
[1130] | 488 | |
---|
[1240] | 489 | def close(self) : |
---|
| 490 | """Must be overriden in children classes.""" |
---|
| 491 | raise RuntimeError, "BaseStorage.close() must be overriden !" |
---|
| 492 | |
---|
[1130] | 493 | def __del__(self) : |
---|
| 494 | """Ensures that the database connection is closed.""" |
---|
| 495 | self.close() |
---|
| 496 | |
---|
| 497 | def getFromCache(self, cachetype, key) : |
---|
| 498 | """Tries to extract something from the cache.""" |
---|
| 499 | if self.usecache : |
---|
| 500 | entry = self.caches[cachetype].get(key) |
---|
| 501 | if entry is not None : |
---|
| 502 | self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key)) |
---|
| 503 | else : |
---|
| 504 | self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key)) |
---|
| 505 | return entry |
---|
| 506 | |
---|
| 507 | def cacheEntry(self, cachetype, key, value) : |
---|
| 508 | """Puts an entry in the cache.""" |
---|
[1151] | 509 | if self.usecache and getattr(value, "Exists", 0) : |
---|
[1130] | 510 | self.caches[cachetype][key] = value |
---|
[1132] | 511 | self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key)) |
---|
[1130] | 512 | |
---|
| 513 | def getUser(self, username) : |
---|
| 514 | """Returns the user from cache.""" |
---|
| 515 | user = self.getFromCache("USERS", username) |
---|
| 516 | if user is None : |
---|
| 517 | user = self.getUserFromBackend(username) |
---|
| 518 | self.cacheEntry("USERS", username, user) |
---|
| 519 | return user |
---|
| 520 | |
---|
| 521 | def getGroup(self, groupname) : |
---|
| 522 | """Returns the group from cache.""" |
---|
| 523 | group = self.getFromCache("GROUPS", groupname) |
---|
| 524 | if group is None : |
---|
| 525 | group = self.getGroupFromBackend(groupname) |
---|
| 526 | self.cacheEntry("GROUPS", groupname, group) |
---|
| 527 | return group |
---|
| 528 | |
---|
| 529 | def getPrinter(self, printername) : |
---|
| 530 | """Returns the printer from cache.""" |
---|
| 531 | printer = self.getFromCache("PRINTERS", printername) |
---|
| 532 | if printer is None : |
---|
| 533 | printer = self.getPrinterFromBackend(printername) |
---|
| 534 | self.cacheEntry("PRINTERS", printername, printer) |
---|
| 535 | return printer |
---|
| 536 | |
---|
| 537 | def getUserPQuota(self, user, printer) : |
---|
| 538 | """Returns the user quota information from cache.""" |
---|
| 539 | useratprinter = "%s@%s" % (user.Name, printer.Name) |
---|
| 540 | upquota = self.getFromCache("USERPQUOTAS", useratprinter) |
---|
| 541 | if upquota is None : |
---|
| 542 | upquota = self.getUserPQuotaFromBackend(user, printer) |
---|
| 543 | self.cacheEntry("USERPQUOTAS", useratprinter, upquota) |
---|
| 544 | return upquota |
---|
| 545 | |
---|
| 546 | def getGroupPQuota(self, group, printer) : |
---|
| 547 | """Returns the group quota information from cache.""" |
---|
| 548 | groupatprinter = "%s@%s" % (group.Name, printer.Name) |
---|
| 549 | gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter) |
---|
| 550 | if gpquota is None : |
---|
| 551 | gpquota = self.getGroupPQuotaFromBackend(group, printer) |
---|
| 552 | self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota) |
---|
| 553 | return gpquota |
---|
| 554 | |
---|
| 555 | def getPrinterLastJob(self, printer) : |
---|
| 556 | """Extracts last job information for a given printer from cache.""" |
---|
| 557 | lastjob = self.getFromCache("LASTJOBS", printer.Name) |
---|
| 558 | if lastjob is None : |
---|
| 559 | lastjob = self.getPrinterLastJobFromBackend(printer) |
---|
| 560 | self.cacheEntry("LASTJOBS", printer.Name, lastjob) |
---|
| 561 | return lastjob |
---|
| 562 | |
---|
[1249] | 563 | def getParentPrinters(self, printer) : |
---|
| 564 | """Extracts parent printers information for a given printer from cache.""" |
---|
[1252] | 565 | if self.usecache : |
---|
| 566 | if not hasattr(printer, "Parents") : |
---|
| 567 | self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name) |
---|
| 568 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
| 569 | self.tool.logdebug("Cache store (%s->Parents)" % printer.Name) |
---|
| 570 | else : |
---|
| 571 | self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name) |
---|
| 572 | else : |
---|
| 573 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
[1364] | 574 | for parent in printer.Parents[:] : |
---|
| 575 | printer.Parents.extend(self.getParentPrinters(parent)) |
---|
[1368] | 576 | uniquedic = {} |
---|
| 577 | for parent in printer.Parents : |
---|
| 578 | uniquedic[parent.Name] = parent |
---|
| 579 | printer.Parents = uniquedic.values() |
---|
[1252] | 580 | return printer.Parents |
---|
[1249] | 581 | |
---|
[1137] | 582 | def getGroupMembers(self, group) : |
---|
| 583 | """Returns the group's members list from in-group cache.""" |
---|
| 584 | if self.usecache : |
---|
| 585 | if not hasattr(group, "Members") : |
---|
| 586 | self.tool.logdebug("Cache miss (%s->Members)" % group.Name) |
---|
| 587 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 588 | self.tool.logdebug("Cache store (%s->Members)" % group.Name) |
---|
| 589 | else : |
---|
| 590 | self.tool.logdebug("Cache hit (%s->Members)" % group.Name) |
---|
| 591 | else : |
---|
| 592 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 593 | return group.Members |
---|
| 594 | |
---|
| 595 | def getUserGroups(self, user) : |
---|
| 596 | """Returns the user's groups list from in-user cache.""" |
---|
| 597 | if self.usecache : |
---|
| 598 | if not hasattr(user, "Groups") : |
---|
| 599 | self.tool.logdebug("Cache miss (%s->Groups)" % user.Name) |
---|
| 600 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 601 | self.tool.logdebug("Cache store (%s->Groups)" % user.Name) |
---|
| 602 | else : |
---|
| 603 | self.tool.logdebug("Cache hit (%s->Groups)" % user.Name) |
---|
| 604 | else : |
---|
| 605 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 606 | return user.Groups |
---|
| 607 | |
---|
[1240] | 608 | def getParentPrintersUserPQuota(self, userpquota) : |
---|
[1364] | 609 | """Returns all user print quota on the printer and all its parents recursively.""" |
---|
[1365] | 610 | upquotas = [ ] |
---|
[1240] | 611 | for printer in self.getParentPrinters(userpquota.Printer) : |
---|
| 612 | upquotas.append(self.getUserPQuota(userpquota.User, printer)) |
---|
| 613 | return upquotas |
---|
| 614 | |
---|
[1365] | 615 | def getParentPrintersGroupPQuota(self, grouppquota) : |
---|
| 616 | """Returns all group print quota on the printer and all its parents recursively.""" |
---|
| 617 | gpquotas = [ ] |
---|
| 618 | for printer in self.getParentPrinters(grouppquota.Printer) : |
---|
| 619 | gpquotas.append(self.getGroupPQuota(grouppquota.Group, printer)) |
---|
| 620 | return gpquotas |
---|
| 621 | |
---|
[1021] | 622 | def openConnection(pykotatool) : |
---|
[695] | 623 | """Returns a connection handle to the appropriate Quota Storage Database.""" |
---|
[1021] | 624 | backendinfo = pykotatool.config.getStorageBackend() |
---|
[800] | 625 | backend = backendinfo["storagebackend"] |
---|
[695] | 626 | try : |
---|
[1219] | 627 | exec "from pykota.storages import %s as storagebackend" % backend.lower() |
---|
[695] | 628 | except ImportError : |
---|
[773] | 629 | raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend |
---|
[695] | 630 | else : |
---|
[800] | 631 | host = backendinfo["storageserver"] |
---|
| 632 | database = backendinfo["storagename"] |
---|
[1087] | 633 | admin = backendinfo["storageadmin"] or backendinfo["storageuser"] |
---|
| 634 | adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"] |
---|
[1240] | 635 | return storagebackend.Storage(pykotatool, host, database, admin, adminpw) |
---|