Changeset 2722 for pykota

Show
Ignore:
Timestamp:
02/21/06 00:22:17 (19 years ago)
Author:
jerome
Message:

Added userful helpers to retrieve all interesting datas with a single query
(LDAP will use more queries, as usual)

Location:
pykota/trunk/pykota/storages
Files:
2 modified

Legend:

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

    r2721 r2722  
    827827        return usersandquotas 
    828828                 
     829    def getMatchingUserPQuotas(self, pnames = ["*"], unames=["*"]) :     
     830        """Returns all printers, users and users print quota entries which match a set of names.""" 
     831        printers = {} 
     832        users = {} 
     833        upquotas = {} 
     834        for printer in self.getMatchingPrinters(",".join(pnames)) : 
     835            printers[printer.Name] = printer 
     836        for user in self.getMatchingUsers(",".join(unames)) : 
     837            users[user.Name] = user 
     838        for (p, printer) in printers.items() : 
     839            for (u, user) in users.items() : 
     840                upqkey = "%s@%s" % (u, p) 
     841                upquotas[upqkey] = self.getUserPQuota(user, printer) 
     842        return (printers, users, upquotas) 
     843         
     844    def getMatchingGroupPQuotas(self, pnames = ["*"], gnames=["*"]) :     
     845        """Returns all printers, groups and groups print quota entries which match a set of names.""" 
     846        printers = {} 
     847        groups = {} 
     848        gpquotas = {} 
     849        for printer in self.getMatchingPrinters(",".join(pnames)) : 
     850            printers[printer.Name] = printer 
     851        for group in self.getMatchingGroups(",".join(gnames)) : 
     852            groups[group.Name] = group 
     853        for (p, printer) in printers.items() : 
     854            for (g, group) in groups.items() : 
     855                gpqkey = "%s@%s" % (g, p) 
     856                gpquotas[gpqkey] = self.getGroupPQuota(group, printer) 
     857        return (printers, groups, gpquotas) 
     858         
    829859    def getPrinterGroupsAndQuotas(self, printer, names=["*"]) :         
    830860        """Returns the list of groups which uses a given printer, along with their quotas.""" 
  • pykota/trunk/pykota/storages/sql.py

    r2721 r2722  
    462462        return codes         
    463463         
     464    def getMatchingUserPQuotas(self, pnames = ["*"], unames=["*"]) :     
     465        """Returns all printers, users and users print quota entries which match a set of names.""" 
     466        printers = {} 
     467        users = {} 
     468        upquotas = {} 
     469        result = self.doSearch("SELECT users.id AS uid, users.description AS udesc, printers.id AS pid, printers.description AS pdesc, printers.maxjobsize AS pmaxjobsize, userpquota.id AS upqid, userpquota.maxjobsize AS upqmaxjobsize, users.*, printers.*, userpquota.* FROM users, printers, userpquota WHERE users.id=userpquota.userid AND printers.id=userpquota.printerid;") 
     470        if result : 
     471            for record in result : 
     472                uname = self.databaseToUserCharset(record.get("username")) 
     473                pname = self.databaseToUserCharset(record.get("printername")) 
     474                if self.tool.matchString(pname, pnames) and self.tool.matchString(uname, unames) : 
     475                    if not printers.has_key(pname) : 
     476                        printer = StoragePrinter(self, pname) 
     477                        printer.ident = record.get("pid") 
     478                        printer.PricePerJob = record.get("priceperjob") or 0.0 
     479                        printer.PricePerPage = record.get("priceperpage") or 0.0 
     480                        printer.Description = self.databaseToUserCharset(record.get("pdesc") or "")  
     481                        printer.MaxJobSize = record.get("pmaxjobsize") or 0 
     482                        printer.PassThrough = record.get("passthrough") or 0 
     483                        if printer.PassThrough in (1, "1", "t", "true", "TRUE", "True") : 
     484                            printer.PassThrough = 1 
     485                        else : 
     486                            printer.PassThrough = 0 
     487                        printer.Exists = 1 
     488                        printers[pname] = printer 
     489                        self.cacheEntry("PRINTERS", pname, printer) 
     490                     
     491                    if not users.has_key(uname) : 
     492                        user = StorageUser(self, uname) 
     493                        user.ident = record.get("uid") 
     494                        user.LimitBy = record.get("limitby") or "quota" 
     495                        user.AccountBalance = record.get("balance") 
     496                        user.LifeTimePaid = record.get("lifetimepaid") 
     497                        user.Email = record.get("email")  
     498                        user.OverCharge = record.get("overcharge") 
     499                        user.Description = self.databaseToUserCharset(record.get("udesc")) 
     500                        user.Exists = 1 
     501                        users[uname] = user 
     502                        self.cacheEntry("USERS", uname, user) 
     503                     
     504                    upqkey = "%s@%s" % (uname, pname) 
     505                    if not upquotas.has_key(upqkey) : # NB : should always be verified. 
     506                        userpquota = StorageUserPQuota(self, users[uname], printers[pname]) 
     507                        userpquota.ident = record.get("id") 
     508                        userpquota.PageCounter = record.get("pagecounter") 
     509                        userpquota.LifePageCounter = record.get("lifepagecounter") 
     510                        userpquota.SoftLimit = record.get("softlimit") 
     511                        userpquota.HardLimit = record.get("hardlimit") 
     512                        userpquota.DateLimit = record.get("datelimit") 
     513                        userpquota.WarnCount = record.get("warncount") 
     514                        userpquota.Exists = 1 
     515                        upquotas[upqkey] = userpquota 
     516                        self.cacheEntry("USERPQUOTAS", upqkey, userpquota) 
     517        return (printers, users, upquotas) 
     518         
     519    def getMatchingGroupPQuotas(self, pnames = ["*"], gnames=["*"]) :     
     520        """Returns all printers, groups and groups print quota entries which match a set of names.""" 
     521        printers = {} 
     522        groups = {} 
     523        gpquotas = {} 
     524        result = self.doSearch("SELECT printername, groupname FROM printers, groups") 
     525        if result : 
     526            for record in result : 
     527                gname = self.databaseToUserCharset(record.get("groupname")) 
     528                pname = self.databaseToUserCharset(record.get("printername")) 
     529                if self.tool.matchString(pname, pnames) and self.tool.matchString(gname, gnames) : 
     530                    if not printers.has_key(pname) : 
     531                        printers[pname] = self.getPrinter(pname) 
     532                     
     533                    if not groups.has_key(gname) : 
     534                        groups[gname] = self.getGroup(gname) 
     535                     
     536                    gpqkey = "%s@%s" % (gname, pname) 
     537                    if not gpquotas.has_key(gpqkey) : # NB : should always be verified. 
     538                        gpquotas[upqkey] = self.getGroupPQuota(groups[gname], printers[pname]) 
     539        return (printers, groups, gpquotas) 
     540         
    464541    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :         
    465542        """Returns the list of users who uses a given printer, along with their quotas.""" 
    466543        usersandquotas = [] 
    467         result = self.doSearch("SELECT users.id as uid,username,balance,lifetimepaid,limitby,email,overcharge,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit,warncount FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident)) 
     544        result = self.doSearch("SELECT users.id as uid,username,description,balance,lifetimepaid,limitby,email,overcharge,userpquota.id,lifepagecounter,pagecounter,softlimit,hardlimit,datelimit,warncount FROM users JOIN userpquota ON users.id=userpquota.userid AND printerid=%s ORDER BY username ASC" % self.doQuote(printer.ident)) 
    468545        if result : 
    469546            for record in result :