[1016] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1016] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[2622] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[1016] | 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 |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1016] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2107] | 23 | # |
---|
[1016] | 24 | |
---|
| 25 | # |
---|
| 26 | # My IANA assigned number, for |
---|
| 27 | # "Conseil Internet & Logiciels Libres, J�me Alet" |
---|
| 28 | # is 16868. Use this as a base to create the LDAP schema. |
---|
| 29 | # |
---|
| 30 | |
---|
[2221] | 31 | import sys |
---|
[1240] | 32 | import types |
---|
[1030] | 33 | import time |
---|
| 34 | import md5 |
---|
[2460] | 35 | import base64 |
---|
[2679] | 36 | import random |
---|
| 37 | |
---|
[1522] | 38 | from mx import DateTime |
---|
[1016] | 39 | |
---|
[2830] | 40 | from pykota.storage import PyKotaStorageError, BaseStorage, \ |
---|
[2380] | 41 | StorageUser, StorageGroup, StoragePrinter, \ |
---|
| 42 | StorageJob, StorageLastJob, StorageUserPQuota, \ |
---|
| 43 | StorageGroupPQuota, StorageBillingCode |
---|
[1016] | 44 | |
---|
| 45 | try : |
---|
| 46 | import ldap |
---|
[1356] | 47 | import ldap.modlist |
---|
[1016] | 48 | except ImportError : |
---|
| 49 | raise PyKotaStorageError, "This python version (%s) doesn't seem to have the python-ldap module installed correctly." % sys.version.split()[0] |
---|
[2221] | 50 | else : |
---|
| 51 | try : |
---|
| 52 | from ldap.cidict import cidict |
---|
| 53 | except ImportError : |
---|
| 54 | import UserDict |
---|
| 55 | sys.stderr.write("ERROR: PyKota requires a newer version of python-ldap. Workaround activated. Please upgrade python-ldap !\n") |
---|
| 56 | class cidict(UserDict.UserDict) : |
---|
| 57 | pass # Fake it all, and don't care for case insensitivity : users who need it will have to upgrade. |
---|
[1016] | 58 | |
---|
[1130] | 59 | class Storage(BaseStorage) : |
---|
[1021] | 60 | def __init__(self, pykotatool, host, dbname, user, passwd) : |
---|
[1016] | 61 | """Opens the LDAP connection.""" |
---|
[1966] | 62 | self.savedtool = pykotatool |
---|
| 63 | self.savedhost = host |
---|
| 64 | self.saveddbname = dbname |
---|
| 65 | self.saveduser = user |
---|
| 66 | self.savedpasswd = passwd |
---|
| 67 | self.secondStageInit() |
---|
| 68 | |
---|
| 69 | def secondStageInit(self) : |
---|
| 70 | """Second stage initialisation.""" |
---|
| 71 | BaseStorage.__init__(self, self.savedtool) |
---|
| 72 | self.info = self.tool.config.getLDAPInfo() |
---|
| 73 | message = "" |
---|
| 74 | for tryit in range(3) : |
---|
| 75 | try : |
---|
[2418] | 76 | self.tool.logdebug("Trying to open database (host=%s, dbname=%s, user=%s)..." % (self.savedhost, self.saveddbname, self.saveduser)) |
---|
[1966] | 77 | self.database = ldap.initialize(self.savedhost) |
---|
[1968] | 78 | if self.info["ldaptls"] : |
---|
| 79 | # we want TLS |
---|
| 80 | ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, self.info["cacert"]) |
---|
| 81 | self.database.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND) |
---|
| 82 | self.database.start_tls_s() |
---|
[1966] | 83 | self.database.simple_bind_s(self.saveduser, self.savedpasswd) |
---|
| 84 | self.basedn = self.saveddbname |
---|
| 85 | except ldap.SERVER_DOWN : |
---|
| 86 | message = "LDAP backend for PyKota seems to be down !" |
---|
| 87 | self.tool.printInfo("%s" % message, "error") |
---|
| 88 | self.tool.printInfo("Trying again in 2 seconds...", "warn") |
---|
| 89 | time.sleep(2) |
---|
| 90 | except ldap.LDAPError : |
---|
| 91 | message = "Unable to connect to LDAP server %s as %s." % (self.savedhost, self.saveduser) |
---|
| 92 | self.tool.printInfo("%s" % message, "error") |
---|
| 93 | self.tool.printInfo("Trying again in 2 seconds...", "warn") |
---|
| 94 | time.sleep(2) |
---|
| 95 | else : |
---|
| 96 | self.useldapcache = self.tool.config.getLDAPCache() |
---|
| 97 | if self.useldapcache : |
---|
| 98 | self.tool.logdebug("Low-Level LDAP Caching enabled.") |
---|
| 99 | self.ldapcache = {} # low-level cache specific to LDAP backend |
---|
| 100 | self.closed = 0 |
---|
| 101 | self.tool.logdebug("Database opened (host=%s, dbname=%s, user=%s)" % (self.savedhost, self.saveddbname, self.saveduser)) |
---|
| 102 | return # All is fine here. |
---|
| 103 | raise PyKotaStorageError, message |
---|
[1016] | 104 | |
---|
[1113] | 105 | def close(self) : |
---|
[1016] | 106 | """Closes the database connection.""" |
---|
| 107 | if not self.closed : |
---|
[1170] | 108 | self.database.unbind_s() |
---|
[1016] | 109 | self.closed = 1 |
---|
[1130] | 110 | self.tool.logdebug("Database closed.") |
---|
[1016] | 111 | |
---|
[1030] | 112 | def genUUID(self) : |
---|
| 113 | """Generates an unique identifier. |
---|
| 114 | |
---|
| 115 | TODO : this one is not unique accross several print servers, but should be sufficient for testing. |
---|
| 116 | """ |
---|
[2680] | 117 | return md5.md5("%s-%s" % (time.time(), random.random())).hexdigest() |
---|
[1030] | 118 | |
---|
[1240] | 119 | def normalizeFields(self, fields) : |
---|
| 120 | """Ensure all items are lists.""" |
---|
| 121 | for (k, v) in fields.items() : |
---|
| 122 | if type(v) not in (types.TupleType, types.ListType) : |
---|
| 123 | if not v : |
---|
| 124 | del fields[k] |
---|
| 125 | else : |
---|
| 126 | fields[k] = [ v ] |
---|
| 127 | return fields |
---|
| 128 | |
---|
[1041] | 129 | def beginTransaction(self) : |
---|
| 130 | """Starts a transaction.""" |
---|
[1130] | 131 | self.tool.logdebug("Transaction begins... WARNING : No transactions in LDAP !") |
---|
[1041] | 132 | |
---|
| 133 | def commitTransaction(self) : |
---|
| 134 | """Commits a transaction.""" |
---|
[1130] | 135 | self.tool.logdebug("Transaction committed. WARNING : No transactions in LDAP !") |
---|
[1041] | 136 | |
---|
| 137 | def rollbackTransaction(self) : |
---|
| 138 | """Rollbacks a transaction.""" |
---|
[1130] | 139 | self.tool.logdebug("Transaction aborted. WARNING : No transaction in LDAP !") |
---|
[1041] | 140 | |
---|
[1356] | 141 | def doSearch(self, key, fields=None, base="", scope=ldap.SCOPE_SUBTREE, flushcache=0) : |
---|
[1016] | 142 | """Does an LDAP search query.""" |
---|
[1966] | 143 | message = "" |
---|
| 144 | for tryit in range(3) : |
---|
| 145 | try : |
---|
| 146 | base = base or self.basedn |
---|
| 147 | if self.useldapcache : |
---|
| 148 | # Here we overwrite the fields the app want, to try and |
---|
| 149 | # retrieve ALL user defined attributes ("*") |
---|
| 150 | # + the createTimestamp attribute, needed by job history |
---|
| 151 | # |
---|
| 152 | # This may not work with all LDAP servers |
---|
| 153 | # but works at least in OpenLDAP (2.1.25) |
---|
| 154 | # and iPlanet Directory Server (5.1 SP3) |
---|
| 155 | fields = ["*", "createTimestamp"] |
---|
| 156 | |
---|
| 157 | if self.useldapcache and (not flushcache) and (scope == ldap.SCOPE_BASE) and self.ldapcache.has_key(base) : |
---|
| 158 | entry = self.ldapcache[base] |
---|
| 159 | self.tool.logdebug("LDAP cache hit %s => %s" % (base, entry)) |
---|
| 160 | result = [(base, entry)] |
---|
| 161 | else : |
---|
| 162 | self.tool.logdebug("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields)) |
---|
| 163 | result = self.database.search_s(base, scope, key, fields) |
---|
| 164 | except ldap.NO_SUCH_OBJECT, msg : |
---|
| 165 | raise PyKotaStorageError, (_("Search base %s doesn't seem to exist. Probable misconfiguration. Please double check /etc/pykota/pykota.conf : %s") % (base, msg)) |
---|
| 166 | except ldap.LDAPError, msg : |
---|
| 167 | message = (_("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope)) + " : %s" % str(msg) |
---|
| 168 | self.tool.printInfo("LDAP error : %s" % message, "error") |
---|
| 169 | self.tool.printInfo("LDAP connection will be closed and reopened.", "warn") |
---|
| 170 | self.close() |
---|
| 171 | self.secondStageInit() |
---|
| 172 | else : |
---|
| 173 | self.tool.logdebug("QUERY : Result : %s" % result) |
---|
[2221] | 174 | result = [ (dn, cidict(attrs)) for (dn, attrs) in result ] |
---|
[1966] | 175 | if self.useldapcache : |
---|
| 176 | for (dn, attributes) in result : |
---|
| 177 | self.tool.logdebug("LDAP cache store %s => %s" % (dn, attributes)) |
---|
| 178 | self.ldapcache[dn] = attributes |
---|
| 179 | return result |
---|
| 180 | raise PyKotaStorageError, message |
---|
[1030] | 181 | |
---|
| 182 | def doAdd(self, dn, fields) : |
---|
| 183 | """Adds an entry in the LDAP directory.""" |
---|
[2221] | 184 | fields = self.normalizeFields(cidict(fields)) |
---|
[1966] | 185 | message = "" |
---|
| 186 | for tryit in range(3) : |
---|
| 187 | try : |
---|
| 188 | self.tool.logdebug("QUERY : ADD(%s, %s)" % (dn, str(fields))) |
---|
| 189 | entry = ldap.modlist.addModlist(fields) |
---|
| 190 | self.tool.logdebug("%s" % entry) |
---|
| 191 | self.database.add_s(dn, entry) |
---|
[2749] | 192 | except ldap.ALREADY_EXISTS, msg : |
---|
[2751] | 193 | raise PyKotaStorageError, "Entry %s already exists : %s" % (dn, str(msg)) |
---|
[1966] | 194 | except ldap.LDAPError, msg : |
---|
| 195 | message = (_("Problem adding LDAP entry (%s, %s)") % (dn, str(fields))) + " : %s" % str(msg) |
---|
| 196 | self.tool.printInfo("LDAP error : %s" % message, "error") |
---|
| 197 | self.tool.printInfo("LDAP connection will be closed and reopened.", "warn") |
---|
| 198 | self.close() |
---|
| 199 | self.secondStageInit() |
---|
| 200 | else : |
---|
| 201 | if self.useldapcache : |
---|
| 202 | self.tool.logdebug("LDAP cache add %s => %s" % (dn, fields)) |
---|
| 203 | self.ldapcache[dn] = fields |
---|
| 204 | return dn |
---|
| 205 | raise PyKotaStorageError, message |
---|
[1030] | 206 | |
---|
[1041] | 207 | def doDelete(self, dn) : |
---|
| 208 | """Deletes an entry from the LDAP directory.""" |
---|
[1966] | 209 | message = "" |
---|
| 210 | for tryit in range(3) : |
---|
| 211 | try : |
---|
| 212 | self.tool.logdebug("QUERY : Delete(%s)" % dn) |
---|
| 213 | self.database.delete_s(dn) |
---|
[2751] | 214 | except ldap.NO_SUCH_OBJECT : |
---|
| 215 | self.tool.printInfo("Entry %s was already missing before we deleted it. This **MAY** be normal." % dn, "info") |
---|
[1966] | 216 | except ldap.LDAPError, msg : |
---|
| 217 | message = (_("Problem deleting LDAP entry (%s)") % dn) + " : %s" % str(msg) |
---|
| 218 | self.tool.printInfo("LDAP error : %s" % message, "error") |
---|
| 219 | self.tool.printInfo("LDAP connection will be closed and reopened.", "warn") |
---|
| 220 | self.close() |
---|
| 221 | self.secondStageInit() |
---|
| 222 | else : |
---|
| 223 | if self.useldapcache : |
---|
| 224 | try : |
---|
| 225 | self.tool.logdebug("LDAP cache del %s" % dn) |
---|
| 226 | del self.ldapcache[dn] |
---|
| 227 | except KeyError : |
---|
| 228 | pass |
---|
| 229 | return |
---|
| 230 | raise PyKotaStorageError, message |
---|
[1041] | 231 | |
---|
[1356] | 232 | def doModify(self, dn, fields, ignoreold=1, flushcache=0) : |
---|
[1030] | 233 | """Modifies an entry in the LDAP directory.""" |
---|
[2221] | 234 | fields = cidict(fields) |
---|
[1966] | 235 | for tryit in range(3) : |
---|
| 236 | try : |
---|
| 237 | # TODO : take care of, and update LDAP specific cache |
---|
| 238 | if self.useldapcache and not flushcache : |
---|
| 239 | if self.ldapcache.has_key(dn) : |
---|
| 240 | old = self.ldapcache[dn] |
---|
| 241 | self.tool.logdebug("LDAP cache hit %s => %s" % (dn, old)) |
---|
| 242 | oldentry = {} |
---|
| 243 | for (k, v) in old.items() : |
---|
| 244 | if k != "createTimestamp" : |
---|
| 245 | oldentry[k] = v |
---|
[1269] | 246 | else : |
---|
[1966] | 247 | self.tool.logdebug("LDAP cache miss %s" % dn) |
---|
| 248 | oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE)[0][1] |
---|
| 249 | else : |
---|
| 250 | oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE, flushcache=flushcache)[0][1] |
---|
| 251 | for (k, v) in fields.items() : |
---|
| 252 | if type(v) == type({}) : |
---|
| 253 | try : |
---|
| 254 | oldvalue = v["convert"](oldentry.get(k, [0])[0]) |
---|
| 255 | except ValueError : |
---|
| 256 | self.tool.logdebug("Error converting %s with %s(%s)" % (oldentry.get(k), k, v)) |
---|
| 257 | oldvalue = 0 |
---|
| 258 | if v["operator"] == '+' : |
---|
| 259 | newvalue = oldvalue + v["value"] |
---|
| 260 | else : |
---|
| 261 | newvalue = oldvalue - v["value"] |
---|
| 262 | fields[k] = str(newvalue) |
---|
| 263 | fields = self.normalizeFields(fields) |
---|
| 264 | self.tool.logdebug("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry, fields)) |
---|
| 265 | entry = ldap.modlist.modifyModlist(oldentry, fields, ignore_oldexistent=ignoreold) |
---|
| 266 | modentry = [] |
---|
[1356] | 267 | for (mop, mtyp, mval) in entry : |
---|
[2191] | 268 | if mtyp and (mtyp.lower() != "createtimestamp") : |
---|
[1966] | 269 | modentry.append((mop, mtyp, mval)) |
---|
| 270 | self.tool.logdebug("MODIFY : %s ==> %s ==> %s" % (fields, entry, modentry)) |
---|
| 271 | if modentry : |
---|
| 272 | self.database.modify_s(dn, modentry) |
---|
| 273 | except ldap.LDAPError, msg : |
---|
| 274 | message = (_("Problem modifying LDAP entry (%s, %s)") % (dn, fields)) + " : %s" % str(msg) |
---|
| 275 | self.tool.printInfo("LDAP error : %s" % message, "error") |
---|
| 276 | self.tool.printInfo("LDAP connection will be closed and reopened.", "warn") |
---|
| 277 | self.close() |
---|
| 278 | self.secondStageInit() |
---|
| 279 | else : |
---|
| 280 | if self.useldapcache : |
---|
| 281 | cachedentry = self.ldapcache[dn] |
---|
| 282 | for (mop, mtyp, mval) in entry : |
---|
| 283 | if mop in (ldap.MOD_ADD, ldap.MOD_REPLACE) : |
---|
| 284 | cachedentry[mtyp] = mval |
---|
| 285 | else : |
---|
| 286 | try : |
---|
| 287 | del cachedentry[mtyp] |
---|
| 288 | except KeyError : |
---|
| 289 | pass |
---|
| 290 | self.tool.logdebug("LDAP cache update %s => %s" % (dn, cachedentry)) |
---|
| 291 | return dn |
---|
| 292 | raise PyKotaStorageError, message |
---|
[1016] | 293 | |
---|
[2652] | 294 | def filterNames(self, records, attribute, patterns=None) : |
---|
[2107] | 295 | """Returns a list of 'attribute' from a list of records. |
---|
| 296 | |
---|
| 297 | Logs any missing attribute. |
---|
| 298 | """ |
---|
| 299 | result = [] |
---|
[2191] | 300 | for (dn, record) in records : |
---|
| 301 | attrval = record.get(attribute, [None])[0] |
---|
[2107] | 302 | if attrval is None : |
---|
[2191] | 303 | self.tool.printInfo("Object %s has no %s attribute !" % (dn, attribute), "error") |
---|
[2652] | 304 | else : |
---|
[2653] | 305 | attrval = self.databaseToUserCharset(attrval) |
---|
[2652] | 306 | if patterns : |
---|
| 307 | if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) : |
---|
| 308 | patterns = [ patterns ] |
---|
| 309 | if self.tool.matchString(attrval, patterns) : |
---|
| 310 | result.append(attrval) |
---|
| 311 | else : |
---|
| 312 | result.append(attrval) |
---|
[2107] | 313 | return result |
---|
| 314 | |
---|
[2386] | 315 | def getAllBillingCodes(self, billingcode=None) : |
---|
| 316 | """Extracts all billing codes or only the billing codes matching the optional parameter.""" |
---|
| 317 | ldapfilter = "objectClass=pykotaBilling" |
---|
| 318 | result = self.doSearch(ldapfilter, ["pykotaBillingCode"], base=self.info["billingcodebase"]) |
---|
| 319 | if result : |
---|
[2652] | 320 | return [self.databaseToUserCharset(bc) for bc in self.filterNames(result, "pykotaBillingCode", billingcode)] |
---|
| 321 | else : |
---|
| 322 | return [] |
---|
[2386] | 323 | |
---|
[1993] | 324 | def getAllPrintersNames(self, printername=None) : |
---|
| 325 | """Extracts all printer names or only the printers' names matching the optional parameter.""" |
---|
| 326 | ldapfilter = "objectClass=pykotaPrinter" |
---|
| 327 | result = self.doSearch(ldapfilter, ["pykotaPrinterName"], base=self.info["printerbase"]) |
---|
[1754] | 328 | if result : |
---|
[2652] | 329 | return self.filterNames(result, "pykotaPrinterName", printername) |
---|
| 330 | else : |
---|
| 331 | return [] |
---|
[1754] | 332 | |
---|
[1993] | 333 | def getAllUsersNames(self, username=None) : |
---|
| 334 | """Extracts all user names or only the users' names matching the optional parameter.""" |
---|
| 335 | ldapfilter = "objectClass=pykotaAccount" |
---|
| 336 | result = self.doSearch(ldapfilter, ["pykotaUserName"], base=self.info["userbase"]) |
---|
[1179] | 337 | if result : |
---|
[2652] | 338 | return self.filterNames(result, "pykotaUserName", username) |
---|
| 339 | else : |
---|
| 340 | return [] |
---|
[1179] | 341 | |
---|
[1993] | 342 | def getAllGroupsNames(self, groupname=None) : |
---|
| 343 | """Extracts all group names or only the groups' names matching the optional parameter.""" |
---|
| 344 | ldapfilter = "objectClass=pykotaGroup" |
---|
| 345 | result = self.doSearch(ldapfilter, ["pykotaGroupName"], base=self.info["groupbase"]) |
---|
[1179] | 346 | if result : |
---|
[2652] | 347 | return self.filterNames(result, "pykotaGroupName", groupname) |
---|
| 348 | else : |
---|
| 349 | return [] |
---|
[1179] | 350 | |
---|
[1806] | 351 | def getUserNbJobsFromHistory(self, user) : |
---|
| 352 | """Returns the number of jobs the user has in history.""" |
---|
[2652] | 353 | result = self.doSearch("(&(pykotaUserName=%s)(objectClass=pykotaJob))" % self.userCharsetToDatabase(user.Name), None, base=self.info["jobbase"]) |
---|
[1806] | 354 | return len(result) |
---|
| 355 | |
---|
[1130] | 356 | def getUserFromBackend(self, username) : |
---|
[1041] | 357 | """Extracts user information given its name.""" |
---|
| 358 | user = StorageUser(self, username) |
---|
[2652] | 359 | username = self.userCharsetToDatabase(username) |
---|
[2721] | 360 | result = self.doSearch("(&(objectClass=pykotaAccount)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["userrdn"], username), ["pykotaUserName", "pykotaLimitBy", self.info["usermail"], "pykotaOverCharge", "description"], base=self.info["userbase"]) |
---|
[1016] | 361 | if result : |
---|
[1041] | 362 | fields = result[0][1] |
---|
| 363 | user.ident = result[0][0] |
---|
[2721] | 364 | user.Description = self.databaseToUserCharset(fields.get("description", [None])[0]) |
---|
[2054] | 365 | user.Email = fields.get(self.info["usermail"], [None])[0] |
---|
| 366 | user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] |
---|
| 367 | user.OverCharge = float(fields.get("pykotaOverCharge", [1.0])[0]) |
---|
[1522] | 368 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["balancerdn"], username), ["pykotaBalance", "pykotaLifeTimePaid", "pykotaPayments"], base=self.info["balancebase"]) |
---|
[1742] | 369 | if not result : |
---|
| 370 | raise PyKotaStorageError, _("No pykotaAccountBalance object found for user %s. Did you create LDAP entries manually ?") % username |
---|
| 371 | else : |
---|
[1041] | 372 | fields = result[0][1] |
---|
| 373 | user.idbalance = result[0][0] |
---|
| 374 | user.AccountBalance = fields.get("pykotaBalance") |
---|
| 375 | if user.AccountBalance is not None : |
---|
| 376 | if user.AccountBalance[0].upper() == "NONE" : |
---|
| 377 | user.AccountBalance = None |
---|
| 378 | else : |
---|
| 379 | user.AccountBalance = float(user.AccountBalance[0]) |
---|
| 380 | user.AccountBalance = user.AccountBalance or 0.0 |
---|
| 381 | user.LifeTimePaid = fields.get("pykotaLifeTimePaid") |
---|
| 382 | if user.LifeTimePaid is not None : |
---|
| 383 | if user.LifeTimePaid[0].upper() == "NONE" : |
---|
| 384 | user.LifeTimePaid = None |
---|
| 385 | else : |
---|
| 386 | user.LifeTimePaid = float(user.LifeTimePaid[0]) |
---|
| 387 | user.LifeTimePaid = user.LifeTimePaid or 0.0 |
---|
[1522] | 388 | user.Payments = [] |
---|
| 389 | for payment in fields.get("pykotaPayments", []) : |
---|
[2452] | 390 | try : |
---|
| 391 | (date, amount, description) = payment.split(" # ") |
---|
| 392 | except ValueError : |
---|
| 393 | # Payment with no description (old Payment) |
---|
| 394 | (date, amount) = payment.split(" # ") |
---|
| 395 | description = "" |
---|
| 396 | else : |
---|
[2460] | 397 | description = self.databaseToUserCharset(base64.decodestring(description)) |
---|
[2452] | 398 | user.Payments.append((date, float(amount), description)) |
---|
[1041] | 399 | user.Exists = 1 |
---|
| 400 | return user |
---|
| 401 | |
---|
[1130] | 402 | def getGroupFromBackend(self, groupname) : |
---|
[1041] | 403 | """Extracts group information given its name.""" |
---|
| 404 | group = StorageGroup(self, groupname) |
---|
[2652] | 405 | groupname = self.userCharsetToDatabase(groupname) |
---|
[2721] | 406 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (groupname, self.info["grouprdn"], groupname), ["pykotaGroupName", "pykotaLimitBy", "description"], base=self.info["groupbase"]) |
---|
[1030] | 407 | if result : |
---|
[1041] | 408 | fields = result[0][1] |
---|
| 409 | group.ident = result[0][0] |
---|
[2652] | 410 | group.Name = fields.get("pykotaGroupName", [self.databaseToUserCharset(groupname)])[0] |
---|
[2721] | 411 | group.Description = self.databaseToUserCharset(fields.get("description", [None])[0]) |
---|
[2054] | 412 | group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] |
---|
[1041] | 413 | group.AccountBalance = 0.0 |
---|
| 414 | group.LifeTimePaid = 0.0 |
---|
[1137] | 415 | for member in self.getGroupMembers(group) : |
---|
[1075] | 416 | if member.Exists : |
---|
| 417 | group.AccountBalance += member.AccountBalance |
---|
| 418 | group.LifeTimePaid += member.LifeTimePaid |
---|
[1041] | 419 | group.Exists = 1 |
---|
| 420 | return group |
---|
| 421 | |
---|
[1130] | 422 | def getPrinterFromBackend(self, printername) : |
---|
[1451] | 423 | """Extracts printer information given its name : returns first matching printer.""" |
---|
[1041] | 424 | printer = StoragePrinter(self, printername) |
---|
[2652] | 425 | printername = self.userCharsetToDatabase(printername) |
---|
[2459] | 426 | result = self.doSearch("(&(objectClass=pykotaPrinter)(|(pykotaPrinterName=%s)(%s=%s)))" \ |
---|
| 427 | % (printername, self.info["printerrdn"], printername), \ |
---|
| 428 | ["pykotaPrinterName", "pykotaPricePerPage", \ |
---|
| 429 | "pykotaPricePerJob", "pykotaMaxJobSize", \ |
---|
| 430 | "pykotaPassThrough", "uniqueMember", "description"], \ |
---|
| 431 | base=self.info["printerbase"]) |
---|
[1016] | 432 | if result : |
---|
[1451] | 433 | fields = result[0][1] # take only first matching printer, ignore the rest |
---|
[1041] | 434 | printer.ident = result[0][0] |
---|
[2652] | 435 | printer.Name = fields.get("pykotaPrinterName", [self.databaseToUserCharset(printername)])[0] |
---|
[2054] | 436 | printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0]) |
---|
| 437 | printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0]) |
---|
[2459] | 438 | printer.MaxJobSize = int(fields.get("pykotaMaxJobSize", [0])[0]) |
---|
| 439 | printer.PassThrough = fields.get("pykotaPassThrough", [None])[0] |
---|
| 440 | if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : |
---|
| 441 | printer.PassThrough = 1 |
---|
| 442 | else : |
---|
| 443 | printer.PassThrough = 0 |
---|
[1258] | 444 | printer.uniqueMember = fields.get("uniqueMember", []) |
---|
[1790] | 445 | printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
[1041] | 446 | printer.Exists = 1 |
---|
| 447 | return printer |
---|
| 448 | |
---|
[1130] | 449 | def getUserPQuotaFromBackend(self, user, printer) : |
---|
[1041] | 450 | """Extracts a user print quota.""" |
---|
| 451 | userpquota = StorageUserPQuota(self, user, printer) |
---|
[1228] | 452 | if printer.Exists and user.Exists : |
---|
[1969] | 453 | if self.info["userquotabase"].lower() == "user" : |
---|
[1998] | 454 | base = user.ident |
---|
[1969] | 455 | else : |
---|
[1998] | 456 | base = self.info["userquotabase"] |
---|
[2652] | 457 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % \ |
---|
| 458 | (self.userCharsetToDatabase(user.Name), self.userCharsetToDatabase(printer.Name)), \ |
---|
[2749] | 459 | ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount", "pykotaMaxJobSize"], \ |
---|
[2652] | 460 | base=base) |
---|
[1017] | 461 | if result : |
---|
[1041] | 462 | fields = result[0][1] |
---|
| 463 | userpquota.ident = result[0][0] |
---|
[2054] | 464 | userpquota.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) |
---|
| 465 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter", [0])[0]) |
---|
| 466 | userpquota.WarnCount = int(fields.get("pykotaWarnCount", [0])[0]) |
---|
[1041] | 467 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 468 | if userpquota.SoftLimit is not None : |
---|
| 469 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 470 | userpquota.SoftLimit = None |
---|
| 471 | else : |
---|
| 472 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 473 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 474 | if userpquota.HardLimit is not None : |
---|
| 475 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 476 | userpquota.HardLimit = None |
---|
| 477 | elif userpquota.HardLimit is not None : |
---|
| 478 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 479 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 480 | if userpquota.DateLimit is not None : |
---|
| 481 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 482 | userpquota.DateLimit = None |
---|
| 483 | else : |
---|
| 484 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
[2749] | 485 | userpquota.MaxJobSize = fields.get("pykotaMaxJobSize") |
---|
| 486 | if userpquota.MaxJobSize is not None : |
---|
| 487 | if userpquota.MaxJobSize[0].upper() == "NONE" : |
---|
| 488 | userpquota.MaxJobSize = None |
---|
| 489 | else : |
---|
| 490 | userpquota.MaxJobSize = int(userpquota.MaxJobSize[0]) |
---|
[1041] | 491 | userpquota.Exists = 1 |
---|
| 492 | return userpquota |
---|
[1016] | 493 | |
---|
[1130] | 494 | def getGroupPQuotaFromBackend(self, group, printer) : |
---|
[1041] | 495 | """Extracts a group print quota.""" |
---|
| 496 | grouppquota = StorageGroupPQuota(self, group, printer) |
---|
| 497 | if group.Exists : |
---|
[1969] | 498 | if self.info["groupquotabase"].lower() == "group" : |
---|
[1998] | 499 | base = group.ident |
---|
[1969] | 500 | else : |
---|
[1998] | 501 | base = self.info["groupquotabase"] |
---|
[2652] | 502 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % \ |
---|
| 503 | (self.userCharsetToDatabase(group.Name), self.userCharsetToDatabase(printer.Name)), \ |
---|
[2749] | 504 | ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaMaxJobSize"], \ |
---|
[2652] | 505 | base=base) |
---|
[1041] | 506 | if result : |
---|
| 507 | fields = result[0][1] |
---|
| 508 | grouppquota.ident = result[0][0] |
---|
| 509 | grouppquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 510 | if grouppquota.SoftLimit is not None : |
---|
| 511 | if grouppquota.SoftLimit[0].upper() == "NONE" : |
---|
| 512 | grouppquota.SoftLimit = None |
---|
| 513 | else : |
---|
| 514 | grouppquota.SoftLimit = int(grouppquota.SoftLimit[0]) |
---|
| 515 | grouppquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 516 | if grouppquota.HardLimit is not None : |
---|
| 517 | if grouppquota.HardLimit[0].upper() == "NONE" : |
---|
| 518 | grouppquota.HardLimit = None |
---|
| 519 | else : |
---|
| 520 | grouppquota.HardLimit = int(grouppquota.HardLimit[0]) |
---|
| 521 | grouppquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 522 | if grouppquota.DateLimit is not None : |
---|
| 523 | if grouppquota.DateLimit[0].upper() == "NONE" : |
---|
| 524 | grouppquota.DateLimit = None |
---|
| 525 | else : |
---|
| 526 | grouppquota.DateLimit = grouppquota.DateLimit[0] |
---|
[2749] | 527 | grouppquota.MaxJobSize = fields.get("pykotaMaxJobSize") |
---|
| 528 | if grouppquota.MaxJobSize is not None : |
---|
| 529 | if grouppquota.MaxJobSize[0].upper() == "NONE" : |
---|
| 530 | grouppquota.MaxJobSize = None |
---|
| 531 | else : |
---|
| 532 | grouppquota.MaxJobSize = int(grouppquota.MaxJobSize[0]) |
---|
[1041] | 533 | grouppquota.PageCounter = 0 |
---|
| 534 | grouppquota.LifePageCounter = 0 |
---|
[2652] | 535 | usernamesfilter = "".join(["(pykotaUserName=%s)" % self.userCharsetToDatabase(member.Name) for member in self.getGroupMembers(group)]) |
---|
[1361] | 536 | if usernamesfilter : |
---|
| 537 | usernamesfilter = "(|%s)" % usernamesfilter |
---|
[1998] | 538 | if self.info["userquotabase"].lower() == "user" : |
---|
| 539 | base = self.info["userbase"] |
---|
| 540 | else : |
---|
| 541 | base = self.info["userquotabase"] |
---|
[2652] | 542 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)%s)" % \ |
---|
| 543 | (self.userCharsetToDatabase(printer.Name), usernamesfilter), \ |
---|
| 544 | ["pykotaPageCounter", "pykotaLifePageCounter"], base=base) |
---|
[1041] | 545 | if result : |
---|
| 546 | for userpquota in result : |
---|
[1392] | 547 | grouppquota.PageCounter += int(userpquota[1].get("pykotaPageCounter", [0])[0] or 0) |
---|
| 548 | grouppquota.LifePageCounter += int(userpquota[1].get("pykotaLifePageCounter", [0])[0] or 0) |
---|
[1041] | 549 | grouppquota.Exists = 1 |
---|
| 550 | return grouppquota |
---|
| 551 | |
---|
[1130] | 552 | def getPrinterLastJobFromBackend(self, printer) : |
---|
[1041] | 553 | """Extracts a printer's last job information.""" |
---|
| 554 | lastjob = StorageLastJob(self, printer) |
---|
[2652] | 555 | pname = self.userCharsetToDatabase(printer.Name) |
---|
| 556 | result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % \ |
---|
| 557 | (pname, self.info["printerrdn"], pname), \ |
---|
| 558 | ["pykotaLastJobIdent"], \ |
---|
| 559 | base=self.info["lastjobbase"]) |
---|
[1016] | 560 | if result : |
---|
[1041] | 561 | lastjob.lastjobident = result[0][0] |
---|
| 562 | lastjobident = result[0][1]["pykotaLastJobIdent"][0] |
---|
[1692] | 563 | result = None |
---|
| 564 | try : |
---|
[2211] | 565 | result = self.doSearch("objectClass=pykotaJob", [ "pykotaJobSizeBytes", |
---|
| 566 | "pykotaHostName", |
---|
| 567 | "pykotaUserName", |
---|
| 568 | "pykotaPrinterName", |
---|
| 569 | "pykotaJobId", |
---|
| 570 | "pykotaPrinterPageCounter", |
---|
| 571 | "pykotaJobSize", |
---|
| 572 | "pykotaAction", |
---|
| 573 | "pykotaJobPrice", |
---|
| 574 | "pykotaFileName", |
---|
| 575 | "pykotaTitle", |
---|
| 576 | "pykotaCopies", |
---|
| 577 | "pykotaOptions", |
---|
| 578 | "pykotaBillingCode", |
---|
| 579 | "pykotaPages", |
---|
| 580 | "pykotaMD5Sum", |
---|
[2455] | 581 | "pykotaPrecomputedJobSize", |
---|
| 582 | "pykotaPrecomputedJobPrice", |
---|
[2211] | 583 | "createTimestamp" ], |
---|
| 584 | base="cn=%s,%s" % (lastjobident, self.info["jobbase"]), scope=ldap.SCOPE_BASE) |
---|
[1692] | 585 | except PyKotaStorageError : |
---|
| 586 | pass # Last job entry exists, but job probably doesn't exist anymore. |
---|
[1017] | 587 | if result : |
---|
[1041] | 588 | fields = result[0][1] |
---|
| 589 | lastjob.ident = result[0][0] |
---|
| 590 | lastjob.JobId = fields.get("pykotaJobId")[0] |
---|
[2652] | 591 | lastjob.UserName = self.databaseToUserCharset(fields.get("pykotaUserName")[0]) |
---|
[2054] | 592 | lastjob.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter", [0])[0]) |
---|
[1601] | 593 | try : |
---|
| 594 | lastjob.JobSize = int(fields.get("pykotaJobSize", [0])[0]) |
---|
| 595 | except ValueError : |
---|
| 596 | lastjob.JobSize = None |
---|
| 597 | try : |
---|
| 598 | lastjob.JobPrice = float(fields.get("pykotaJobPrice", [0.0])[0]) |
---|
| 599 | except ValueError : |
---|
| 600 | lastjob.JobPrice = None |
---|
[1393] | 601 | lastjob.JobAction = fields.get("pykotaAction", [""])[0] |
---|
[1790] | 602 | lastjob.JobFileName = self.databaseToUserCharset(fields.get("pykotaFileName", [""])[0]) |
---|
| 603 | lastjob.JobTitle = self.databaseToUserCharset(fields.get("pykotaTitle", [""])[0]) |
---|
[1203] | 604 | lastjob.JobCopies = int(fields.get("pykotaCopies", [0])[0]) |
---|
[1790] | 605 | lastjob.JobOptions = self.databaseToUserCharset(fields.get("pykotaOptions", [""])[0]) |
---|
[1502] | 606 | lastjob.JobHostName = fields.get("pykotaHostName", [""])[0] |
---|
[1520] | 607 | lastjob.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] |
---|
[2217] | 608 | lastjob.JobBillingCode = self.databaseToUserCharset(fields.get("pykotaBillingCode", [None])[0]) |
---|
[2054] | 609 | lastjob.JobMD5Sum = fields.get("pykotaMD5Sum", [None])[0] |
---|
| 610 | lastjob.JobPages = fields.get("pykotaPages", [""])[0] |
---|
[2455] | 611 | try : |
---|
| 612 | lastjob.PrecomputedJobSize = int(fields.get("pykotaPrecomputedJobSize", [0])[0]) |
---|
| 613 | except ValueError : |
---|
| 614 | lastjob.PrecomputedJobSize = None |
---|
| 615 | try : |
---|
| 616 | lastjob.PrecomputedJobPrice = float(fields.get("pykotaPrecomputedJobPrice", [0.0])[0]) |
---|
| 617 | except ValueError : |
---|
| 618 | lastjob.PrecomputedJobPrice = None |
---|
[2287] | 619 | if lastjob.JobTitle == lastjob.JobFileName == lastjob.JobOptions == "hidden" : |
---|
| 620 | (lastjob.JobTitle, lastjob.JobFileName, lastjob.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 |
---|
[2538] | 621 | date = fields.get("createTimestamp", ["19700101000000Z"])[0] # It's in UTC ! |
---|
| 622 | mxtime = DateTime.strptime(date[:14], "%Y%m%d%H%M%S").localtime() |
---|
| 623 | lastjob.JobDate = mxtime.strftime("%Y%m%d %H:%M:%S") |
---|
[1041] | 624 | lastjob.Exists = 1 |
---|
| 625 | return lastjob |
---|
[1016] | 626 | |
---|
[1137] | 627 | def getGroupMembersFromBackend(self, group) : |
---|
| 628 | """Returns the group's members list.""" |
---|
| 629 | groupmembers = [] |
---|
[2652] | 630 | gname = self.userCharsetToDatabase(group.Name) |
---|
| 631 | result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % \ |
---|
| 632 | (gname, self.info["grouprdn"], gname), \ |
---|
| 633 | [self.info["groupmembers"]], \ |
---|
| 634 | base=self.info["groupbase"]) |
---|
[1137] | 635 | if result : |
---|
| 636 | for username in result[0][1].get(self.info["groupmembers"], []) : |
---|
[2652] | 637 | groupmembers.append(self.getUser(self.databaseToUserCharset(username))) |
---|
[1137] | 638 | return groupmembers |
---|
| 639 | |
---|
| 640 | def getUserGroupsFromBackend(self, user) : |
---|
[1130] | 641 | """Returns the user's groups list.""" |
---|
| 642 | groups = [] |
---|
[2652] | 643 | uname = self.userCharsetToDatabase(user.Name) |
---|
| 644 | result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % \ |
---|
| 645 | (self.info["groupmembers"], uname), \ |
---|
| 646 | [self.info["grouprdn"], "pykotaGroupName", "pykotaLimitBy"], \ |
---|
| 647 | base=self.info["groupbase"]) |
---|
[1130] | 648 | if result : |
---|
| 649 | for (groupid, fields) in result : |
---|
[2652] | 650 | groupname = self.databaseToUserCharset((fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0]) |
---|
[1147] | 651 | group = self.getFromCache("GROUPS", groupname) |
---|
| 652 | if group is None : |
---|
| 653 | group = StorageGroup(self, groupname) |
---|
| 654 | group.ident = groupid |
---|
| 655 | group.LimitBy = fields.get("pykotaLimitBy") |
---|
| 656 | if group.LimitBy is not None : |
---|
| 657 | group.LimitBy = group.LimitBy[0] |
---|
[2000] | 658 | else : |
---|
| 659 | group.LimitBy = "quota" |
---|
[1147] | 660 | group.AccountBalance = 0.0 |
---|
| 661 | group.LifeTimePaid = 0.0 |
---|
| 662 | for member in self.getGroupMembers(group) : |
---|
| 663 | if member.Exists : |
---|
| 664 | group.AccountBalance += member.AccountBalance |
---|
| 665 | group.LifeTimePaid += member.LifeTimePaid |
---|
| 666 | group.Exists = 1 |
---|
| 667 | self.cacheEntry("GROUPS", group.Name, group) |
---|
| 668 | groups.append(group) |
---|
[1130] | 669 | return groups |
---|
| 670 | |
---|
[1249] | 671 | def getParentPrintersFromBackend(self, printer) : |
---|
| 672 | """Get all the printer groups this printer is a member of.""" |
---|
| 673 | pgroups = [] |
---|
[2652] | 674 | result = self.doSearch("(&(objectClass=pykotaPrinter)(uniqueMember=%s))" % \ |
---|
| 675 | printer.ident, \ |
---|
| 676 | ["pykotaPrinterName"], \ |
---|
| 677 | base=self.info["printerbase"]) |
---|
[1249] | 678 | if result : |
---|
| 679 | for (printerid, fields) in result : |
---|
| 680 | if printerid != printer.ident : # In case of integrity violation. |
---|
[2652] | 681 | parentprinter = self.getPrinter(self.databaseToUserCharset(fields.get("pykotaPrinterName")[0])) |
---|
[1249] | 682 | if parentprinter.Exists : |
---|
| 683 | pgroups.append(parentprinter) |
---|
| 684 | return pgroups |
---|
| 685 | |
---|
[1041] | 686 | def getMatchingPrinters(self, printerpattern) : |
---|
| 687 | """Returns the list of all printers for which name matches a certain pattern.""" |
---|
| 688 | printers = [] |
---|
| 689 | # see comment at the same place in pgstorage.py |
---|
[2657] | 690 | result = self.doSearch("objectClass=pykotaPrinter", \ |
---|
[2652] | 691 | ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob", "pykotaMaxJobSize", "pykotaPassThrough", "uniqueMember", "description"], \ |
---|
| 692 | base=self.info["printerbase"]) |
---|
[1016] | 693 | if result : |
---|
[2657] | 694 | patterns = printerpattern.split(",") |
---|
[2754] | 695 | try : |
---|
| 696 | patdict = {}.fromkeys(patterns) |
---|
| 697 | except AttributeError : |
---|
| 698 | # Python v2.2 or earlier |
---|
| 699 | patdict = {} |
---|
| 700 | for p in patterns : |
---|
| 701 | patdict[p] = None |
---|
[1041] | 702 | for (printerid, fields) in result : |
---|
[2652] | 703 | printername = self.databaseToUserCharset(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) |
---|
[2754] | 704 | if patdict.has_key(printername) or self.tool.matchString(printername, patterns) : |
---|
[2657] | 705 | printer = StoragePrinter(self, printername) |
---|
| 706 | printer.ident = printerid |
---|
| 707 | printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0] or 0.0) |
---|
| 708 | printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0] or 0.0) |
---|
| 709 | printer.MaxJobSize = int(fields.get("pykotaMaxJobSize", [0])[0]) |
---|
| 710 | printer.PassThrough = fields.get("pykotaPassThrough", [None])[0] |
---|
| 711 | if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : |
---|
| 712 | printer.PassThrough = 1 |
---|
| 713 | else : |
---|
| 714 | printer.PassThrough = 0 |
---|
| 715 | printer.uniqueMember = fields.get("uniqueMember", []) |
---|
| 716 | printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
| 717 | printer.Exists = 1 |
---|
| 718 | printers.append(printer) |
---|
| 719 | self.cacheEntry("PRINTERS", printer.Name, printer) |
---|
[1041] | 720 | return printers |
---|
[1016] | 721 | |
---|
[2657] | 722 | def getMatchingUsers(self, userpattern) : |
---|
| 723 | """Returns the list of all users for which name matches a certain pattern.""" |
---|
| 724 | users = [] |
---|
| 725 | # see comment at the same place in pgstorage.py |
---|
| 726 | result = self.doSearch("objectClass=pykotaAccount", \ |
---|
[2721] | 727 | ["pykotaUserName", "pykotaLimitBy", self.info["usermail"], "pykotaOverCharge", "description"], \ |
---|
[2657] | 728 | base=self.info["userbase"]) |
---|
| 729 | if result : |
---|
| 730 | patterns = userpattern.split(",") |
---|
[2754] | 731 | try : |
---|
| 732 | patdict = {}.fromkeys(patterns) |
---|
| 733 | except AttributeError : |
---|
| 734 | # Python v2.2 or earlier |
---|
| 735 | patdict = {} |
---|
| 736 | for p in patterns : |
---|
| 737 | patdict[p] = None |
---|
[2657] | 738 | for (userid, fields) in result : |
---|
| 739 | username = self.databaseToUserCharset(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) |
---|
[2754] | 740 | if patdict.has_key(username) or self.tool.matchString(username, patterns) : |
---|
[2657] | 741 | user = StorageUser(self, username) |
---|
| 742 | user.ident = userid |
---|
| 743 | user.Email = fields.get(self.info["usermail"], [None])[0] |
---|
| 744 | user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] |
---|
| 745 | user.OverCharge = float(fields.get("pykotaOverCharge", [1.0])[0]) |
---|
[2721] | 746 | user.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
[2657] | 747 | uname = self.userCharsetToDatabase(username) |
---|
| 748 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % \ |
---|
| 749 | (uname, self.info["balancerdn"], uname), \ |
---|
| 750 | ["pykotaBalance", "pykotaLifeTimePaid", "pykotaPayments"], \ |
---|
| 751 | base=self.info["balancebase"]) |
---|
| 752 | if not result : |
---|
| 753 | raise PyKotaStorageError, _("No pykotaAccountBalance object found for user %s. Did you create LDAP entries manually ?") % username |
---|
| 754 | else : |
---|
| 755 | fields = result[0][1] |
---|
| 756 | user.idbalance = result[0][0] |
---|
| 757 | user.AccountBalance = fields.get("pykotaBalance") |
---|
| 758 | if user.AccountBalance is not None : |
---|
| 759 | if user.AccountBalance[0].upper() == "NONE" : |
---|
| 760 | user.AccountBalance = None |
---|
| 761 | else : |
---|
| 762 | user.AccountBalance = float(user.AccountBalance[0]) |
---|
| 763 | user.AccountBalance = user.AccountBalance or 0.0 |
---|
| 764 | user.LifeTimePaid = fields.get("pykotaLifeTimePaid") |
---|
| 765 | if user.LifeTimePaid is not None : |
---|
| 766 | if user.LifeTimePaid[0].upper() == "NONE" : |
---|
| 767 | user.LifeTimePaid = None |
---|
| 768 | else : |
---|
| 769 | user.LifeTimePaid = float(user.LifeTimePaid[0]) |
---|
| 770 | user.LifeTimePaid = user.LifeTimePaid or 0.0 |
---|
| 771 | user.Payments = [] |
---|
| 772 | for payment in fields.get("pykotaPayments", []) : |
---|
| 773 | try : |
---|
| 774 | (date, amount, description) = payment.split(" # ") |
---|
| 775 | except ValueError : |
---|
| 776 | # Payment with no description (old Payment) |
---|
| 777 | (date, amount) = payment.split(" # ") |
---|
| 778 | description = "" |
---|
| 779 | else : |
---|
| 780 | description = self.databaseToUserCharset(base64.decodestring(description)) |
---|
| 781 | user.Payments.append((date, float(amount), description)) |
---|
| 782 | user.Exists = 1 |
---|
| 783 | users.append(user) |
---|
| 784 | self.cacheEntry("USERS", user.Name, user) |
---|
| 785 | return users |
---|
| 786 | |
---|
| 787 | def getMatchingGroups(self, grouppattern) : |
---|
| 788 | """Returns the list of all groups for which name matches a certain pattern.""" |
---|
| 789 | groups = [] |
---|
| 790 | # see comment at the same place in pgstorage.py |
---|
| 791 | result = self.doSearch("objectClass=pykotaGroup", \ |
---|
[2721] | 792 | ["pykotaGroupName", "pykotaLimitBy", "description"], \ |
---|
[2657] | 793 | base=self.info["groupbase"]) |
---|
| 794 | if result : |
---|
| 795 | patterns = grouppattern.split(",") |
---|
[2754] | 796 | try : |
---|
| 797 | patdict = {}.fromkeys(patterns) |
---|
| 798 | except AttributeError : |
---|
| 799 | # Python v2.2 or earlier |
---|
| 800 | patdict = {} |
---|
| 801 | for p in patterns : |
---|
| 802 | patdict[p] = None |
---|
[2657] | 803 | for (groupid, fields) in result : |
---|
| 804 | groupname = self.databaseToUserCharset(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) |
---|
[2754] | 805 | if patdict.has_key(groupname) or self.tool.matchString(groupname, patterns) : |
---|
[2657] | 806 | group = StorageGroup(self, groupname) |
---|
| 807 | group.ident = groupid |
---|
| 808 | group.Name = fields.get("pykotaGroupName", [self.databaseToUserCharset(groupname)])[0] |
---|
| 809 | group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] |
---|
[2721] | 810 | group.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
[2657] | 811 | group.AccountBalance = 0.0 |
---|
| 812 | group.LifeTimePaid = 0.0 |
---|
| 813 | for member in self.getGroupMembers(group) : |
---|
| 814 | if member.Exists : |
---|
| 815 | group.AccountBalance += member.AccountBalance |
---|
| 816 | group.LifeTimePaid += member.LifeTimePaid |
---|
| 817 | group.Exists = 1 |
---|
[2752] | 818 | groups.append(group) |
---|
| 819 | self.cacheEntry("GROUPS", group.Name, group) |
---|
[2657] | 820 | return groups |
---|
| 821 | |
---|
[1133] | 822 | def getPrinterUsersAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 823 | """Returns the list of users who uses a given printer, along with their quotas.""" |
---|
| 824 | usersandquotas = [] |
---|
[2652] | 825 | pname = self.userCharsetToDatabase(printer.Name) |
---|
| 826 | names = [self.userCharsetToDatabase(n) for n in names] |
---|
[1998] | 827 | if self.info["userquotabase"].lower() == "user" : |
---|
[2830] | 828 | base = self.info["userbase"] |
---|
[1998] | 829 | else : |
---|
[2830] | 830 | base = self.info["userquotabase"] |
---|
[2652] | 831 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)(|%s))" % \ |
---|
| 832 | (pname, "".join(["(pykotaUserName=%s)" % uname for uname in names])), \ |
---|
| 833 | ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount"], \ |
---|
| 834 | base=base) |
---|
[1041] | 835 | if result : |
---|
| 836 | for (userquotaid, fields) in result : |
---|
[2652] | 837 | user = self.getUser(self.databaseToUserCharset(fields.get("pykotaUserName")[0])) |
---|
[1133] | 838 | userpquota = StorageUserPQuota(self, user, printer) |
---|
| 839 | userpquota.ident = userquotaid |
---|
[2054] | 840 | userpquota.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) |
---|
| 841 | userpquota.LifePageCounter = int(fields.get("pykotaLifePageCounter", [0])[0]) |
---|
| 842 | userpquota.WarnCount = int(fields.get("pykotaWarnCount", [0])[0]) |
---|
[1133] | 843 | userpquota.SoftLimit = fields.get("pykotaSoftLimit") |
---|
| 844 | if userpquota.SoftLimit is not None : |
---|
| 845 | if userpquota.SoftLimit[0].upper() == "NONE" : |
---|
| 846 | userpquota.SoftLimit = None |
---|
| 847 | else : |
---|
| 848 | userpquota.SoftLimit = int(userpquota.SoftLimit[0]) |
---|
| 849 | userpquota.HardLimit = fields.get("pykotaHardLimit") |
---|
| 850 | if userpquota.HardLimit is not None : |
---|
| 851 | if userpquota.HardLimit[0].upper() == "NONE" : |
---|
| 852 | userpquota.HardLimit = None |
---|
| 853 | elif userpquota.HardLimit is not None : |
---|
| 854 | userpquota.HardLimit = int(userpquota.HardLimit[0]) |
---|
| 855 | userpquota.DateLimit = fields.get("pykotaDateLimit") |
---|
| 856 | if userpquota.DateLimit is not None : |
---|
| 857 | if userpquota.DateLimit[0].upper() == "NONE" : |
---|
| 858 | userpquota.DateLimit = None |
---|
| 859 | else : |
---|
| 860 | userpquota.DateLimit = userpquota.DateLimit[0] |
---|
| 861 | userpquota.Exists = 1 |
---|
| 862 | usersandquotas.append((user, userpquota)) |
---|
| 863 | self.cacheEntry("USERPQUOTAS", "%s@%s" % (user.Name, printer.Name), userpquota) |
---|
[1051] | 864 | usersandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 865 | return usersandquotas |
---|
| 866 | |
---|
[1133] | 867 | def getPrinterGroupsAndQuotas(self, printer, names=["*"]) : |
---|
[1041] | 868 | """Returns the list of groups which uses a given printer, along with their quotas.""" |
---|
| 869 | groupsandquotas = [] |
---|
[2652] | 870 | pname = self.userCharsetToDatabase(printer.Name) |
---|
| 871 | names = [self.userCharsetToDatabase(n) for n in names] |
---|
[1998] | 872 | if self.info["groupquotabase"].lower() == "group" : |
---|
[2830] | 873 | base = self.info["groupbase"] |
---|
[1998] | 874 | else : |
---|
[2830] | 875 | base = self.info["groupquotabase"] |
---|
[2652] | 876 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s)(|%s))" % \ |
---|
| 877 | (pname, "".join(["(pykotaGroupName=%s)" % gname for gname in names])), \ |
---|
| 878 | ["pykotaGroupName"], \ |
---|
| 879 | base=base) |
---|
[1041] | 880 | if result : |
---|
| 881 | for (groupquotaid, fields) in result : |
---|
[2652] | 882 | group = self.getGroup(self.databaseToUserCharset(fields.get("pykotaGroupName")[0])) |
---|
[1133] | 883 | grouppquota = self.getGroupPQuota(group, printer) |
---|
| 884 | groupsandquotas.append((group, grouppquota)) |
---|
[1051] | 885 | groupsandquotas.sort(lambda x, y : cmp(x[0].Name, y[0].Name)) |
---|
[1041] | 886 | return groupsandquotas |
---|
[1016] | 887 | |
---|
[2768] | 888 | def addPrinter(self, printer) : |
---|
| 889 | """Adds a printer to the quota storage, returns the old value if it already exists.""" |
---|
| 890 | oldentry = self.getPrinter(printer.Name) |
---|
| 891 | if oldentry.Exists : |
---|
| 892 | return oldentry # we return the existing entry |
---|
| 893 | printername = self.userCharsetToDatabase(printer.Name) |
---|
[1030] | 894 | fields = { self.info["printerrdn"] : printername, |
---|
| 895 | "objectClass" : ["pykotaObject", "pykotaPrinter"], |
---|
[1041] | 896 | "cn" : printername, |
---|
[1030] | 897 | "pykotaPrinterName" : printername, |
---|
[2768] | 898 | "pykotaPassThrough" : (printer.PassThrough and "t") or "f", |
---|
| 899 | "pykotaMaxJobSize" : str(printer.MaxJobSize or 0), |
---|
| 900 | "description" : self.userCharsetToDatabase(printer.Description or ""), |
---|
| 901 | "pykotaPricePerPage" : str(printer.PricePerPage or 0.0), |
---|
| 902 | "pykotaPricePerJob" : str(printer.PricePerJob or 0.0), |
---|
[1030] | 903 | } |
---|
| 904 | dn = "%s=%s,%s" % (self.info["printerrdn"], printername, self.info["printerbase"]) |
---|
[1041] | 905 | self.doAdd(dn, fields) |
---|
[2768] | 906 | printer.isDirty = False |
---|
| 907 | return None # the entry created doesn't need further modification |
---|
[1016] | 908 | |
---|
[1041] | 909 | def addUser(self, user) : |
---|
[2773] | 910 | """Adds a user to the quota storage, returns the old value if it already exists.""" |
---|
| 911 | oldentry = self.getUser(user.Name) |
---|
| 912 | if oldentry.Exists : |
---|
| 913 | return oldentry # we return the existing entry |
---|
[2652] | 914 | uname = self.userCharsetToDatabase(user.Name) |
---|
[1105] | 915 | newfields = { |
---|
[2652] | 916 | "pykotaUserName" : uname, |
---|
[1742] | 917 | "pykotaLimitBy" : (user.LimitBy or "quota"), |
---|
[2054] | 918 | "pykotaOverCharge" : str(user.OverCharge), |
---|
[2721] | 919 | "description" : self.userCharsetToDatabase(user.Description or "") |
---|
[1105] | 920 | } |
---|
[1742] | 921 | |
---|
[1224] | 922 | if user.Email : |
---|
| 923 | newfields.update({self.info["usermail"]: user.Email}) |
---|
[1105] | 924 | mustadd = 1 |
---|
| 925 | if self.info["newuser"].lower() != 'below' : |
---|
[1510] | 926 | try : |
---|
| 927 | (where, action) = [s.strip() for s in self.info["newuser"].split(",")] |
---|
| 928 | except ValueError : |
---|
| 929 | (where, action) = (self.info["newuser"].strip(), "fail") |
---|
[2652] | 930 | result = self.doSearch("(&(objectClass=%s)(%s=%s))" % \ |
---|
| 931 | (where, self.info["userrdn"], uname), \ |
---|
| 932 | None, \ |
---|
| 933 | base=self.info["userbase"]) |
---|
[1105] | 934 | if result : |
---|
| 935 | (dn, fields) = result[0] |
---|
[2188] | 936 | oc = fields.get("objectClass", fields.get("objectclass", [])) |
---|
| 937 | oc.extend(["pykotaAccount", "pykotaAccountBalance"]) |
---|
[1105] | 938 | fields.update(newfields) |
---|
[1742] | 939 | fields.update({ "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 940 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), }) |
---|
[1105] | 941 | self.doModify(dn, fields) |
---|
| 942 | mustadd = 0 |
---|
[1510] | 943 | else : |
---|
[1534] | 944 | message = _("Unable to find an existing objectClass %s entry with %s=%s to attach pykotaAccount objectClass") % (where, self.info["userrdn"], user.Name) |
---|
[1510] | 945 | if action.lower() == "warn" : |
---|
[2191] | 946 | self.tool.printInfo(_("%s. A new entry will be created instead.") % message, "warn") |
---|
[1510] | 947 | else : # 'fail' or incorrect setting |
---|
| 948 | raise PyKotaStorageError, "%s. Action aborted. Please check your configuration." % message |
---|
[1105] | 949 | |
---|
| 950 | if mustadd : |
---|
[1742] | 951 | if self.info["userbase"] == self.info["balancebase"] : |
---|
[2652] | 952 | fields = { self.info["userrdn"] : uname, |
---|
[1742] | 953 | "objectClass" : ["pykotaObject", "pykotaAccount", "pykotaAccountBalance"], |
---|
[2652] | 954 | "cn" : uname, |
---|
[1742] | 955 | "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 956 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), |
---|
| 957 | } |
---|
| 958 | else : |
---|
[2652] | 959 | fields = { self.info["userrdn"] : uname, |
---|
[1742] | 960 | "objectClass" : ["pykotaObject", "pykotaAccount"], |
---|
[2652] | 961 | "cn" : uname, |
---|
[1742] | 962 | } |
---|
[1105] | 963 | fields.update(newfields) |
---|
[2652] | 964 | dn = "%s=%s,%s" % (self.info["userrdn"], uname, self.info["userbase"]) |
---|
[1105] | 965 | self.doAdd(dn, fields) |
---|
[1742] | 966 | if self.info["userbase"] != self.info["balancebase"] : |
---|
[2652] | 967 | fields = { self.info["balancerdn"] : uname, |
---|
[1742] | 968 | "objectClass" : ["pykotaObject", "pykotaAccountBalance"], |
---|
[2652] | 969 | "cn" : uname, |
---|
[1742] | 970 | "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 971 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), |
---|
| 972 | } |
---|
[2652] | 973 | dn = "%s=%s,%s" % (self.info["balancerdn"], uname, self.info["balancebase"]) |
---|
[1742] | 974 | self.doAdd(dn, fields) |
---|
[2773] | 975 | user.idbalance = dn |
---|
| 976 | if user.PaymentsBacklog : |
---|
| 977 | for (value, comment) in user.PaymentsBacklog : |
---|
| 978 | self.writeNewPayment(user, value, comment) |
---|
| 979 | user.PaymentsBacklog = [] |
---|
| 980 | user.isDirty = False |
---|
| 981 | return None # the entry created doesn't need further modification |
---|
[1016] | 982 | |
---|
[1041] | 983 | def addGroup(self, group) : |
---|
[2773] | 984 | """Adds a group to the quota storage, returns the old value if it already exists.""" |
---|
| 985 | oldentry = self.getGroup(group.Name) |
---|
| 986 | if oldentry.Exists : |
---|
| 987 | return oldentry # we return the existing entry |
---|
[2652] | 988 | gname = self.userCharsetToDatabase(group.Name) |
---|
[1105] | 989 | newfields = { |
---|
[2652] | 990 | "pykotaGroupName" : gname, |
---|
[2054] | 991 | "pykotaLimitBy" : (group.LimitBy or "quota"), |
---|
[2721] | 992 | "description" : self.userCharsetToDatabase(group.Description or "") |
---|
[1105] | 993 | } |
---|
| 994 | mustadd = 1 |
---|
| 995 | if self.info["newgroup"].lower() != 'below' : |
---|
[1510] | 996 | try : |
---|
| 997 | (where, action) = [s.strip() for s in self.info["newgroup"].split(",")] |
---|
| 998 | except ValueError : |
---|
| 999 | (where, action) = (self.info["newgroup"].strip(), "fail") |
---|
[2652] | 1000 | result = self.doSearch("(&(objectClass=%s)(%s=%s))" % \ |
---|
| 1001 | (where, self.info["grouprdn"], gname), \ |
---|
| 1002 | None, \ |
---|
| 1003 | base=self.info["groupbase"]) |
---|
[1105] | 1004 | if result : |
---|
| 1005 | (dn, fields) = result[0] |
---|
[2188] | 1006 | oc = fields.get("objectClass", fields.get("objectclass", [])) |
---|
| 1007 | oc.extend(["pykotaGroup"]) |
---|
[1105] | 1008 | fields.update(newfields) |
---|
| 1009 | self.doModify(dn, fields) |
---|
| 1010 | mustadd = 0 |
---|
[1510] | 1011 | else : |
---|
| 1012 | message = _("Unable to find an existing entry to attach pykotaGroup objectclass %s") % group.Name |
---|
| 1013 | if action.lower() == "warn" : |
---|
[1584] | 1014 | self.tool.printInfo("%s. A new entry will be created instead." % message, "warn") |
---|
[1510] | 1015 | else : # 'fail' or incorrect setting |
---|
| 1016 | raise PyKotaStorageError, "%s. Action aborted. Please check your configuration." % message |
---|
[1105] | 1017 | |
---|
| 1018 | if mustadd : |
---|
[2652] | 1019 | fields = { self.info["grouprdn"] : gname, |
---|
[1105] | 1020 | "objectClass" : ["pykotaObject", "pykotaGroup"], |
---|
[2652] | 1021 | "cn" : gname, |
---|
[1105] | 1022 | } |
---|
| 1023 | fields.update(newfields) |
---|
[2652] | 1024 | dn = "%s=%s,%s" % (self.info["grouprdn"], gname, self.info["groupbase"]) |
---|
[1105] | 1025 | self.doAdd(dn, fields) |
---|
[2773] | 1026 | group.isDirty = False |
---|
| 1027 | return None # the entry created doesn't need further modification |
---|
[1016] | 1028 | |
---|
[1041] | 1029 | def addUserToGroup(self, user, group) : |
---|
| 1030 | """Adds an user to a group.""" |
---|
[1141] | 1031 | if user.Name not in [u.Name for u in self.getGroupMembers(group)] : |
---|
[1041] | 1032 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
| 1033 | if result : |
---|
| 1034 | fields = result[0][1] |
---|
[1070] | 1035 | if not fields.has_key(self.info["groupmembers"]) : |
---|
| 1036 | fields[self.info["groupmembers"]] = [] |
---|
[2652] | 1037 | fields[self.info["groupmembers"]].append(self.userCharsetToDatabase(user.Name)) |
---|
[1041] | 1038 | self.doModify(group.ident, fields) |
---|
| 1039 | group.Members.append(user) |
---|
| 1040 | |
---|
[2706] | 1041 | def delUserFromGroup(self, user, group) : |
---|
| 1042 | """Removes an user from a group.""" |
---|
[2753] | 1043 | if user.Name in [u.Name for u in self.getGroupMembers(group)] : |
---|
[2750] | 1044 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
| 1045 | if result : |
---|
| 1046 | fields = result[0][1] |
---|
| 1047 | if not fields.has_key(self.info["groupmembers"]) : |
---|
| 1048 | fields[self.info["groupmembers"]] = [] |
---|
| 1049 | try : |
---|
| 1050 | fields[self.info["groupmembers"]].remove(self.userCharsetToDatabase(user.Name)) |
---|
| 1051 | except ValueError : |
---|
| 1052 | pass # TODO : Strange, shouldn't it be there ? |
---|
| 1053 | else : |
---|
| 1054 | self.doModify(group.ident, fields) |
---|
| 1055 | group.Members.remove(user) |
---|
[2706] | 1056 | |
---|
[2749] | 1057 | def addUserPQuota(self, upq) : |
---|
[1041] | 1058 | """Initializes a user print quota on a printer.""" |
---|
[2749] | 1059 | # first check if an entry already exists |
---|
| 1060 | oldentry = self.getUserPQuota(upq.User, upq.Printer) |
---|
| 1061 | if oldentry.Exists : |
---|
| 1062 | return oldentry # we return the existing entry |
---|
[1030] | 1063 | uuid = self.genUUID() |
---|
[2749] | 1064 | uname = self.userCharsetToDatabase(upq.User.Name) |
---|
| 1065 | pname = self.userCharsetToDatabase(upq.Printer.Name) |
---|
[1041] | 1066 | fields = { "cn" : uuid, |
---|
| 1067 | "objectClass" : ["pykotaObject", "pykotaUserPQuota"], |
---|
[2652] | 1068 | "pykotaUserName" : uname, |
---|
| 1069 | "pykotaPrinterName" : pname, |
---|
[2749] | 1070 | "pykotaSoftLimit" : str(upq.SoftLimit), |
---|
| 1071 | "pykotaHardLimit" : str(upq.HardLimit), |
---|
| 1072 | "pykotaDateLimit" : str(upq.DateLimit), |
---|
| 1073 | "pykotaPageCounter" : str(upq.PageCounter or 0), |
---|
| 1074 | "pykotaLifePageCounter" : str(upq.LifePageCounter or 0), |
---|
| 1075 | "pykotaWarnCount" : str(upq.WarnCount or 0), |
---|
| 1076 | "pykotaMaxJobSize" : str(upq.MaxJobSize or 0), |
---|
[1030] | 1077 | } |
---|
[1969] | 1078 | if self.info["userquotabase"].lower() == "user" : |
---|
[2749] | 1079 | dn = "cn=%s,%s" % (uuid, upq.User.ident) |
---|
[1969] | 1080 | else : |
---|
| 1081 | dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) |
---|
[1031] | 1082 | self.doAdd(dn, fields) |
---|
[2749] | 1083 | upq.isDirty = False |
---|
| 1084 | return None # the entry created doesn't need further modification |
---|
[1016] | 1085 | |
---|
[2749] | 1086 | def addGroupPQuota(self, gpq) : |
---|
[1041] | 1087 | """Initializes a group print quota on a printer.""" |
---|
[2749] | 1088 | oldentry = self.getGroupPQuota(gpq.Group, gpq.Printer) |
---|
| 1089 | if oldentry.Exists : |
---|
| 1090 | return oldentry # we return the existing entry |
---|
[1030] | 1091 | uuid = self.genUUID() |
---|
[2749] | 1092 | gname = self.userCharsetToDatabase(gpq.Group.Name) |
---|
| 1093 | pname = self.userCharsetToDatabase(gpq.Printer.Name) |
---|
[1041] | 1094 | fields = { "cn" : uuid, |
---|
| 1095 | "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], |
---|
[2652] | 1096 | "pykotaGroupName" : gname, |
---|
| 1097 | "pykotaPrinterName" : pname, |
---|
[1030] | 1098 | "pykotaDateLimit" : "None", |
---|
| 1099 | } |
---|
[1969] | 1100 | if self.info["groupquotabase"].lower() == "group" : |
---|
[2749] | 1101 | dn = "cn=%s,%s" % (uuid, gpq.Group.ident) |
---|
[1969] | 1102 | else : |
---|
| 1103 | dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) |
---|
[1031] | 1104 | self.doAdd(dn, fields) |
---|
[2749] | 1105 | gpq.isDirty = False |
---|
| 1106 | return None # the entry created doesn't need further modification |
---|
[1016] | 1107 | |
---|
[2686] | 1108 | def savePrinter(self, printer) : |
---|
| 1109 | """Saves the printer to the database in a single operation.""" |
---|
[1041] | 1110 | fields = { |
---|
[2686] | 1111 | "pykotaPassThrough" : (printer.PassThrough and "t") or "f", |
---|
[2768] | 1112 | "pykotaMaxJobSize" : str(printer.MaxJobSize or 0), |
---|
[2686] | 1113 | "description" : self.userCharsetToDatabase(printer.Description or ""), |
---|
[2768] | 1114 | "pykotaPricePerPage" : str(printer.PricePerPage or 0.0), |
---|
| 1115 | "pykotaPricePerJob" : str(printer.PricePerJob or 0.0), |
---|
[1041] | 1116 | } |
---|
| 1117 | self.doModify(printer.ident, fields) |
---|
[1016] | 1118 | |
---|
[2706] | 1119 | def saveUser(self, user) : |
---|
| 1120 | """Saves the user to the database in a single operation.""" |
---|
| 1121 | newfields = { |
---|
| 1122 | "pykotaLimitBy" : (user.LimitBy or "quota"), |
---|
| 1123 | "pykotaOverCharge" : str(user.OverCharge), |
---|
[2721] | 1124 | "description" : self.userCharsetToDatabase(user.Description or ""), |
---|
[2706] | 1125 | } |
---|
| 1126 | if user.Email : |
---|
| 1127 | newfields.update({self.info["usermail"]: user.Email}) |
---|
| 1128 | self.doModify(user.ident, newfields) |
---|
[2054] | 1129 | |
---|
[2707] | 1130 | newfields = { "pykotaBalance" : str(user.AccountBalance or 0.0), |
---|
| 1131 | "pykotaLifeTimePaid" : str(user.LifeTimePaid or 0.0), |
---|
| 1132 | } |
---|
| 1133 | self.doModify(user.idbalance, newfields) |
---|
| 1134 | |
---|
[2706] | 1135 | def saveGroup(self, group) : |
---|
| 1136 | """Saves the group to the database in a single operation.""" |
---|
| 1137 | newfields = { |
---|
| 1138 | "pykotaLimitBy" : (group.LimitBy or "quota"), |
---|
[2721] | 1139 | "description" : self.userCharsetToDatabase(group.Description or ""), |
---|
[2706] | 1140 | } |
---|
| 1141 | self.doModify(group.ident, newfields) |
---|
[1016] | 1142 | |
---|
[1041] | 1143 | def writeUserPQuotaDateLimit(self, userpquota, datelimit) : |
---|
| 1144 | """Sets the date limit permanently for a user print quota.""" |
---|
[1031] | 1145 | fields = { |
---|
[2880] | 1146 | "pykotaDateLimit" : str(datelimit), |
---|
[1031] | 1147 | } |
---|
[1041] | 1148 | return self.doModify(userpquota.ident, fields) |
---|
| 1149 | |
---|
| 1150 | def writeGroupPQuotaDateLimit(self, grouppquota, datelimit) : |
---|
| 1151 | """Sets the date limit permanently for a group print quota.""" |
---|
[1031] | 1152 | fields = { |
---|
[2880] | 1153 | "pykotaDateLimit" : str(datelimit), |
---|
[1031] | 1154 | } |
---|
[1041] | 1155 | return self.doModify(grouppquota.ident, fields) |
---|
[1016] | 1156 | |
---|
[1269] | 1157 | def increaseUserPQuotaPagesCounters(self, userpquota, nbpages) : |
---|
| 1158 | """Increase page counters for a user print quota.""" |
---|
| 1159 | fields = { |
---|
| 1160 | "pykotaPageCounter" : { "operator" : "+", "value" : nbpages, "convert" : int }, |
---|
| 1161 | "pykotaLifePageCounter" : { "operator" : "+", "value" : nbpages, "convert" : int }, |
---|
| 1162 | } |
---|
| 1163 | return self.doModify(userpquota.ident, fields) |
---|
| 1164 | |
---|
| 1165 | def decreaseUserAccountBalance(self, user, amount) : |
---|
| 1166 | """Decreases user's account balance from an amount.""" |
---|
| 1167 | fields = { |
---|
| 1168 | "pykotaBalance" : { "operator" : "-", "value" : amount, "convert" : float }, |
---|
| 1169 | } |
---|
[1356] | 1170 | return self.doModify(user.idbalance, fields, flushcache=1) |
---|
[1269] | 1171 | |
---|
[2452] | 1172 | def writeNewPayment(self, user, amount, comment="") : |
---|
[1522] | 1173 | """Adds a new payment to the payments history.""" |
---|
| 1174 | payments = [] |
---|
| 1175 | for payment in user.Payments : |
---|
[2461] | 1176 | payments.append("%s # %s # %s" % (payment[0], str(payment[1]), base64.encodestring(self.userCharsetToDatabase(payment[2])).strip())) |
---|
[2460] | 1177 | payments.append("%s # %s # %s" % (str(DateTime.now()), str(amount), base64.encodestring(self.userCharsetToDatabase(comment)).strip())) |
---|
[1522] | 1178 | fields = { |
---|
| 1179 | "pykotaPayments" : payments, |
---|
| 1180 | } |
---|
| 1181 | return self.doModify(user.idbalance, fields) |
---|
| 1182 | |
---|
[1203] | 1183 | def writeLastJobSize(self, lastjob, jobsize, jobprice) : |
---|
[1041] | 1184 | """Sets the last job's size permanently.""" |
---|
| 1185 | fields = { |
---|
| 1186 | "pykotaJobSize" : str(jobsize), |
---|
[1203] | 1187 | "pykotaJobPrice" : str(jobprice), |
---|
[1041] | 1188 | } |
---|
| 1189 | self.doModify(lastjob.ident, fields) |
---|
| 1190 | |
---|
[2455] | 1191 | def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) : |
---|
[1041] | 1192 | """Adds a job in a printer's history.""" |
---|
[2652] | 1193 | uname = self.userCharsetToDatabase(user.Name) |
---|
| 1194 | pname = self.userCharsetToDatabase(printer.Name) |
---|
[1149] | 1195 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 1196 | uuid = self.genUUID() |
---|
| 1197 | dn = "cn=%s,%s" % (uuid, self.info["jobbase"]) |
---|
| 1198 | else : |
---|
| 1199 | uuid = printer.LastJob.ident[3:].split(",")[0] |
---|
| 1200 | dn = printer.LastJob.ident |
---|
[1875] | 1201 | if self.privacy : |
---|
| 1202 | # For legal reasons, we want to hide the title, filename and options |
---|
[2287] | 1203 | title = filename = options = "hidden" |
---|
[1032] | 1204 | fields = { |
---|
| 1205 | "objectClass" : ["pykotaObject", "pykotaJob"], |
---|
| 1206 | "cn" : uuid, |
---|
[2652] | 1207 | "pykotaUserName" : uname, |
---|
| 1208 | "pykotaPrinterName" : pname, |
---|
[1032] | 1209 | "pykotaJobId" : jobid, |
---|
| 1210 | "pykotaPrinterPageCounter" : str(pagecounter), |
---|
| 1211 | "pykotaAction" : action, |
---|
[1790] | 1212 | "pykotaFileName" : ((filename is None) and "None") or self.userCharsetToDatabase(filename), |
---|
| 1213 | "pykotaTitle" : ((title is None) and "None") or self.userCharsetToDatabase(title), |
---|
[1200] | 1214 | "pykotaCopies" : str(copies), |
---|
[1790] | 1215 | "pykotaOptions" : ((options is None) and "None") or self.userCharsetToDatabase(options), |
---|
[1502] | 1216 | "pykotaHostName" : str(clienthost), |
---|
[1520] | 1217 | "pykotaJobSizeBytes" : str(jobsizebytes), |
---|
[2057] | 1218 | "pykotaMD5Sum" : str(jobmd5sum), |
---|
[2217] | 1219 | "pykotaPages" : jobpages, # don't add this attribute if it is not set, so no string conversion |
---|
| 1220 | "pykotaBillingCode" : self.userCharsetToDatabase(jobbilling), # don't add this attribute if it is not set, so no string conversion |
---|
[2455] | 1221 | "pykotaPrecomputedJobSize" : str(precomputedsize), |
---|
[2456] | 1222 | "pykotaPrecomputedJobPrice" : str(precomputedprice), |
---|
[1032] | 1223 | } |
---|
[1149] | 1224 | if (not self.disablehistory) or (not printer.LastJob.Exists) : |
---|
| 1225 | if jobsize is not None : |
---|
[1203] | 1226 | fields.update({ "pykotaJobSize" : str(jobsize), "pykotaJobPrice" : str(jobprice) }) |
---|
[1149] | 1227 | self.doAdd(dn, fields) |
---|
| 1228 | else : |
---|
| 1229 | # here we explicitly want to reset jobsize to 'None' if needed |
---|
[1203] | 1230 | fields.update({ "pykotaJobSize" : str(jobsize), "pykotaJobPrice" : str(jobprice) }) |
---|
[1149] | 1231 | self.doModify(dn, fields) |
---|
| 1232 | |
---|
[1041] | 1233 | if printer.LastJob.Exists : |
---|
[1032] | 1234 | fields = { |
---|
| 1235 | "pykotaLastJobIdent" : uuid, |
---|
| 1236 | } |
---|
[1041] | 1237 | self.doModify(printer.LastJob.lastjobident, fields) |
---|
[1032] | 1238 | else : |
---|
| 1239 | lastjuuid = self.genUUID() |
---|
[1067] | 1240 | lastjdn = "cn=%s,%s" % (lastjuuid, self.info["lastjobbase"]) |
---|
[1032] | 1241 | fields = { |
---|
| 1242 | "objectClass" : ["pykotaObject", "pykotaLastJob"], |
---|
| 1243 | "cn" : lastjuuid, |
---|
[2652] | 1244 | "pykotaPrinterName" : pname, |
---|
[1032] | 1245 | "pykotaLastJobIdent" : uuid, |
---|
| 1246 | } |
---|
| 1247 | self.doAdd(lastjdn, fields) |
---|
[1041] | 1248 | |
---|
[2735] | 1249 | def saveUserPQuota(self, userpquota) : |
---|
| 1250 | """Saves an user print quota entry.""" |
---|
[1041] | 1251 | fields = { |
---|
[2735] | 1252 | "pykotaSoftLimit" : str(userpquota.SoftLimit), |
---|
| 1253 | "pykotaHardLimit" : str(userpquota.HardLimit), |
---|
| 1254 | "pykotaDateLimit" : str(userpquota.DateLimit), |
---|
[2749] | 1255 | "pykotaWarnCount" : str(userpquota.WarnCount or 0), |
---|
| 1256 | "pykotaPageCounter" : str(userpquota.PageCounter or 0), |
---|
| 1257 | "pykotaLifePageCounter" : str(userpquota.LifePageCounter or 0), |
---|
| 1258 | "pykotaMaxJobSize" : str(userpquota.MaxJobSize or 0), |
---|
[1041] | 1259 | } |
---|
| 1260 | self.doModify(userpquota.ident, fields) |
---|
| 1261 | |
---|
[2054] | 1262 | def writeUserPQuotaWarnCount(self, userpquota, warncount) : |
---|
| 1263 | """Sets the warn counter value for a user quota.""" |
---|
| 1264 | fields = { |
---|
| 1265 | "pykotaWarnCount" : str(warncount or 0), |
---|
| 1266 | } |
---|
| 1267 | self.doModify(userpquota.ident, fields) |
---|
| 1268 | |
---|
| 1269 | def increaseUserPQuotaWarnCount(self, userpquota) : |
---|
| 1270 | """Increases the warn counter value for a user quota.""" |
---|
| 1271 | fields = { |
---|
| 1272 | "pykotaWarnCount" : { "operator" : "+", "value" : 1, "convert" : int }, |
---|
| 1273 | } |
---|
| 1274 | return self.doModify(userpquota.ident, fields) |
---|
| 1275 | |
---|
[2735] | 1276 | def saveGroupPQuota(self, grouppquota) : |
---|
| 1277 | """Saves a group print quota entry.""" |
---|
[1041] | 1278 | fields = { |
---|
[2735] | 1279 | "pykotaSoftLimit" : str(grouppquota.SoftLimit), |
---|
| 1280 | "pykotaHardLimit" : str(grouppquota.HardLimit), |
---|
| 1281 | "pykotaDateLimit" : str(grouppquota.DateLimit), |
---|
[2798] | 1282 | "pykotaMaxJobSize" : str(grouppquota.MaxJobSize or 0), |
---|
[1041] | 1283 | } |
---|
| 1284 | self.doModify(grouppquota.ident, fields) |
---|
| 1285 | |
---|
[1258] | 1286 | def writePrinterToGroup(self, pgroup, printer) : |
---|
| 1287 | """Puts a printer into a printer group.""" |
---|
[1259] | 1288 | if printer.ident not in pgroup.uniqueMember : |
---|
[1269] | 1289 | pgroup.uniqueMember.append(printer.ident) |
---|
[1258] | 1290 | fields = { |
---|
[1269] | 1291 | "uniqueMember" : pgroup.uniqueMember |
---|
[1258] | 1292 | } |
---|
| 1293 | self.doModify(pgroup.ident, fields) |
---|
[1274] | 1294 | |
---|
[1332] | 1295 | def removePrinterFromGroup(self, pgroup, printer) : |
---|
| 1296 | """Removes a printer from a printer group.""" |
---|
| 1297 | try : |
---|
| 1298 | pgroup.uniqueMember.remove(printer.ident) |
---|
| 1299 | except ValueError : |
---|
| 1300 | pass |
---|
| 1301 | else : |
---|
| 1302 | fields = { |
---|
| 1303 | "uniqueMember" : pgroup.uniqueMember, |
---|
| 1304 | } |
---|
| 1305 | self.doModify(pgroup.ident, fields) |
---|
| 1306 | |
---|
[2266] | 1307 | def retrieveHistory(self, user=None, printer=None, hostname=None, billingcode=None, limit=100, start=None, end=None) : |
---|
| 1308 | """Retrieves all print jobs for user on printer (or all) between start and end date, limited to first 100 results.""" |
---|
[1274] | 1309 | precond = "(objectClass=pykotaJob)" |
---|
| 1310 | where = [] |
---|
[2222] | 1311 | if user is not None : |
---|
[2652] | 1312 | where.append("(pykotaUserName=%s)" % self.userCharsetToDatabase(user.Name)) |
---|
[2222] | 1313 | if printer is not None : |
---|
[2652] | 1314 | where.append("(pykotaPrinterName=%s)" % self.userCharsetToDatabase(printer.Name)) |
---|
[1502] | 1315 | if hostname is not None : |
---|
| 1316 | where.append("(pykotaHostName=%s)" % hostname) |
---|
[2218] | 1317 | if billingcode is not None : |
---|
| 1318 | where.append("(pykotaBillingCode=%s)" % self.userCharsetToDatabase(billingcode)) |
---|
[1274] | 1319 | if where : |
---|
| 1320 | where = "(&%s)" % "".join([precond] + where) |
---|
| 1321 | else : |
---|
| 1322 | where = precond |
---|
| 1323 | jobs = [] |
---|
[2211] | 1324 | result = self.doSearch(where, fields=[ "pykotaJobSizeBytes", |
---|
| 1325 | "pykotaHostName", |
---|
| 1326 | "pykotaUserName", |
---|
| 1327 | "pykotaPrinterName", |
---|
| 1328 | "pykotaJobId", |
---|
| 1329 | "pykotaPrinterPageCounter", |
---|
| 1330 | "pykotaAction", |
---|
| 1331 | "pykotaJobSize", |
---|
| 1332 | "pykotaJobPrice", |
---|
| 1333 | "pykotaFileName", |
---|
| 1334 | "pykotaTitle", |
---|
| 1335 | "pykotaCopies", |
---|
| 1336 | "pykotaOptions", |
---|
| 1337 | "pykotaBillingCode", |
---|
| 1338 | "pykotaPages", |
---|
| 1339 | "pykotaMD5Sum", |
---|
[2455] | 1340 | "pykotaPrecomputedJobSize", |
---|
| 1341 | "pykotaPrecomputedJobPrice", |
---|
[2211] | 1342 | "createTimestamp" ], |
---|
| 1343 | base=self.info["jobbase"]) |
---|
[1274] | 1344 | if result : |
---|
| 1345 | for (ident, fields) in result : |
---|
| 1346 | job = StorageJob(self) |
---|
| 1347 | job.ident = ident |
---|
| 1348 | job.JobId = fields.get("pykotaJobId")[0] |
---|
[1392] | 1349 | job.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter", [0])[0] or 0) |
---|
[1601] | 1350 | try : |
---|
| 1351 | job.JobSize = int(fields.get("pykotaJobSize", [0])[0]) |
---|
| 1352 | except ValueError : |
---|
| 1353 | job.JobSize = None |
---|
| 1354 | try : |
---|
| 1355 | job.JobPrice = float(fields.get("pykotaJobPrice", [0.0])[0]) |
---|
| 1356 | except ValueError : |
---|
| 1357 | job.JobPrice = None |
---|
[1392] | 1358 | job.JobAction = fields.get("pykotaAction", [""])[0] |
---|
[1790] | 1359 | job.JobFileName = self.databaseToUserCharset(fields.get("pykotaFileName", [""])[0]) |
---|
| 1360 | job.JobTitle = self.databaseToUserCharset(fields.get("pykotaTitle", [""])[0]) |
---|
[1274] | 1361 | job.JobCopies = int(fields.get("pykotaCopies", [0])[0]) |
---|
[1790] | 1362 | job.JobOptions = self.databaseToUserCharset(fields.get("pykotaOptions", [""])[0]) |
---|
[1502] | 1363 | job.JobHostName = fields.get("pykotaHostName", [""])[0] |
---|
[1520] | 1364 | job.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] |
---|
[2217] | 1365 | job.JobBillingCode = self.databaseToUserCharset(fields.get("pykotaBillingCode", [None])[0]) |
---|
[2211] | 1366 | job.JobMD5Sum = fields.get("pykotaMD5Sum", [None])[0] |
---|
| 1367 | job.JobPages = fields.get("pykotaPages", [""])[0] |
---|
[2455] | 1368 | try : |
---|
| 1369 | job.PrecomputedJobSize = int(fields.get("pykotaPrecomputedJobSize", [0])[0]) |
---|
| 1370 | except ValueError : |
---|
| 1371 | job.PrecomputedJobSize = None |
---|
| 1372 | try : |
---|
| 1373 | job.PrecomputedJobPrice = float(fields.get("pykotaPrecomputedJobPrice", [0.0])[0]) |
---|
| 1374 | except ValueError : |
---|
| 1375 | job.PrecomputedJobPrice = None |
---|
[2287] | 1376 | if job.JobTitle == job.JobFileName == job.JobOptions == "hidden" : |
---|
| 1377 | (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 |
---|
[2538] | 1378 | date = fields.get("createTimestamp", ["19700101000000Z"])[0] # It's in UTC ! |
---|
| 1379 | mxtime = DateTime.strptime(date[:14], "%Y%m%d%H%M%S").localtime() |
---|
| 1380 | job.JobDate = mxtime.strftime("%Y%m%d %H:%M:%S") |
---|
[2266] | 1381 | if ((start is None) and (end is None)) or \ |
---|
| 1382 | ((start is None) and (job.JobDate <= end)) or \ |
---|
| 1383 | ((end is None) and (job.JobDate >= start)) or \ |
---|
| 1384 | ((job.JobDate >= start) and (job.JobDate <= end)) : |
---|
[2652] | 1385 | job.UserName = self.databaseToUserCharset(fields.get("pykotaUserName")[0]) |
---|
| 1386 | job.PrinterName = self.databaseToUserCharset(fields.get("pykotaPrinterName")[0]) |
---|
[1274] | 1387 | job.Exists = 1 |
---|
| 1388 | jobs.append(job) |
---|
[1874] | 1389 | jobs.sort(lambda x, y : cmp(y.JobDate, x.JobDate)) |
---|
[1274] | 1390 | if limit : |
---|
| 1391 | jobs = jobs[:int(limit)] |
---|
| 1392 | return jobs |
---|
[1258] | 1393 | |
---|
[1041] | 1394 | def deleteUser(self, user) : |
---|
| 1395 | """Completely deletes an user from the Quota Storage.""" |
---|
[2652] | 1396 | uname = self.userCharsetToDatabase(user.Name) |
---|
[1692] | 1397 | todelete = [] |
---|
[2652] | 1398 | result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % uname, base=self.info["jobbase"]) |
---|
[1041] | 1399 | for (ident, fields) in result : |
---|
[1692] | 1400 | todelete.append(ident) |
---|
[1998] | 1401 | if self.info["userquotabase"].lower() == "user" : |
---|
| 1402 | base = self.info["userbase"] |
---|
| 1403 | else : |
---|
| 1404 | base = self.info["userquotabase"] |
---|
[2652] | 1405 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s))" % uname, \ |
---|
| 1406 | ["pykotaPrinterName", "pykotaUserName"], \ |
---|
| 1407 | base=base) |
---|
[1041] | 1408 | for (ident, fields) in result : |
---|
[1692] | 1409 | # ensure the user print quota entry will be deleted |
---|
| 1410 | todelete.append(ident) |
---|
| 1411 | |
---|
| 1412 | # if last job of current printer was printed by the user |
---|
| 1413 | # to delete, we also need to delete the printer's last job entry. |
---|
[2652] | 1414 | printer = self.getPrinter(self.databaseToUserCharset(fields["pykotaPrinterName"][0])) |
---|
[1692] | 1415 | if printer.LastJob.UserName == user.Name : |
---|
| 1416 | todelete.append(printer.LastJob.lastjobident) |
---|
| 1417 | |
---|
| 1418 | for ident in todelete : |
---|
[1041] | 1419 | self.doDelete(ident) |
---|
[1692] | 1420 | |
---|
[1041] | 1421 | result = self.doSearch("objectClass=pykotaAccount", None, base=user.ident, scope=ldap.SCOPE_BASE) |
---|
[1032] | 1422 | if result : |
---|
[1041] | 1423 | fields = result[0][1] |
---|
| 1424 | for k in fields.keys() : |
---|
| 1425 | if k.startswith("pykota") : |
---|
| 1426 | del fields[k] |
---|
| 1427 | elif k.lower() == "objectclass" : |
---|
| 1428 | todelete = [] |
---|
| 1429 | for i in range(len(fields[k])) : |
---|
| 1430 | if fields[k][i].startswith("pykota") : |
---|
| 1431 | todelete.append(i) |
---|
| 1432 | todelete.sort() |
---|
| 1433 | todelete.reverse() |
---|
| 1434 | for i in todelete : |
---|
| 1435 | del fields[k][i] |
---|
[1119] | 1436 | if fields.get("objectClass") or fields.get("objectclass") : |
---|
[1041] | 1437 | self.doModify(user.ident, fields, ignoreold=0) |
---|
| 1438 | else : |
---|
| 1439 | self.doDelete(user.ident) |
---|
[2652] | 1440 | result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % \ |
---|
| 1441 | uname, \ |
---|
| 1442 | ["pykotaUserName"], \ |
---|
| 1443 | base=self.info["balancebase"]) |
---|
[1041] | 1444 | for (ident, fields) in result : |
---|
| 1445 | self.doDelete(ident) |
---|
| 1446 | |
---|
| 1447 | def deleteGroup(self, group) : |
---|
| 1448 | """Completely deletes a group from the Quota Storage.""" |
---|
[2652] | 1449 | gname = self.userCharsetToDatabase(group.Name) |
---|
[1998] | 1450 | if self.info["groupquotabase"].lower() == "group" : |
---|
| 1451 | base = self.info["groupbase"] |
---|
| 1452 | else : |
---|
| 1453 | base = self.info["groupquotabase"] |
---|
[2652] | 1454 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s))" % \ |
---|
| 1455 | gname, \ |
---|
| 1456 | ["pykotaGroupName"], \ |
---|
| 1457 | base=base) |
---|
[1041] | 1458 | for (ident, fields) in result : |
---|
| 1459 | self.doDelete(ident) |
---|
| 1460 | result = self.doSearch("objectClass=pykotaGroup", None, base=group.ident, scope=ldap.SCOPE_BASE) |
---|
[1016] | 1461 | if result : |
---|
[1027] | 1462 | fields = result[0][1] |
---|
[1041] | 1463 | for k in fields.keys() : |
---|
| 1464 | if k.startswith("pykota") : |
---|
| 1465 | del fields[k] |
---|
| 1466 | elif k.lower() == "objectclass" : |
---|
| 1467 | todelete = [] |
---|
| 1468 | for i in range(len(fields[k])) : |
---|
| 1469 | if fields[k][i].startswith("pykota") : |
---|
| 1470 | todelete.append(i) |
---|
| 1471 | todelete.sort() |
---|
| 1472 | todelete.reverse() |
---|
| 1473 | for i in todelete : |
---|
| 1474 | del fields[k][i] |
---|
[1119] | 1475 | if fields.get("objectClass") or fields.get("objectclass") : |
---|
[1041] | 1476 | self.doModify(group.ident, fields, ignoreold=0) |
---|
| 1477 | else : |
---|
| 1478 | self.doDelete(group.ident) |
---|
[1330] | 1479 | |
---|
[2765] | 1480 | def deleteManyBillingCodes(self, billingcodes) : |
---|
| 1481 | """Deletes many billing codes.""" |
---|
| 1482 | for bcode in billingcodes : |
---|
[2763] | 1483 | bcode.delete() |
---|
| 1484 | |
---|
[2765] | 1485 | def deleteManyUsers(self, users) : |
---|
| 1486 | """Deletes many users.""" |
---|
| 1487 | for user in users : |
---|
[2763] | 1488 | user.delete() |
---|
| 1489 | |
---|
[2765] | 1490 | def deleteManyGroups(self, groups) : |
---|
| 1491 | """Deletes many groups.""" |
---|
| 1492 | for group in groups : |
---|
[2763] | 1493 | group.delete() |
---|
| 1494 | |
---|
[2765] | 1495 | def deleteManyPrinters(self, printers) : |
---|
| 1496 | """Deletes many printers.""" |
---|
| 1497 | for printer in printers : |
---|
[2763] | 1498 | printer.delete() |
---|
| 1499 | |
---|
[2749] | 1500 | def deleteManyUserPQuotas(self, printers, users) : |
---|
| 1501 | """Deletes many user print quota entries.""" |
---|
| 1502 | # TODO : grab all with a single (possibly VERY huge) filter if possible (might depend on the LDAP server !) |
---|
| 1503 | for printer in printers : |
---|
| 1504 | for user in users : |
---|
| 1505 | upq = self.getUserPQuota(user, printer) |
---|
| 1506 | if upq.Exists : |
---|
| 1507 | upq.delete() |
---|
| 1508 | |
---|
| 1509 | def deleteManyGroupPQuotas(self, printers, groups) : |
---|
| 1510 | """Deletes many group print quota entries.""" |
---|
| 1511 | # TODO : grab all with a single (possibly VERY huge) filter if possible (might depend on the LDAP server !) |
---|
| 1512 | for printer in printers : |
---|
| 1513 | for group in groups : |
---|
| 1514 | gpq = self.getGroupPQuota(group, printer) |
---|
| 1515 | if gpq.Exists : |
---|
| 1516 | gpq.delete() |
---|
| 1517 | |
---|
[2717] | 1518 | def deleteUserPQuota(self, upquota) : |
---|
| 1519 | """Completely deletes an user print quota entry from the database.""" |
---|
| 1520 | uname = self.userCharsetToDatabase(upquota.User.Name) |
---|
| 1521 | pname = self.userCharsetToDatabase(upquota.Printer.Name) |
---|
| 1522 | result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s)(pykotaPrinterName=%s))" \ |
---|
| 1523 | % (uname, pname), \ |
---|
| 1524 | base=self.info["jobbase"]) |
---|
| 1525 | for (ident, fields) in result : |
---|
| 1526 | self.doDelete(ident) |
---|
[2718] | 1527 | if upquota.Printer.LastJob.UserName == upquota.User.Name : |
---|
| 1528 | self.doDelete(upquota.Printer.LastJob.lastjobident) |
---|
[2717] | 1529 | self.doDelete(upquota.ident) |
---|
| 1530 | |
---|
| 1531 | def deleteGroupPQuota(self, gpquota) : |
---|
| 1532 | """Completely deletes a group print quota entry from the database.""" |
---|
| 1533 | self.doDelete(gpquota.ident) |
---|
| 1534 | |
---|
[1330] | 1535 | def deletePrinter(self, printer) : |
---|
[2358] | 1536 | """Completely deletes a printer from the Quota Storage.""" |
---|
[2652] | 1537 | pname = self.userCharsetToDatabase(printer.Name) |
---|
| 1538 | result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaPrinterName=%s))" % pname, base=self.info["lastjobbase"]) |
---|
[1330] | 1539 | for (ident, fields) in result : |
---|
| 1540 | self.doDelete(ident) |
---|
[2652] | 1541 | result = self.doSearch("(&(objectClass=pykotaJob)(pykotaPrinterName=%s))" % pname, base=self.info["jobbase"]) |
---|
[1330] | 1542 | for (ident, fields) in result : |
---|
| 1543 | self.doDelete(ident) |
---|
[1998] | 1544 | if self.info["groupquotabase"].lower() == "group" : |
---|
| 1545 | base = self.info["groupbase"] |
---|
| 1546 | else : |
---|
| 1547 | base = self.info["groupquotabase"] |
---|
[2652] | 1548 | result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s))" % pname, base=base) |
---|
[1330] | 1549 | for (ident, fields) in result : |
---|
| 1550 | self.doDelete(ident) |
---|
[1998] | 1551 | if self.info["userquotabase"].lower() == "user" : |
---|
| 1552 | base = self.info["userbase"] |
---|
| 1553 | else : |
---|
| 1554 | base = self.info["userquotabase"] |
---|
[2652] | 1555 | result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s))" % pname, base=base) |
---|
[1330] | 1556 | for (ident, fields) in result : |
---|
| 1557 | self.doDelete(ident) |
---|
| 1558 | for parent in self.getParentPrinters(printer) : |
---|
[1332] | 1559 | try : |
---|
| 1560 | parent.uniqueMember.remove(printer.ident) |
---|
| 1561 | except ValueError : |
---|
| 1562 | pass |
---|
| 1563 | else : |
---|
| 1564 | fields = { |
---|
| 1565 | "uniqueMember" : parent.uniqueMember, |
---|
| 1566 | } |
---|
| 1567 | self.doModify(parent.ident, fields) |
---|
[1330] | 1568 | self.doDelete(printer.ident) |
---|
[1754] | 1569 | |
---|
[2358] | 1570 | def deleteBillingCode(self, code) : |
---|
| 1571 | """Deletes a billing code from the Quota Storage (no entries are deleted from the history)""" |
---|
| 1572 | self.doDelete(code.ident) |
---|
| 1573 | |
---|
[1990] | 1574 | def extractPrinters(self, extractonly={}) : |
---|
[1754] | 1575 | """Extracts all printer records.""" |
---|
[1993] | 1576 | pname = extractonly.get("printername") |
---|
| 1577 | entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] |
---|
[1754] | 1578 | if entries : |
---|
[2459] | 1579 | result = [ ("dn", "printername", "priceperpage", "priceperjob", "description", "maxjobsize", "passthrough") ] |
---|
[1754] | 1580 | for entry in entries : |
---|
[2459] | 1581 | if entry.PassThrough in (1, "1", "t", "true", "T", "TRUE", "True") : |
---|
| 1582 | passthrough = "t" |
---|
| 1583 | else : |
---|
| 1584 | passthrough = "f" |
---|
| 1585 | result.append((entry.ident, entry.Name, entry.PricePerPage, entry.PricePerJob, entry.Description, entry.MaxJobSize, passthrough)) |
---|
[1754] | 1586 | return result |
---|
| 1587 | |
---|
[1990] | 1588 | def extractUsers(self, extractonly={}) : |
---|
[1754] | 1589 | """Extracts all user records.""" |
---|
[1993] | 1590 | uname = extractonly.get("username") |
---|
| 1591 | entries = [u for u in [self.getUser(name) for name in self.getAllUsersNames(uname)] if u.Exists] |
---|
[1754] | 1592 | if entries : |
---|
[2721] | 1593 | result = [ ("dn", "username", "balance", "lifetimepaid", "limitby", "email", "description") ] |
---|
[1754] | 1594 | for entry in entries : |
---|
[2721] | 1595 | result.append((entry.ident, entry.Name, entry.AccountBalance, entry.LifeTimePaid, entry.LimitBy, entry.Email, entry.Description)) |
---|
[1754] | 1596 | return result |
---|
| 1597 | |
---|
[2358] | 1598 | def extractBillingcodes(self, extractonly={}) : |
---|
| 1599 | """Extracts all billing codes records.""" |
---|
| 1600 | billingcode = extractonly.get("billingcode") |
---|
| 1601 | entries = [b for b in [self.getBillingCode(label) for label in self.getAllBillingCodes(billingcode)] if b.Exists] |
---|
| 1602 | if entries : |
---|
| 1603 | result = [ ("dn", "billingcode", "balance", "pagecounter", "description") ] |
---|
| 1604 | for entry in entries : |
---|
| 1605 | result.append((entry.ident, entry.BillingCode, entry.Balance, entry.PageCounter, entry.Description)) |
---|
| 1606 | return result |
---|
| 1607 | |
---|
[1990] | 1608 | def extractGroups(self, extractonly={}) : |
---|
[1754] | 1609 | """Extracts all group records.""" |
---|
[1993] | 1610 | gname = extractonly.get("groupname") |
---|
| 1611 | entries = [g for g in [self.getGroup(name) for name in self.getAllGroupsNames(gname)] if g.Exists] |
---|
[1754] | 1612 | if entries : |
---|
[2721] | 1613 | result = [ ("dn", "groupname", "limitby", "balance", "lifetimepaid", "description") ] |
---|
[1754] | 1614 | for entry in entries : |
---|
[2721] | 1615 | result.append((entry.ident, entry.Name, entry.LimitBy, entry.AccountBalance, entry.LifeTimePaid, entry.Description)) |
---|
[1754] | 1616 | return result |
---|
| 1617 | |
---|
[1990] | 1618 | def extractPayments(self, extractonly={}) : |
---|
[1754] | 1619 | """Extracts all payment records.""" |
---|
[1993] | 1620 | uname = extractonly.get("username") |
---|
| 1621 | entries = [u for u in [self.getUser(name) for name in self.getAllUsersNames(uname)] if u.Exists] |
---|
[1765] | 1622 | if entries : |
---|
[2452] | 1623 | result = [ ("username", "amount", "date", "description") ] |
---|
[1765] | 1624 | for entry in entries : |
---|
[2452] | 1625 | for (date, amount, description) in entry.Payments : |
---|
| 1626 | result.append((entry.Name, amount, date, description)) |
---|
[1765] | 1627 | return result |
---|
[1754] | 1628 | |
---|
[1990] | 1629 | def extractUpquotas(self, extractonly={}) : |
---|
[1754] | 1630 | """Extracts all userpquota records.""" |
---|
[1993] | 1631 | pname = extractonly.get("printername") |
---|
| 1632 | entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] |
---|
[1754] | 1633 | if entries : |
---|
[1995] | 1634 | result = [ ("username", "printername", "dn", "userdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") ] |
---|
[2040] | 1635 | uname = extractonly.get("username") |
---|
[1764] | 1636 | for entry in entries : |
---|
[2041] | 1637 | for (user, userpquota) in self.getPrinterUsersAndQuotas(entry, names=[uname or "*"]) : |
---|
| 1638 | result.append((user.Name, entry.Name, userpquota.ident, user.ident, entry.ident, userpquota.LifePageCounter, userpquota.PageCounter, userpquota.SoftLimit, userpquota.HardLimit, userpquota.DateLimit)) |
---|
[1768] | 1639 | return result |
---|
[1754] | 1640 | |
---|
[1990] | 1641 | def extractGpquotas(self, extractonly={}) : |
---|
[1754] | 1642 | """Extracts all grouppquota records.""" |
---|
[1993] | 1643 | pname = extractonly.get("printername") |
---|
| 1644 | entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] |
---|
[1754] | 1645 | if entries : |
---|
[1995] | 1646 | result = [ ("groupname", "printername", "dn", "groupdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") ] |
---|
[1993] | 1647 | gname = extractonly.get("groupname") |
---|
[1764] | 1648 | for entry in entries : |
---|
[2042] | 1649 | for (group, grouppquota) in self.getPrinterGroupsAndQuotas(entry, names=[gname or "*"]) : |
---|
| 1650 | result.append((group.Name, entry.Name, grouppquota.ident, group.ident, entry.ident, grouppquota.LifePageCounter, grouppquota.PageCounter, grouppquota.SoftLimit, grouppquota.HardLimit, grouppquota.DateLimit)) |
---|
[1768] | 1651 | return result |
---|
[1754] | 1652 | |
---|
[1990] | 1653 | def extractUmembers(self, extractonly={}) : |
---|
[1754] | 1654 | """Extracts all user groups members.""" |
---|
[1993] | 1655 | gname = extractonly.get("groupname") |
---|
| 1656 | entries = [g for g in [self.getGroup(name) for name in self.getAllGroupsNames(gname)] if g.Exists] |
---|
[1754] | 1657 | if entries : |
---|
[1995] | 1658 | result = [ ("groupname", "username", "groupdn", "userdn") ] |
---|
[1993] | 1659 | uname = extractonly.get("username") |
---|
[1754] | 1660 | for entry in entries : |
---|
| 1661 | for member in entry.Members : |
---|
[1993] | 1662 | if (uname is None) or (member.Name == uname) : |
---|
| 1663 | result.append((entry.Name, member.Name, entry.ident, member.ident)) |
---|
[1754] | 1664 | return result |
---|
| 1665 | |
---|
[1990] | 1666 | def extractPmembers(self, extractonly={}) : |
---|
[1754] | 1667 | """Extracts all printer groups members.""" |
---|
[1993] | 1668 | pname = extractonly.get("printername") |
---|
| 1669 | entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] |
---|
[1754] | 1670 | if entries : |
---|
[1995] | 1671 | result = [ ("pgroupname", "printername", "pgroupdn", "printerdn") ] |
---|
[1993] | 1672 | pgname = extractonly.get("pgroupname") |
---|
[1754] | 1673 | for entry in entries : |
---|
| 1674 | for parent in self.getParentPrinters(entry) : |
---|
[1993] | 1675 | if (pgname is None) or (parent.Name == pgname) : |
---|
| 1676 | result.append((parent.Name, entry.Name, parent.ident, entry.ident)) |
---|
[1754] | 1677 | return result |
---|
| 1678 | |
---|
[1990] | 1679 | def extractHistory(self, extractonly={}) : |
---|
[1754] | 1680 | """Extracts all jobhistory records.""" |
---|
[1993] | 1681 | uname = extractonly.get("username") |
---|
| 1682 | if uname : |
---|
| 1683 | user = self.getUser(uname) |
---|
| 1684 | else : |
---|
| 1685 | user = None |
---|
| 1686 | pname = extractonly.get("printername") |
---|
| 1687 | if pname : |
---|
| 1688 | printer = self.getPrinter(pname) |
---|
| 1689 | else : |
---|
| 1690 | printer = None |
---|
[2266] | 1691 | startdate = extractonly.get("start") |
---|
| 1692 | enddate = extractonly.get("end") |
---|
| 1693 | (startdate, enddate) = self.cleanDates(startdate, enddate) |
---|
| 1694 | entries = self.retrieveHistory(user, printer, hostname=extractonly.get("hostname"), billingcode=extractonly.get("billingcode"), limit=None, start=startdate, end=enddate) |
---|
[1754] | 1695 | if entries : |
---|
[2455] | 1696 | result = [ ("username", "printername", "dn", "jobid", "pagecounter", "jobsize", "action", "jobdate", "filename", "title", "copies", "options", "jobprice", "hostname", "jobsizebytes", "md5sum", "pages", "billingcode", "precomputedjobsize", "precomputedjobprice") ] |
---|
[1754] | 1697 | for entry in entries : |
---|
[2455] | 1698 | result.append((entry.UserName, entry.PrinterName, entry.ident, entry.JobId, entry.PrinterPageCounter, entry.JobSize, entry.JobAction, entry.JobDate, entry.JobFileName, entry.JobTitle, entry.JobCopies, entry.JobOptions, entry.JobPrice, entry.JobHostName, entry.JobSizeBytes, entry.JobMD5Sum, entry.JobPages, entry.JobBillingCode, entry.PrecomputedJobSize, entry.PrecomputedJobPrice)) |
---|
[2358] | 1699 | return result |
---|
| 1700 | |
---|
[2372] | 1701 | def getBillingCodeFromBackend(self, label) : |
---|
| 1702 | """Extracts billing code information given its label : returns first matching billing code.""" |
---|
| 1703 | code = StorageBillingCode(self, label) |
---|
[2375] | 1704 | ulabel = self.userCharsetToDatabase(label) |
---|
[2652] | 1705 | result = self.doSearch("(&(objectClass=pykotaBilling)(pykotaBillingCode=%s))" % \ |
---|
| 1706 | ulabel, \ |
---|
| 1707 | ["pykotaBillingCode", "pykotaBalance", "pykotaPageCounter", "description"], \ |
---|
| 1708 | base=self.info["billingcodebase"]) |
---|
[2372] | 1709 | if result : |
---|
| 1710 | fields = result[0][1] # take only first matching code, ignore the rest |
---|
| 1711 | code.ident = result[0][0] |
---|
[2375] | 1712 | code.BillingCode = self.databaseToUserCharset(fields.get("pykotaBillingCode", [ulabel])[0]) |
---|
[2372] | 1713 | code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) |
---|
| 1714 | code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) |
---|
| 1715 | code.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
| 1716 | code.Exists = 1 |
---|
| 1717 | return code |
---|
[2375] | 1718 | |
---|
[2765] | 1719 | def addBillingCode(self, bcode) : |
---|
[2375] | 1720 | """Adds a billing code to the quota storage, returns it.""" |
---|
[2765] | 1721 | oldentry = self.getBillingCode(bcode.BillingCode) |
---|
| 1722 | if oldentry.Exists : |
---|
| 1723 | return oldentry # we return the existing entry |
---|
[2375] | 1724 | uuid = self.genUUID() |
---|
| 1725 | dn = "cn=%s,%s" % (uuid, self.info["billingcodebase"]) |
---|
| 1726 | fields = { "objectClass" : ["pykotaObject", "pykotaBilling"], |
---|
| 1727 | "cn" : uuid, |
---|
[2765] | 1728 | "pykotaBillingCode" : self.userCharsetToDatabase(bcode.BillingCode), |
---|
| 1729 | "pykotaPageCounter" : str(bcode.PageCounter or 0), |
---|
| 1730 | "pykotaBalance" : str(bcode.Balance or 0.0), |
---|
| 1731 | "description" : self.userCharsetToDatabase(bcode.Description or ""), |
---|
[2375] | 1732 | } |
---|
| 1733 | self.doAdd(dn, fields) |
---|
[2765] | 1734 | bcode.isDirty = False |
---|
| 1735 | return None # the entry created doesn't need further modification |
---|
[2375] | 1736 | |
---|
[2765] | 1737 | def saveBillingCode(self, bcode) : |
---|
[2375] | 1738 | """Sets the new description for a billing code.""" |
---|
| 1739 | fields = { |
---|
[2765] | 1740 | "description" : self.userCharsetToDatabase(bcode.Description or ""), |
---|
| 1741 | "pykotaPageCounter" : str(bcode.PageCounter or 0), |
---|
| 1742 | "pykotaBalance" : str(bcode.Balance or 0.0), |
---|
[2375] | 1743 | } |
---|
[2765] | 1744 | self.doModify(bcode.ident, fields) |
---|
[2358] | 1745 | |
---|
[2380] | 1746 | def getMatchingBillingCodes(self, billingcodepattern) : |
---|
| 1747 | """Returns the list of all billing codes which match a certain pattern.""" |
---|
| 1748 | codes = [] |
---|
[2657] | 1749 | result = self.doSearch("objectClass=pykotaBilling", \ |
---|
[2380] | 1750 | ["pykotaBillingCode", "description", "pykotaPageCounter", "pykotaBalance"], \ |
---|
| 1751 | base=self.info["billingcodebase"]) |
---|
| 1752 | if result : |
---|
[2657] | 1753 | patterns = billingcodepattern.split(",") |
---|
[2776] | 1754 | try : |
---|
| 1755 | patdict = {}.fromkeys(patterns) |
---|
| 1756 | except AttributeError : |
---|
| 1757 | # Python v2.2 or earlier |
---|
| 1758 | patdict = {} |
---|
| 1759 | for p in patterns : |
---|
| 1760 | patdict[p] = None |
---|
[2380] | 1761 | for (codeid, fields) in result : |
---|
[2678] | 1762 | codename = self.databaseToUserCharset(fields.get("pykotaBillingCode", [""])[0]) |
---|
[2776] | 1763 | if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : |
---|
[2657] | 1764 | code = StorageBillingCode(self, codename) |
---|
| 1765 | code.ident = codeid |
---|
| 1766 | code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) |
---|
| 1767 | code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) |
---|
| 1768 | code.Description = self.databaseToUserCharset(fields.get("description", [""])[0]) |
---|
| 1769 | code.Exists = 1 |
---|
| 1770 | codes.append(code) |
---|
| 1771 | self.cacheEntry("BILLINGCODES", code.BillingCode, code) |
---|
[2380] | 1772 | return codes |
---|
| 1773 | |
---|
[2765] | 1774 | def consumeBillingCode(self, bcode, pagecounter, balance) : |
---|
[2384] | 1775 | """Consumes from a billing code.""" |
---|
| 1776 | fields = { |
---|
| 1777 | "pykotaBalance" : { "operator" : "-", "value" : balance, "convert" : float }, |
---|
| 1778 | "pykotaPageCounter" : { "operator" : "+", "value" : pagecounter, "convert" : int }, |
---|
| 1779 | } |
---|
[2765] | 1780 | return self.doModify(bcode.ident, fields) |
---|