Show
Ignore:
Timestamp:
02/09/06 00:15:46 (18 years ago)
Author:
jerome
Message:

Huge speed improvements when using the --delete command line option for pkprinters, pkbcodes and edpykota.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/ldapstorage.py

    r2653 r2657  
    357357            fields = result[0][1] 
    358358            user.ident = result[0][0] 
    359             user.Name = fields.get("pykotaUserName", [self.databaseToUserCharset(username)])[0]  
    360359            user.Email = fields.get(self.info["usermail"], [None])[0] 
    361360            user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
     
    670669        printers = [] 
    671670        # see comment at the same place in pgstorage.py 
    672         printerpattern = [self.userCharsetToDatabase(p) for p in printerpattern.split(",")] 
    673         result = self.doSearch("(&(objectClass=pykotaPrinter)(|%s))" % \ 
    674                                   "".join(["(pykotaPrinterName=%s)(%s=%s)" % (pname, self.info["printerrdn"], pname) for pname in printerpattern]), \ 
     671        result = self.doSearch("objectClass=pykotaPrinter", \ 
    675672                                  ["pykotaPrinterName", "pykotaPricePerPage", "pykotaPricePerJob", "pykotaMaxJobSize", "pykotaPassThrough", "uniqueMember", "description"], \ 
    676673                                  base=self.info["printerbase"]) 
    677674        if result : 
     675            patterns = printerpattern.split(",") 
    678676            for (printerid, fields) in result : 
    679677                printername = self.databaseToUserCharset(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) 
    680                 printer = StoragePrinter(self, printername) 
    681                 printer.ident = printerid 
    682                 printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0] or 0.0) 
    683                 printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0] or 0.0) 
    684                 printer.MaxJobSize = int(fields.get("pykotaMaxJobSize", [0])[0]) 
    685                 printer.PassThrough = fields.get("pykotaPassThrough", [None])[0] 
    686                 if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : 
    687                     printer.PassThrough = 1 
    688                 else : 
    689                     printer.PassThrough = 0 
    690                 printer.uniqueMember = fields.get("uniqueMember", []) 
    691                 printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
    692                 printer.Exists = 1 
    693                 printers.append(printer) 
    694                 self.cacheEntry("PRINTERS", printer.Name, printer) 
     678                if self.tool.matchString(printername, patterns) : 
     679                    printer = StoragePrinter(self, printername) 
     680                    printer.ident = printerid 
     681                    printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0] or 0.0) 
     682                    printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0] or 0.0) 
     683                    printer.MaxJobSize = int(fields.get("pykotaMaxJobSize", [0])[0]) 
     684                    printer.PassThrough = fields.get("pykotaPassThrough", [None])[0] 
     685                    if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : 
     686                        printer.PassThrough = 1 
     687                    else : 
     688                        printer.PassThrough = 0 
     689                    printer.uniqueMember = fields.get("uniqueMember", []) 
     690                    printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
     691                    printer.Exists = 1 
     692                    printers.append(printer) 
     693                    self.cacheEntry("PRINTERS", printer.Name, printer) 
    695694        return printers         
     695         
     696    def getMatchingUsers(self, userpattern) : 
     697        """Returns the list of all users for which name matches a certain pattern.""" 
     698        users = [] 
     699        # see comment at the same place in pgstorage.py 
     700        result = self.doSearch("objectClass=pykotaAccount", \ 
     701                                  ["pykotaUserName", "pykotaLimitBy", self.info["usermail"], "pykotaOverCharge"], \ 
     702                                  base=self.info["userbase"]) 
     703        if result : 
     704            patterns = userpattern.split(",") 
     705            for (userid, fields) in result : 
     706                username = self.databaseToUserCharset(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) 
     707                if self.tool.matchString(username, patterns) : 
     708                    user = StorageUser(self, username) 
     709                    user.ident = userid 
     710                    user.Email = fields.get(self.info["usermail"], [None])[0] 
     711                    user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
     712                    user.OverCharge = float(fields.get("pykotaOverCharge", [1.0])[0]) 
     713                    uname = self.userCharsetToDatabase(username) 
     714                    result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % \ 
     715                                              (uname, self.info["balancerdn"], uname), \ 
     716                                              ["pykotaBalance", "pykotaLifeTimePaid", "pykotaPayments"], \ 
     717                                              base=self.info["balancebase"]) 
     718                    if not result : 
     719                        raise PyKotaStorageError, _("No pykotaAccountBalance object found for user %s. Did you create LDAP entries manually ?") % username 
     720                    else : 
     721                        fields = result[0][1] 
     722                        user.idbalance = result[0][0] 
     723                        user.AccountBalance = fields.get("pykotaBalance") 
     724                        if user.AccountBalance is not None : 
     725                            if user.AccountBalance[0].upper() == "NONE" : 
     726                                user.AccountBalance = None 
     727                            else :     
     728                                user.AccountBalance = float(user.AccountBalance[0]) 
     729                        user.AccountBalance = user.AccountBalance or 0.0         
     730                        user.LifeTimePaid = fields.get("pykotaLifeTimePaid") 
     731                        if user.LifeTimePaid is not None : 
     732                            if user.LifeTimePaid[0].upper() == "NONE" : 
     733                                user.LifeTimePaid = None 
     734                            else :     
     735                                user.LifeTimePaid = float(user.LifeTimePaid[0]) 
     736                        user.LifeTimePaid = user.LifeTimePaid or 0.0         
     737                        user.Payments = [] 
     738                        for payment in fields.get("pykotaPayments", []) : 
     739                            try : 
     740                                (date, amount, description) = payment.split(" # ") 
     741                            except ValueError : 
     742                                # Payment with no description (old Payment) 
     743                                (date, amount) = payment.split(" # ") 
     744                                description = "" 
     745                            else :     
     746                                description = self.databaseToUserCharset(base64.decodestring(description)) 
     747                            user.Payments.append((date, float(amount), description)) 
     748                    user.Exists = 1 
     749                    users.append(user) 
     750                    self.cacheEntry("USERS", user.Name, user) 
     751        return users        
     752         
     753    def getMatchingGroups(self, grouppattern) : 
     754        """Returns the list of all groups for which name matches a certain pattern.""" 
     755        groups = [] 
     756        # see comment at the same place in pgstorage.py 
     757        result = self.doSearch("objectClass=pykotaGroup", \ 
     758                                  ["pykotaGroupName", "pykotaLimitBy"], \ 
     759                                  base=self.info["groupbase"]) 
     760        if result : 
     761            patterns = grouppattern.split(",") 
     762            for (groupid, fields) in result : 
     763                groupname = self.databaseToUserCharset(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) 
     764                if self.tool.matchString(groupname, patterns) : 
     765                    group = StorageGroup(self, groupname) 
     766                    group.ident = groupid 
     767                    group.Name = fields.get("pykotaGroupName", [self.databaseToUserCharset(groupname)])[0]  
     768                    group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
     769                    group.AccountBalance = 0.0 
     770                    group.LifeTimePaid = 0.0 
     771                    for member in self.getGroupMembers(group) : 
     772                        if member.Exists : 
     773                            group.AccountBalance += member.AccountBalance 
     774                            group.LifeTimePaid += member.LifeTimePaid 
     775                    group.Exists = 1 
     776        return groups 
    696777         
    697778    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :         
     
    15451626        """Returns the list of all billing codes which match a certain pattern.""" 
    15461627        codes = [] 
    1547         billingcodepattern = [self.userCharsetToDatabase(b) for b in billingcodepattern.split(",")] 
    1548         result = self.doSearch("(&(objectClass=pykotaBilling)(|%s))" % \ 
    1549                                 "".join(["(pykotaBillingCode=%s)" % bcode for bcode in billingcodepattern]), \ 
     1628        result = self.doSearch("objectClass=pykotaBilling", \ 
    15501629                                ["pykotaBillingCode", "description", "pykotaPageCounter", "pykotaBalance"], \ 
    15511630                                base=self.info["billingcodebase"]) 
    15521631        if result : 
     1632            patterns = billingcodepattern.split(",") 
    15531633            for (codeid, fields) in result : 
    1554                 codename = self.databaseToUserCharset(fields.get("pykotaBillingCode", [""])[0]) 
    1555                 code = StorageBillingCode(self, codename) 
    1556                 code.ident = codeid 
    1557                 code.BillingCode = codename 
    1558                 code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) 
    1559                 code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) 
    1560                 code.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
    1561                 code.Exists = 1 
    1562                 codes.append(code) 
    1563                 self.cacheEntry("BILLINGCODES", code.BillingCode, code) 
     1634                bcode = self.databaseToUserCharset(fields.get("pykotaBillingCode", [""])[0]) 
     1635                if self.tool.matchString(bcode, patterns) : 
     1636                    code = StorageBillingCode(self, codename) 
     1637                    code.ident = codeid 
     1638                    code.BillingCode = codename 
     1639                    code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) 
     1640                    code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) 
     1641                    code.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
     1642                    code.Exists = 1 
     1643                    codes.append(code) 
     1644                    self.cacheEntry("BILLINGCODES", code.BillingCode, code) 
    15641645        return codes         
    15651646