[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$ |
---|
[2030] | 24 | # Revision 1.67 2005/01/18 19:47:50 jalet |
---|
| 25 | # Big bug fix wrt the datelimit attribute |
---|
| 26 | # |
---|
[1967] | 27 | # Revision 1.66 2004/12/02 21:24:50 jalet |
---|
| 28 | # Integrated the patch by Wilson Roberto Afonso and Matt Hyclak to allow |
---|
| 29 | # edpykota to accept the -U | --used value command line option. |
---|
| 30 | # |
---|
[1875] | 31 | # Revision 1.65 2004/10/25 14:12:25 jalet |
---|
| 32 | # For URGENT legal reasons (Italy), a new "privacy" directive was added to pykota.conf |
---|
| 33 | # to hide print jobs' title, filename, and options. |
---|
| 34 | # |
---|
[1791] | 35 | # Revision 1.64 2004/10/08 20:19:18 jalet |
---|
| 36 | # Added ugly workaround for strange locale problem |
---|
| 37 | # |
---|
[1790] | 38 | # Revision 1.63 2004/10/07 21:14:28 jalet |
---|
| 39 | # Hopefully final fix for data encoding to and from the database |
---|
| 40 | # |
---|
[1755] | 41 | # Revision 1.62 2004/09/28 17:45:31 jalet |
---|
| 42 | # Added the --hardreset command line option to edpykota |
---|
| 43 | # |
---|
[1743] | 44 | # Revision 1.61 2004/09/24 21:19:48 jalet |
---|
| 45 | # Did a pass of PyChecker |
---|
| 46 | # |
---|
[1742] | 47 | # Revision 1.60 2004/09/24 20:21:50 jalet |
---|
| 48 | # Fixed pykotaAccountBalance object location during creation |
---|
| 49 | # |
---|
[1711] | 50 | # Revision 1.59 2004/09/10 21:32:52 jalet |
---|
| 51 | # Small fixes for incomplete entry intialization |
---|
| 52 | # |
---|
[1692] | 53 | # Revision 1.58 2004/09/02 10:09:30 jalet |
---|
| 54 | # Fixed bug in LDAP user deletion code which didn't correctly delete the user's |
---|
| 55 | # pykotaLastJob entries. |
---|
| 56 | # |
---|
[1624] | 57 | # Revision 1.57 2004/07/22 22:41:48 jalet |
---|
| 58 | # Hardware accounting for LPRng should be OK now. UNTESTED. |
---|
| 59 | # |
---|
[1582] | 60 | # Revision 1.56 2004/07/01 17:45:49 jalet |
---|
| 61 | # Added code to handle the description field for printers |
---|
| 62 | # |
---|
[1523] | 63 | # Revision 1.55 2004/06/05 22:18:04 jalet |
---|
| 64 | # Now catches some exceptions earlier. |
---|
| 65 | # storage.py and ldapstorage.py : removed old comments |
---|
| 66 | # |
---|
[1522] | 67 | # Revision 1.54 2004/06/05 22:03:49 jalet |
---|
| 68 | # Payments history is now stored in database |
---|
| 69 | # |
---|
[1520] | 70 | # Revision 1.53 2004/06/03 23:14:10 jalet |
---|
| 71 | # Now stores the job's size in bytes in the database. |
---|
| 72 | # Preliminary work on payments storage : database schemas are OK now, |
---|
| 73 | # but no code to store payments yet. |
---|
| 74 | # Removed schema picture, not relevant anymore. |
---|
| 75 | # |
---|
[1502] | 76 | # Revision 1.52 2004/05/26 14:49:57 jalet |
---|
| 77 | # First try at saving the job-originating-hostname in the database |
---|
| 78 | # |
---|
[1418] | 79 | # Revision 1.51 2004/03/24 15:15:24 jalet |
---|
| 80 | # Began integration of Henrik Janhagen's work on quota-then-balance |
---|
| 81 | # and balance-then-quota |
---|
| 82 | # |
---|
[1395] | 83 | # Revision 1.50 2004/03/09 08:05:27 jalet |
---|
| 84 | # Small fix : only keeps existing quota entries when searching parents |
---|
| 85 | # |
---|
[1376] | 86 | # Revision 1.49 2004/03/01 15:06:51 jalet |
---|
| 87 | # Pre and Post hooks should now work in the pykota filter too. |
---|
| 88 | # The pykota filter doesn't check the last user's quota anymore |
---|
| 89 | # when delayed hardware accounting is used : this will be checked |
---|
| 90 | # anyway the next time the last user will print |
---|
| 91 | # |
---|
[1368] | 92 | # Revision 1.48 2004/02/27 13:50:12 jalet |
---|
| 93 | # Hopefully the final fix for groups (users and printers) |
---|
| 94 | # |
---|
[1367] | 95 | # Revision 1.47 2004/02/27 09:30:33 jalet |
---|
| 96 | # datelimit wasn't reset when modifying soft and hard limits with the LDAP backend |
---|
| 97 | # |
---|
[1365] | 98 | # Revision 1.46 2004/02/26 14:18:07 jalet |
---|
| 99 | # Should fix the remaining bugs wrt printers groups and users groups. |
---|
| 100 | # |
---|
[1364] | 101 | # Revision 1.45 2004/02/26 10:40:40 jalet |
---|
| 102 | # Fixed nested printer groups accounting. |
---|
| 103 | # |
---|
[1363] | 104 | # Revision 1.44 2004/02/25 19:09:24 jalet |
---|
| 105 | # Fix for LDAP problem when job price was 0. |
---|
| 106 | # |
---|
[1359] | 107 | # Revision 1.43 2004/02/25 12:36:34 jalet |
---|
| 108 | # Avoids a database query even if caching was disabled. |
---|
| 109 | # |
---|
[1358] | 110 | # Revision 1.42 2004/02/23 22:53:21 jalet |
---|
| 111 | # Don't retrieve data when it's not needed, to avoid database queries |
---|
| 112 | # |
---|
[1333] | 113 | # Revision 1.41 2004/02/04 17:12:33 jalet |
---|
| 114 | # Removing a printer from a printers group should work now. |
---|
| 115 | # |
---|
[1332] | 116 | # Revision 1.40 2004/02/04 13:24:41 jalet |
---|
| 117 | # pkprinters can now remove printers from printers groups. |
---|
| 118 | # |
---|
[1330] | 119 | # Revision 1.39 2004/02/04 11:16:59 jalet |
---|
| 120 | # pkprinters command line tool added. |
---|
| 121 | # |
---|
[1285] | 122 | # Revision 1.38 2004/01/12 22:43:40 jalet |
---|
| 123 | # New formula to compute a job's price |
---|
| 124 | # |
---|
[1274] | 125 | # Revision 1.37 2004/01/12 14:35:01 jalet |
---|
| 126 | # Printing history added to CGI script. |
---|
| 127 | # |
---|
[1269] | 128 | # Revision 1.36 2004/01/10 09:44:02 jalet |
---|
| 129 | # Fixed potential accuracy problem if a user printed on several printers at |
---|
| 130 | # the very same time. |
---|
| 131 | # |
---|
[1259] | 132 | # Revision 1.35 2004/01/08 16:33:27 jalet |
---|
| 133 | # Additionnal check to not create a circular printers group. |
---|
| 134 | # |
---|
[1258] | 135 | # Revision 1.34 2004/01/08 16:24:49 jalet |
---|
| 136 | # edpykota now supports adding printers to printer groups. |
---|
| 137 | # |
---|
[1257] | 138 | # Revision 1.33 2004/01/08 14:10:32 jalet |
---|
| 139 | # Copyright year changed. |
---|
| 140 | # |
---|
[1252] | 141 | # Revision 1.32 2004/01/06 16:02:57 jalet |
---|
| 142 | # This time printer groups caching works. |
---|
| 143 | # |
---|
[1250] | 144 | # Revision 1.31 2004/01/06 15:51:24 jalet |
---|
| 145 | # Fixed caching of printer groups |
---|
| 146 | # |
---|
[1249] | 147 | # Revision 1.30 2004/01/06 14:24:59 jalet |
---|
| 148 | # Printer groups should be cached now, if caching is enabled. |
---|
| 149 | # |
---|
[1240] | 150 | # Revision 1.29 2003/12/27 16:49:25 uid67467 |
---|
| 151 | # Should be ok now. |
---|
| 152 | # |
---|
[1219] | 153 | # Revision 1.28 2003/11/25 23:46:40 jalet |
---|
| 154 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
| 155 | # |
---|
[1203] | 156 | # Revision 1.27 2003/11/23 19:01:36 jalet |
---|
| 157 | # Job price added to history |
---|
| 158 | # |
---|
[1200] | 159 | # Revision 1.26 2003/11/21 14:28:45 jalet |
---|
| 160 | # More complete job history. |
---|
| 161 | # |
---|
[1151] | 162 | # Revision 1.25 2003/10/08 21:12:27 jalet |
---|
| 163 | # Do not cache anymore entries which don't exist. |
---|
| 164 | # |
---|
[1148] | 165 | # Revision 1.24 2003/10/07 22:06:05 jalet |
---|
| 166 | # Preliminary code to disable job history |
---|
| 167 | # |
---|
[1144] | 168 | # Revision 1.23 2003/10/07 09:07:28 jalet |
---|
| 169 | # Character encoding added to please latest version of Python |
---|
| 170 | # |
---|
[1137] | 171 | # Revision 1.22 2003/10/06 13:12:27 jalet |
---|
| 172 | # More work on caching |
---|
| 173 | # |
---|
[1132] | 174 | # Revision 1.21 2003/10/03 09:02:20 jalet |
---|
| 175 | # Logs cache store actions too |
---|
| 176 | # |
---|
[1130] | 177 | # Revision 1.20 2003/10/02 20:23:18 jalet |
---|
| 178 | # Storage caching mechanism added. |
---|
| 179 | # |
---|
[1087] | 180 | # Revision 1.19 2003/07/16 21:53:07 jalet |
---|
| 181 | # Really big modifications wrt new configuration file's location and content. |
---|
| 182 | # |
---|
[1067] | 183 | # Revision 1.18 2003/07/07 08:33:18 jalet |
---|
| 184 | # Bug fix due to a typo in LDAP code |
---|
| 185 | # |
---|
[1062] | 186 | # Revision 1.17 2003/07/05 07:46:50 jalet |
---|
| 187 | # The previous bug fix was incomplete. |
---|
| 188 | # |
---|
[1043] | 189 | # Revision 1.16 2003/06/25 19:52:31 jalet |
---|
| 190 | # Should be ready for testing :-) |
---|
| 191 | # |
---|
[1042] | 192 | # Revision 1.15 2003/06/25 14:10:58 jalet |
---|
| 193 | # Exception raising for now. |
---|
| 194 | # |
---|
[1041] | 195 | # Revision 1.14 2003/06/25 14:10:01 jalet |
---|
| 196 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 197 | # |
---|
[1021] | 198 | # Revision 1.13 2003/06/10 16:37:54 jalet |
---|
| 199 | # Deletion of the second user which is not needed anymore. |
---|
| 200 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 201 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 202 | # greatly help improve performance when time for this will come. |
---|
| 203 | # |
---|
[952] | 204 | # Revision 1.12 2003/04/23 22:13:57 jalet |
---|
| 205 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 206 | # |
---|
[900] | 207 | # Revision 1.11 2003/04/10 21:47:20 jalet |
---|
| 208 | # Job history added. Upgrade script neutralized for now ! |
---|
| 209 | # |
---|
[873] | 210 | # Revision 1.10 2003/03/29 13:45:27 jalet |
---|
| 211 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 212 | # Two README files were added. |
---|
| 213 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 214 | # |
---|
[802] | 215 | # Revision 1.9 2003/02/17 22:55:01 jalet |
---|
| 216 | # More options can now be set per printer or globally : |
---|
| 217 | # |
---|
[873] | 218 | # admin |
---|
| 219 | # adminmail |
---|
| 220 | # gracedelay |
---|
| 221 | # requester |
---|
[802] | 222 | # |
---|
| 223 | # the printer option has priority when both are defined. |
---|
| 224 | # |
---|
[800] | 225 | # Revision 1.8 2003/02/17 22:05:50 jalet |
---|
| 226 | # Storage backend now supports admin and user passwords (untested) |
---|
| 227 | # |
---|
[791] | 228 | # Revision 1.7 2003/02/10 12:07:31 jalet |
---|
| 229 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 230 | # |
---|
[773] | 231 | # Revision 1.6 2003/02/09 13:05:43 jalet |
---|
| 232 | # Internationalization continues... |
---|
| 233 | # |
---|
[768] | 234 | # Revision 1.5 2003/02/08 22:39:46 jalet |
---|
| 235 | # --reset command line option added |
---|
| 236 | # |
---|
[759] | 237 | # Revision 1.4 2003/02/08 09:59:59 jalet |
---|
| 238 | # Added preliminary base class for all storages |
---|
| 239 | # |
---|
[698] | 240 | # Revision 1.3 2003/02/05 22:10:29 jalet |
---|
| 241 | # Typos |
---|
| 242 | # |
---|
[697] | 243 | # Revision 1.2 2003/02/05 22:02:22 jalet |
---|
| 244 | # __import__ statement didn't work as expected |
---|
| 245 | # |
---|
[695] | 246 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 247 | # Initial import into CVS |
---|
| 248 | # |
---|
| 249 | # |
---|
| 250 | # |
---|
| 251 | |
---|
| 252 | class PyKotaStorageError(Exception): |
---|
| 253 | """An exception for Quota Storage related stuff.""" |
---|
| 254 | def __init__(self, message = ""): |
---|
| 255 | self.message = message |
---|
| 256 | Exception.__init__(self, message) |
---|
| 257 | def __repr__(self): |
---|
| 258 | return self.message |
---|
| 259 | __str__ = __repr__ |
---|
[759] | 260 | |
---|
[1041] | 261 | class StorageObject : |
---|
| 262 | """Object present in the Quota Storage.""" |
---|
| 263 | def __init__(self, parent) : |
---|
| 264 | "Initialize minimal data.""" |
---|
| 265 | self.parent = parent |
---|
| 266 | self.ident = None |
---|
| 267 | self.Exists = 0 |
---|
| 268 | |
---|
| 269 | class StorageUser(StorageObject) : |
---|
| 270 | """User class.""" |
---|
| 271 | def __init__(self, parent, name) : |
---|
| 272 | StorageObject.__init__(self, parent) |
---|
| 273 | self.Name = name |
---|
| 274 | self.LimitBy = None |
---|
| 275 | self.AccountBalance = None |
---|
| 276 | self.LifeTimePaid = None |
---|
[1067] | 277 | self.Email = None |
---|
[1522] | 278 | self.Payments = [] # TODO : maybe handle this smartly for SQL, for now just don't retrieve them |
---|
[1041] | 279 | |
---|
| 280 | def consumeAccountBalance(self, amount) : |
---|
| 281 | """Consumes an amount of money from the user's account balance.""" |
---|
[1269] | 282 | self.parent.decreaseUserAccountBalance(self, amount) |
---|
| 283 | self.AccountBalance = float(self.AccountBalance or 0.0) - amount |
---|
[1041] | 284 | |
---|
| 285 | def setAccountBalance(self, balance, lifetimepaid) : |
---|
| 286 | """Sets the user's account balance in case he pays more money.""" |
---|
[1522] | 287 | diff = float(lifetimepaid or 0.0) - float(self.LifeTimePaid or 0.0) |
---|
| 288 | self.parent.beginTransaction() |
---|
| 289 | try : |
---|
| 290 | self.parent.writeUserAccountBalance(self, balance, lifetimepaid) |
---|
| 291 | self.parent.writeNewPayment(self, diff) |
---|
| 292 | except PyKotaStorageError, msg : |
---|
| 293 | self.parent.rollbackTransaction() |
---|
| 294 | raise PyKotaStorageError, msg |
---|
| 295 | else : |
---|
| 296 | self.parent.commitTransaction() |
---|
| 297 | self.AccountBalance = balance |
---|
| 298 | self.LifeTimePaid = lifetimepaid |
---|
[1041] | 299 | |
---|
| 300 | def setLimitBy(self, limitby) : |
---|
| 301 | """Sets the user's limiting factor.""" |
---|
[1062] | 302 | try : |
---|
| 303 | limitby = limitby.lower() |
---|
| 304 | except AttributeError : |
---|
| 305 | limitby = "quota" |
---|
[1418] | 306 | if limitby in ["quota", "balance", "quota-then-balance", "balance-then-quota"] : |
---|
[1041] | 307 | self.parent.writeUserLimitBy(self, limitby) |
---|
| 308 | self.LimitBy = limitby |
---|
| 309 | |
---|
| 310 | def delete(self) : |
---|
| 311 | """Deletes an user from the Quota Storage.""" |
---|
| 312 | self.parent.beginTransaction() |
---|
| 313 | try : |
---|
| 314 | self.parent.deleteUser(self) |
---|
| 315 | except PyKotaStorageError, msg : |
---|
| 316 | self.parent.rollbackTransaction() |
---|
| 317 | raise PyKotaStorageError, msg |
---|
| 318 | else : |
---|
| 319 | self.parent.commitTransaction() |
---|
| 320 | |
---|
| 321 | class StorageGroup(StorageObject) : |
---|
| 322 | """User class.""" |
---|
| 323 | def __init__(self, parent, name) : |
---|
| 324 | StorageObject.__init__(self, parent) |
---|
| 325 | self.Name = name |
---|
| 326 | self.LimitBy = None |
---|
| 327 | self.AccountBalance = None |
---|
| 328 | self.LifeTimePaid = None |
---|
| 329 | |
---|
| 330 | def setLimitBy(self, limitby) : |
---|
| 331 | """Sets the user's limiting factor.""" |
---|
[1062] | 332 | try : |
---|
| 333 | limitby = limitby.lower() |
---|
| 334 | except AttributeError : |
---|
| 335 | limitby = "quota" |
---|
[1041] | 336 | if limitby in ["quota", "balance"] : |
---|
| 337 | self.parent.writeGroupLimitBy(self, limitby) |
---|
| 338 | self.LimitBy = limitby |
---|
| 339 | |
---|
| 340 | def delete(self) : |
---|
| 341 | """Deletes a group from the Quota Storage.""" |
---|
| 342 | self.parent.beginTransaction() |
---|
| 343 | try : |
---|
| 344 | self.parent.deleteGroup(self) |
---|
| 345 | except PyKotaStorageError, msg : |
---|
| 346 | self.parent.rollbackTransaction() |
---|
| 347 | raise PyKotaStorageError, msg |
---|
| 348 | else : |
---|
| 349 | self.parent.commitTransaction() |
---|
| 350 | |
---|
| 351 | class StoragePrinter(StorageObject) : |
---|
| 352 | """Printer class.""" |
---|
| 353 | def __init__(self, parent, name) : |
---|
| 354 | StorageObject.__init__(self, parent) |
---|
| 355 | self.Name = name |
---|
| 356 | self.PricePerPage = None |
---|
| 357 | self.PricePerJob = None |
---|
[1582] | 358 | self.Description = None |
---|
[1041] | 359 | |
---|
[1358] | 360 | def __getattr__(self, name) : |
---|
| 361 | """Delays data retrieval until it's really needed.""" |
---|
| 362 | if name == "LastJob" : |
---|
| 363 | self.LastJob = self.parent.getPrinterLastJob(self) |
---|
| 364 | return self.LastJob |
---|
| 365 | else : |
---|
| 366 | raise AttributeError, name |
---|
| 367 | |
---|
[1520] | 368 | def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None) : |
---|
[1041] | 369 | """Adds a job to the printer's history.""" |
---|
[1520] | 370 | self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options, clienthost, jobsizebytes) |
---|
[1041] | 371 | # TODO : update LastJob object ? Probably not needed. |
---|
| 372 | |
---|
[1258] | 373 | def addPrinterToGroup(self, printer) : |
---|
| 374 | """Adds a printer to a printer group.""" |
---|
[1259] | 375 | if (printer not in self.parent.getParentPrinters(self)) and (printer.ident != self.ident) : |
---|
[1258] | 376 | self.parent.writePrinterToGroup(self, printer) |
---|
[1269] | 377 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
[1332] | 378 | |
---|
| 379 | def delPrinterFromGroup(self, printer) : |
---|
| 380 | """Deletes a printer from a printer group.""" |
---|
[1333] | 381 | self.parent.removePrinterFromGroup(self, printer) |
---|
| 382 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
[1258] | 383 | |
---|
[1041] | 384 | def setPrices(self, priceperpage = None, priceperjob = None) : |
---|
| 385 | """Sets the printer's prices.""" |
---|
| 386 | if priceperpage is None : |
---|
[1711] | 387 | priceperpage = self.PricePerPage or 0.0 |
---|
[1041] | 388 | else : |
---|
| 389 | self.PricePerPage = float(priceperpage) |
---|
| 390 | if priceperjob is None : |
---|
[1711] | 391 | priceperjob = self.PricePerJob or 0.0 |
---|
[1041] | 392 | else : |
---|
| 393 | self.PricePerJob = float(priceperjob) |
---|
| 394 | self.parent.writePrinterPrices(self) |
---|
| 395 | |
---|
[1582] | 396 | def setDescription(self, description = None) : |
---|
| 397 | """Sets the printer's prices.""" |
---|
| 398 | if description is None : |
---|
| 399 | description = self.Description |
---|
| 400 | else : |
---|
| 401 | self.Description = str(description) |
---|
| 402 | self.parent.writePrinterDescription(self) |
---|
| 403 | |
---|
[1330] | 404 | def delete(self) : |
---|
| 405 | """Deletes a printer from the Quota Storage.""" |
---|
| 406 | self.parent.beginTransaction() |
---|
| 407 | try : |
---|
| 408 | self.parent.deletePrinter(self) |
---|
| 409 | except PyKotaStorageError, msg : |
---|
| 410 | self.parent.rollbackTransaction() |
---|
| 411 | raise PyKotaStorageError, msg |
---|
| 412 | else : |
---|
| 413 | self.parent.commitTransaction() |
---|
| 414 | |
---|
[1041] | 415 | class StorageUserPQuota(StorageObject) : |
---|
| 416 | """User Print Quota class.""" |
---|
| 417 | def __init__(self, parent, user, printer) : |
---|
| 418 | StorageObject.__init__(self, parent) |
---|
| 419 | self.User = user |
---|
| 420 | self.Printer = printer |
---|
| 421 | self.PageCounter = None |
---|
| 422 | self.LifePageCounter = None |
---|
| 423 | self.SoftLimit = None |
---|
| 424 | self.HardLimit = None |
---|
| 425 | self.DateLimit = None |
---|
| 426 | |
---|
[1358] | 427 | def __getattr__(self, name) : |
---|
| 428 | """Delays data retrieval until it's really needed.""" |
---|
| 429 | if name == "ParentPrintersUserPQuota" : |
---|
| 430 | self.ParentPrintersUserPQuota = (self.User.Exists and self.Printer.Exists and self.parent.getParentPrintersUserPQuota(self)) or [] |
---|
| 431 | return self.ParentPrintersUserPQuota |
---|
| 432 | else : |
---|
| 433 | raise AttributeError, name |
---|
| 434 | |
---|
[1041] | 435 | def setDateLimit(self, datelimit) : |
---|
| 436 | """Sets the date limit for this quota.""" |
---|
| 437 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 438 | self.parent.writeUserPQuotaDateLimit(self, date) |
---|
| 439 | self.DateLimit = date |
---|
| 440 | |
---|
| 441 | def setLimits(self, softlimit, hardlimit) : |
---|
| 442 | """Sets the soft and hard limit for this quota.""" |
---|
| 443 | self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit) |
---|
| 444 | self.SoftLimit = softlimit |
---|
| 445 | self.HardLimit = hardlimit |
---|
[1367] | 446 | self.DateLimit = None |
---|
[1041] | 447 | |
---|
[1967] | 448 | def setUsage(self, used) : |
---|
| 449 | """Sets the PageCounter and LifePageCounter to used, or if used is + or - prefixed, changes the values of {Life,}PageCounter by that amount.""" |
---|
| 450 | vused = int(used) |
---|
| 451 | if used.startswith("+") or used.startswith("-") : |
---|
[2030] | 452 | self.parent.beginTransaction() |
---|
| 453 | try : |
---|
| 454 | self.parent.increaseUserPQuotaPagesCounters(self, vused) |
---|
| 455 | self.parent.writeUserPQuotaDateLimit(self, None) |
---|
| 456 | except PyKotaStorageError, msg : |
---|
| 457 | self.parent.rollbackTransaction() |
---|
| 458 | raise PyKotaStorageError, msg |
---|
| 459 | else : |
---|
| 460 | self.parent.commitTransaction() |
---|
| 461 | self.PageCounter += vused |
---|
| 462 | self.LifePageCounter += vused |
---|
[1967] | 463 | else : |
---|
[2030] | 464 | self.parent.writeUserPQuotaPagesCounters(self, vused, vused) |
---|
| 465 | self.PageCounter = self.LifePageCounter = vused |
---|
| 466 | self.DateLimit = None |
---|
[1967] | 467 | |
---|
[1041] | 468 | def reset(self) : |
---|
| 469 | """Resets page counter to 0.""" |
---|
| 470 | self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0)) |
---|
| 471 | self.PageCounter = 0 |
---|
[2030] | 472 | self.DateLimit = None |
---|
[1041] | 473 | |
---|
[1755] | 474 | def hardreset(self) : |
---|
| 475 | """Resets actual and life time page counters to 0.""" |
---|
| 476 | self.parent.writeUserPQuotaPagesCounters(self, 0, 0) |
---|
| 477 | self.PageCounter = self.LifePageCounter = 0 |
---|
[2030] | 478 | self.DateLimit = None |
---|
[1755] | 479 | |
---|
[1285] | 480 | def computeJobPrice(self, jobsize) : |
---|
| 481 | """Computes the job price as the sum of all parent printers' prices + current printer's ones.""" |
---|
| 482 | totalprice = 0.0 |
---|
| 483 | if jobsize : |
---|
| 484 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
| 485 | price = (float(upq.Printer.PricePerPage or 0.0) * jobsize) + float(upq.Printer.PricePerJob or 0.0) |
---|
| 486 | totalprice += price |
---|
| 487 | return totalprice |
---|
| 488 | |
---|
| 489 | def increasePagesUsage(self, jobsize) : |
---|
[1041] | 490 | """Increase the value of used pages and money.""" |
---|
[1285] | 491 | jobprice = self.computeJobPrice(jobsize) |
---|
| 492 | if jobsize : |
---|
| 493 | self.parent.beginTransaction() |
---|
| 494 | try : |
---|
[1363] | 495 | if jobprice : |
---|
| 496 | self.User.consumeAccountBalance(jobprice) |
---|
[1240] | 497 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
[1285] | 498 | self.parent.increaseUserPQuotaPagesCounters(upq, jobsize) |
---|
| 499 | upq.PageCounter = int(upq.PageCounter or 0) + jobsize |
---|
| 500 | upq.LifePageCounter = int(upq.LifePageCounter or 0) + jobsize |
---|
| 501 | except PyKotaStorageError, msg : |
---|
| 502 | self.parent.rollbackTransaction() |
---|
| 503 | raise PyKotaStorageError, msg |
---|
| 504 | else : |
---|
| 505 | self.parent.commitTransaction() |
---|
| 506 | return jobprice |
---|
[1041] | 507 | |
---|
| 508 | class StorageGroupPQuota(StorageObject) : |
---|
| 509 | """Group Print Quota class.""" |
---|
| 510 | def __init__(self, parent, group, printer) : |
---|
| 511 | StorageObject.__init__(self, parent) |
---|
| 512 | self.Group = group |
---|
| 513 | self.Printer = printer |
---|
| 514 | self.PageCounter = None |
---|
| 515 | self.LifePageCounter = None |
---|
| 516 | self.SoftLimit = None |
---|
| 517 | self.HardLimit = None |
---|
| 518 | self.DateLimit = None |
---|
| 519 | |
---|
[1365] | 520 | def __getattr__(self, name) : |
---|
| 521 | """Delays data retrieval until it's really needed.""" |
---|
| 522 | if name == "ParentPrintersGroupPQuota" : |
---|
| 523 | self.ParentPrintersGroupPQuota = (self.Group.Exists and self.Printer.Exists and self.parent.getParentPrintersGroupPQuota(self)) or [] |
---|
| 524 | return self.ParentPrintersGroupPQuota |
---|
| 525 | else : |
---|
| 526 | raise AttributeError, name |
---|
| 527 | |
---|
[2030] | 528 | def reset(self) : |
---|
| 529 | """Resets page counter to 0.""" |
---|
| 530 | self.parent.beginTransaction() |
---|
| 531 | try : |
---|
| 532 | for user in self.parent.getGroupMembers(self.Group) : |
---|
| 533 | uq = self.parent.getUserPQuota(user, self.Printer) |
---|
| 534 | uq.reset() |
---|
| 535 | self.parent.writeGroupPQuotaDateLimit(self, None) |
---|
| 536 | except PyKotaStorageError, msg : |
---|
| 537 | self.parent.rollbackTransaction() |
---|
| 538 | raise PyKotaStorageError, msg |
---|
| 539 | else : |
---|
| 540 | self.parent.commitTransaction() |
---|
| 541 | self.PageCounter = 0 |
---|
| 542 | self.DateLimit = None |
---|
| 543 | |
---|
| 544 | def hardreset(self) : |
---|
| 545 | """Resets actual and life time page counters to 0.""" |
---|
| 546 | self.parent.beginTransaction() |
---|
| 547 | try : |
---|
| 548 | for user in self.parent.getGroupMembers(self.Group) : |
---|
| 549 | uq = self.parent.getUserPQuota(user, self.Printer) |
---|
| 550 | uq.hardreset() |
---|
| 551 | self.parent.writeGroupPQuotaDateLimit(self, None) |
---|
| 552 | except PyKotaStorageError, msg : |
---|
| 553 | self.parent.rollbackTransaction() |
---|
| 554 | raise PyKotaStorageError, msg |
---|
| 555 | else : |
---|
| 556 | self.parent.commitTransaction() |
---|
| 557 | self.PageCounter = self.LifePageCounter = 0 |
---|
| 558 | self.DateLimit = None |
---|
| 559 | |
---|
[1041] | 560 | def setDateLimit(self, datelimit) : |
---|
| 561 | """Sets the date limit for this quota.""" |
---|
| 562 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 563 | self.parent.writeGroupPQuotaDateLimit(self, date) |
---|
| 564 | self.DateLimit = date |
---|
| 565 | |
---|
| 566 | def setLimits(self, softlimit, hardlimit) : |
---|
| 567 | """Sets the soft and hard limit for this quota.""" |
---|
| 568 | self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit) |
---|
| 569 | self.SoftLimit = softlimit |
---|
| 570 | self.HardLimit = hardlimit |
---|
[1367] | 571 | self.DateLimit = None |
---|
[1041] | 572 | |
---|
[1274] | 573 | class StorageJob(StorageObject) : |
---|
[1692] | 574 | """Printer's Job class.""" |
---|
[1274] | 575 | def __init__(self, parent) : |
---|
[1041] | 576 | StorageObject.__init__(self, parent) |
---|
[1358] | 577 | self.UserName = None |
---|
| 578 | self.PrinterName = None |
---|
[1041] | 579 | self.JobId = None |
---|
| 580 | self.PrinterPageCounter = None |
---|
[1520] | 581 | self.JobSizeBytes = None |
---|
[1041] | 582 | self.JobSize = None |
---|
| 583 | self.JobAction = None |
---|
| 584 | self.JobDate = None |
---|
[1203] | 585 | self.JobPrice = None |
---|
| 586 | self.JobFileName = None |
---|
| 587 | self.JobTitle = None |
---|
| 588 | self.JobCopies = None |
---|
| 589 | self.JobOptions = None |
---|
[1502] | 590 | self.JobHostName = None |
---|
[1041] | 591 | |
---|
[1358] | 592 | def __getattr__(self, name) : |
---|
| 593 | """Delays data retrieval until it's really needed.""" |
---|
| 594 | if name == "User" : |
---|
| 595 | self.User = self.parent.getUser(self.UserName) |
---|
| 596 | return self.User |
---|
| 597 | elif name == "Printer" : |
---|
| 598 | self.Printer = self.parent.getPrinter(self.PrinterName) |
---|
| 599 | return self.Printer |
---|
| 600 | else : |
---|
| 601 | raise AttributeError, name |
---|
| 602 | |
---|
[1274] | 603 | class StorageLastJob(StorageJob) : |
---|
| 604 | """Printer's Last Job class.""" |
---|
| 605 | def __init__(self, parent, printer) : |
---|
| 606 | StorageJob.__init__(self, parent) |
---|
[1359] | 607 | self.PrinterName = printer.Name # not needed |
---|
[1274] | 608 | self.Printer = printer |
---|
| 609 | |
---|
[1130] | 610 | class BaseStorage : |
---|
| 611 | def __init__(self, pykotatool) : |
---|
[1523] | 612 | """Opens the storage connection.""" |
---|
[1130] | 613 | self.closed = 1 |
---|
| 614 | self.tool = pykotatool |
---|
| 615 | self.usecache = pykotatool.config.getCaching() |
---|
[1148] | 616 | self.disablehistory = pykotatool.config.getDisableHistory() |
---|
[1875] | 617 | self.privacy = pykotatool.config.getPrivacy() |
---|
| 618 | if self.privacy : |
---|
| 619 | pykotatool.logdebug("Jobs' title, filename and options will be hidden because of privacy concerns.") |
---|
[1130] | 620 | if self.usecache : |
---|
| 621 | self.tool.logdebug("Caching enabled.") |
---|
[1252] | 622 | self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} } |
---|
[1130] | 623 | |
---|
[1240] | 624 | def close(self) : |
---|
| 625 | """Must be overriden in children classes.""" |
---|
| 626 | raise RuntimeError, "BaseStorage.close() must be overriden !" |
---|
| 627 | |
---|
[1130] | 628 | def __del__(self) : |
---|
| 629 | """Ensures that the database connection is closed.""" |
---|
| 630 | self.close() |
---|
| 631 | |
---|
| 632 | def getFromCache(self, cachetype, key) : |
---|
| 633 | """Tries to extract something from the cache.""" |
---|
| 634 | if self.usecache : |
---|
| 635 | entry = self.caches[cachetype].get(key) |
---|
| 636 | if entry is not None : |
---|
| 637 | self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key)) |
---|
| 638 | else : |
---|
| 639 | self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key)) |
---|
[1743] | 640 | return entry |
---|
[1130] | 641 | |
---|
| 642 | def cacheEntry(self, cachetype, key, value) : |
---|
| 643 | """Puts an entry in the cache.""" |
---|
[1151] | 644 | if self.usecache and getattr(value, "Exists", 0) : |
---|
[1130] | 645 | self.caches[cachetype][key] = value |
---|
[1132] | 646 | self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key)) |
---|
[1130] | 647 | |
---|
| 648 | def getUser(self, username) : |
---|
| 649 | """Returns the user from cache.""" |
---|
| 650 | user = self.getFromCache("USERS", username) |
---|
| 651 | if user is None : |
---|
| 652 | user = self.getUserFromBackend(username) |
---|
| 653 | self.cacheEntry("USERS", username, user) |
---|
| 654 | return user |
---|
| 655 | |
---|
| 656 | def getGroup(self, groupname) : |
---|
| 657 | """Returns the group from cache.""" |
---|
| 658 | group = self.getFromCache("GROUPS", groupname) |
---|
| 659 | if group is None : |
---|
| 660 | group = self.getGroupFromBackend(groupname) |
---|
| 661 | self.cacheEntry("GROUPS", groupname, group) |
---|
| 662 | return group |
---|
| 663 | |
---|
| 664 | def getPrinter(self, printername) : |
---|
| 665 | """Returns the printer from cache.""" |
---|
| 666 | printer = self.getFromCache("PRINTERS", printername) |
---|
| 667 | if printer is None : |
---|
| 668 | printer = self.getPrinterFromBackend(printername) |
---|
| 669 | self.cacheEntry("PRINTERS", printername, printer) |
---|
| 670 | return printer |
---|
| 671 | |
---|
| 672 | def getUserPQuota(self, user, printer) : |
---|
| 673 | """Returns the user quota information from cache.""" |
---|
| 674 | useratprinter = "%s@%s" % (user.Name, printer.Name) |
---|
| 675 | upquota = self.getFromCache("USERPQUOTAS", useratprinter) |
---|
| 676 | if upquota is None : |
---|
| 677 | upquota = self.getUserPQuotaFromBackend(user, printer) |
---|
| 678 | self.cacheEntry("USERPQUOTAS", useratprinter, upquota) |
---|
| 679 | return upquota |
---|
| 680 | |
---|
| 681 | def getGroupPQuota(self, group, printer) : |
---|
| 682 | """Returns the group quota information from cache.""" |
---|
| 683 | groupatprinter = "%s@%s" % (group.Name, printer.Name) |
---|
| 684 | gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter) |
---|
| 685 | if gpquota is None : |
---|
| 686 | gpquota = self.getGroupPQuotaFromBackend(group, printer) |
---|
| 687 | self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota) |
---|
| 688 | return gpquota |
---|
| 689 | |
---|
| 690 | def getPrinterLastJob(self, printer) : |
---|
| 691 | """Extracts last job information for a given printer from cache.""" |
---|
| 692 | lastjob = self.getFromCache("LASTJOBS", printer.Name) |
---|
| 693 | if lastjob is None : |
---|
| 694 | lastjob = self.getPrinterLastJobFromBackend(printer) |
---|
| 695 | self.cacheEntry("LASTJOBS", printer.Name, lastjob) |
---|
| 696 | return lastjob |
---|
| 697 | |
---|
[1249] | 698 | def getParentPrinters(self, printer) : |
---|
| 699 | """Extracts parent printers information for a given printer from cache.""" |
---|
[1252] | 700 | if self.usecache : |
---|
| 701 | if not hasattr(printer, "Parents") : |
---|
| 702 | self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name) |
---|
| 703 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
| 704 | self.tool.logdebug("Cache store (%s->Parents)" % printer.Name) |
---|
| 705 | else : |
---|
| 706 | self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name) |
---|
| 707 | else : |
---|
| 708 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
[1364] | 709 | for parent in printer.Parents[:] : |
---|
| 710 | printer.Parents.extend(self.getParentPrinters(parent)) |
---|
[1368] | 711 | uniquedic = {} |
---|
| 712 | for parent in printer.Parents : |
---|
| 713 | uniquedic[parent.Name] = parent |
---|
| 714 | printer.Parents = uniquedic.values() |
---|
[1252] | 715 | return printer.Parents |
---|
[1249] | 716 | |
---|
[1137] | 717 | def getGroupMembers(self, group) : |
---|
| 718 | """Returns the group's members list from in-group cache.""" |
---|
| 719 | if self.usecache : |
---|
| 720 | if not hasattr(group, "Members") : |
---|
| 721 | self.tool.logdebug("Cache miss (%s->Members)" % group.Name) |
---|
| 722 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 723 | self.tool.logdebug("Cache store (%s->Members)" % group.Name) |
---|
| 724 | else : |
---|
| 725 | self.tool.logdebug("Cache hit (%s->Members)" % group.Name) |
---|
| 726 | else : |
---|
| 727 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 728 | return group.Members |
---|
| 729 | |
---|
| 730 | def getUserGroups(self, user) : |
---|
| 731 | """Returns the user's groups list from in-user cache.""" |
---|
| 732 | if self.usecache : |
---|
| 733 | if not hasattr(user, "Groups") : |
---|
| 734 | self.tool.logdebug("Cache miss (%s->Groups)" % user.Name) |
---|
| 735 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 736 | self.tool.logdebug("Cache store (%s->Groups)" % user.Name) |
---|
| 737 | else : |
---|
| 738 | self.tool.logdebug("Cache hit (%s->Groups)" % user.Name) |
---|
| 739 | else : |
---|
| 740 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 741 | return user.Groups |
---|
| 742 | |
---|
[1240] | 743 | def getParentPrintersUserPQuota(self, userpquota) : |
---|
[1364] | 744 | """Returns all user print quota on the printer and all its parents recursively.""" |
---|
[1365] | 745 | upquotas = [ ] |
---|
[1240] | 746 | for printer in self.getParentPrinters(userpquota.Printer) : |
---|
[1395] | 747 | upq = self.getUserPQuota(userpquota.User, printer) |
---|
| 748 | if upq.Exists : |
---|
| 749 | upquotas.append(upq) |
---|
[1240] | 750 | return upquotas |
---|
| 751 | |
---|
[1365] | 752 | def getParentPrintersGroupPQuota(self, grouppquota) : |
---|
| 753 | """Returns all group print quota on the printer and all its parents recursively.""" |
---|
| 754 | gpquotas = [ ] |
---|
| 755 | for printer in self.getParentPrinters(grouppquota.Printer) : |
---|
[1395] | 756 | gpq = self.getGroupPQuota(grouppquota.Group, printer) |
---|
| 757 | if gpq.Exists : |
---|
| 758 | gpquotas.append(gpq) |
---|
[1365] | 759 | return gpquotas |
---|
| 760 | |
---|
[1790] | 761 | def databaseToUserCharset(self, text) : |
---|
| 762 | """Converts from database format (UTF-8) to user's charset.""" |
---|
| 763 | if text is not None : |
---|
| 764 | try : |
---|
| 765 | return unicode(text, "UTF-8").encode(self.tool.getCharset()) |
---|
| 766 | except UnicodeError : |
---|
[1791] | 767 | try : |
---|
| 768 | # Incorrect locale settings ? |
---|
| 769 | return unicode(text, "UTF-8").encode("ISO-8859-15") |
---|
| 770 | except UnicodeError : |
---|
| 771 | pass |
---|
[1790] | 772 | return text |
---|
| 773 | |
---|
| 774 | def userCharsetToDatabase(self, text) : |
---|
| 775 | """Converts from user's charset to database format (UTF-8).""" |
---|
| 776 | if text is not None : |
---|
| 777 | try : |
---|
| 778 | return unicode(text, self.tool.getCharset()).encode("UTF-8") |
---|
| 779 | except UnicodeError : |
---|
[1791] | 780 | try : |
---|
| 781 | # Incorrect locale settings ? |
---|
| 782 | return unicode(text, "ISO-8859-15").encode("UTF-8") |
---|
| 783 | except UnicodeError : |
---|
| 784 | pass |
---|
[1790] | 785 | return text |
---|
| 786 | |
---|
[1021] | 787 | def openConnection(pykotatool) : |
---|
[695] | 788 | """Returns a connection handle to the appropriate Quota Storage Database.""" |
---|
[1021] | 789 | backendinfo = pykotatool.config.getStorageBackend() |
---|
[800] | 790 | backend = backendinfo["storagebackend"] |
---|
[695] | 791 | try : |
---|
[1219] | 792 | exec "from pykota.storages import %s as storagebackend" % backend.lower() |
---|
[695] | 793 | except ImportError : |
---|
[773] | 794 | raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend |
---|
[695] | 795 | else : |
---|
[800] | 796 | host = backendinfo["storageserver"] |
---|
| 797 | database = backendinfo["storagename"] |
---|
[1087] | 798 | admin = backendinfo["storageadmin"] or backendinfo["storageuser"] |
---|
| 799 | adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"] |
---|
[1240] | 800 | return storagebackend.Storage(pykotatool, host, database, admin, adminpw) |
---|