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