Index: pykota/trunk/pykota/storages/ldapstorage.py
===================================================================
--- pykota/trunk/pykota/storages/ldapstorage.py (revision 1016)
+++ pykota/trunk/pykota/storages/ldapstorage.py (revision 1017)
@@ -21,4 +21,8 @@
 #
 # $Log$
+# Revision 1.2  2003/06/06 14:21:08  jalet
+# New LDAP schema.
+# Small bug fixes.
+#
 # Revision 1.1  2003/06/05 11:19:13  jalet
 # More good work on LDAP storage.
@@ -64,9 +68,9 @@
             self.closed = 1
         
-    def doSearch(self, key, fields, base="") :
+    def doSearch(self, key, fields, base="", scope=ldap.SCOPE_SUBTREE) :
         """Does an LDAP search query."""
         try :
             # prepends something more restrictive at the beginning of the base dn
-            result = self.database.search_s(base or self.basedn, ldap.SCOPE_SUBTREE, key, fields)
+            result = self.database.search_s(base or self.basedn, scope, key, fields)
         except ldap.NO_SUCH_OBJECT :    
             return
@@ -79,6 +83,4 @@
         if result :
             return [(printerid, printer["pykotaPrinterName"][0]) for (printerid, printer) in result if fnmatch.fnmatchcase(printer["pykotaPrinterName"][0], printerpattern)]
-        else :    
-            return
             
     def getPrinterId(self, printername) :        
@@ -87,6 +89,4 @@
         if result :
             return result[0][0]
-        else :    
-            return
             
     def getPrinterPrices(self, printerid) :        
@@ -95,6 +95,4 @@
         if result :
             return (float(result[0][1]["pykotaPricePerPage"][0]), float(result[0][1]["pykotaPricePerJob"][0]))
-        else :
-            return 
             
     def setPrinterPrices(self, printerid, perpage, perjob) :
@@ -104,13 +102,11 @@
     def getUserId(self, username) :
         """Returns a userid given a username."""
-        result = self.doSearch("(&(objectClass=pykotaUser)(uid=%s))" % username, ["uid"])
+        result = self.doSearch("(&(objectClass=pykotaAccount)(pykotaUserName=%s))" % username, ["uid"])
         if result :
             return result[0][0]
-        else :    
-            return
             
     def getGroupId(self, groupname) :
         """Returns a groupid given a grupname."""
-        result = self.doSearch("(&(objectClass=pykotaGroup)(cn=%s))" % groupname, ["cn"])
+        result = self.doSearch("(&(objectClass=pykotaGroup)(pykotaGroupName=%s))" % groupname, ["cn"])
         if result is not None :
             (groupid, dummy) = result[0]
@@ -123,25 +119,27 @@
     def getPrinterUsers(self, printerid) :        
         """Returns the list of userids and usernames which uses a given printer."""
-        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(uid=*))", ["uid"], base=printerid) 
-        if result :
-            return [(pquotauserid, fields["uid"][0]) for (pquotauserid, fields) in result]
-        else :
-            return
+        # first get the printer's name from the id
+        result = self.doSearch("objectClass=pykotaPrinter", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE)
+        if result :
+            printername = result[0][1]["pykotaPrinterName"][0]
+            result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s))" % printername, ["pykotaUserName"]) 
+            if result :
+                return [(pquotauserid, fields["pykotaUserName"][0]) for (pquotauserid, fields) in result]
         
     def getPrinterGroups(self, printerid) :        
         """Returns the list of groups which uses a given printer."""
-        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(cn=*))", ["cn"], base=printerid) 
-        if result :
-            return [(pquotagroupid, fields["cn"][0]) for (pquotagroupid, fields) in result]
-        else :
-            return
+        # first get the printer's name from the id
+        result = self.doSearch("objectClass=pykotaPrinter", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE)
+        if result :
+            printername = result[0][1]["pykotaPrinterName"][0]
+            result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaPrinterName=%s))" % printername, ["pykotaGroupName"]) 
+            if result :
+                return [(pquotagroupid, fields["pykotaGroupName"][0]) for (pquotagroupid, fields) in result]
         
     def getGroupMembersNames(self, groupname) :        
         """Returns the list of user's names which are member of this group."""
-        result = self.doSearch("(&(objectClass=pykotaGroup)(cn=%s))" % groupname, ["memberUid"])
+        result = self.doSearch("(&(objectClass=pykotaGroup)(pykotaGroupName=%s))" % groupname, ["memberUid"])
         if result :
             return result[0][1]["memberUid"]
-        else :
-            return
         
     def getUserGroupsNames(self, userid) :        
@@ -173,46 +171,59 @@
         pass
         
-    def getUserBalance(self, userid) :    
-        """Returns the current account balance for a given user."""
-        shortuid = userid.split(",")[0]
-        result = self.doSearch("(&(objectClass=pykotaUser)(%s))" % shortuid, ["pykotaBalance", "pykotaLifeTimePaid"])
-        if result :
-            fields = result[0][1]
-            return (float(fields["pykotaBalance"][0]), float(fields["pykotaLifeTimePaid"][0]))
-        else :    
-            return
-        
-    def getGroupBalance(self, groupid) :    
+    def getUserBalance(self, userquotaid) :    
+        """Returns the current account balance for a given user quota identifier."""
+        # first get the user's name from the user quota id
+        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName"], base=userquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            username = result[0][1]["pykotaUserName"][0]
+            result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % username, ["pykotaBalance", "pykotaLifeTimePaid"])
+            if result :
+                fields = result[0][1]
+                return (float(fields["pykotaBalance"][0]), float(fields["pykotaLifeTimePaid"][0]))
+        
+    def getUserBalanceFromUserId(self, userid) :    
+        """Returns the current account balance for a given user id."""
+        # first get the user's name from the user id
+        result = self.doSearch("objectClass=pykotaAccount", ["pykotaUserName"], base=userid, scope=ldap.SCOPE_BASE)
+        if result :
+            username = result[0][1]["pykotaUserName"][0]
+            result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % username, ["pykotaBalance", "pykotaLifeTimePaid"])
+            if result :
+                fields = result[0][1]
+                return (float(fields["pykotaBalance"][0]), float(fields["pykotaLifeTimePaid"][0]))
+        
+    def getGroupBalance(self, groupquotaid) :    
         """Returns the current account balance for a given group, as the sum of each of its users' account balance."""
-        groupname = groupid.split(",")[0].split("=")[1]
-        members = self.getGroupMembersNames(groupname)
-        balance = lifetimepaid = 0.0
-        for member in members :
-            userid = self.getUserId(member)
-            if userid :
-                userbal = self.getUserBalance(userid)
-                if userbal :
-                    (bal, paid) = userbal
-                    balance += bal
-                    lifetimepaid += paid
-        return (balance, lifetimepaid)            
+        # first get the group's name from the group quota id
+        result = self.doSearch("objectClass=pykotaGroupPQuota", ["pykotaGroupName"], base=groupquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            groupname = result[0][1]["pykotaGroupName"][0]
+            members = self.getGroupMembersNames(groupname)
+            balance = lifetimepaid = 0.0
+            for member in members :
+                userid = self.getUserId(member)
+                if userid :
+                    userbal = self.getUserBalanceFromUserId(userid)
+                    if userbal :
+                        (bal, paid) = userbal
+                        balance += bal
+                        lifetimepaid += paid
+            return (balance, lifetimepaid)            
         
     def getUserLimitBy(self, userid) :    
         """Returns the way in which user printing is limited."""
-        shortuid = userid.split(",")[0]
-        result = self.doSearch("(&(objectClass=pykotaUser)(%s))" % shortuid, ["pykotaLimitBy"])
+        result = self.doSearch("objectClass=pykotaAccount", ["pykotaLimitBy"], base=userid, scope=ldap.SCOPE_BASE)
         if result :
             return result[0][1]["pykotaLimitBy"][0]
-        else :    
-            return
-        
-    def getGroupLimitBy(self, groupid) :    
+        
+    def getGroupLimitBy(self, groupquotaid) :    
         """Returns the way in which group printing is limited."""
-        shortgid = groupid.split(",")[0]
-        result = self.doSearch("(&(objectClass=pykotaGroup)(%s))" % shortgid, ["pykotaLimitBy"])
-        if result :
-            return result[0][1]["pykotaLimitBy"][0]
-        else :    
-            return
+        # first get the group's name from the group quota id
+        result = self.doSearch("objectClass=pykotaGroupPQuota", ["pykotaGroupName"], base=groupquotaid, scope=ldap.SCOPE_BASE)
+        if result :
+            groupname = result[0][1]["pykotaGroupName"][0]
+            result = self.doSearch("(&(objectClass=pykotaGroup)(pykotaGroupName=%s))" % groupname, ["pykotaLimitBy"])
+            if result :
+                return result[0][1]["pykotaLimitBy"][0]
         
     def setUserBalance(self, userid, balance) :    
@@ -248,7 +259,8 @@
         pass
         
-    def getUserPQuota(self, userid, printerid) :
-        """Returns the Print Quota information for a given (userid, printerid)."""
-        result = self.doSearch("(&(objectClass=pykotaUserPQuota)(uid=*))", ["uid", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=userid)
+    def getUserPQuota(self, userquotaid, printerid) :
+        """Returns the Print Quota information for a given (userquotaid, printerid)."""
+        # first get the user's name from the id
+        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName", "pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=userquotaid, scope=ldap.SCOPE_BASE)
         if result :
             fields = result[0][1]
@@ -262,12 +274,11 @@
                      "datelimit" : datelimit
                    }
-        else :
-            return
-        
-    def getGroupPQuota(self, groupid, printerid) :
-        """Returns the Print Quota information for a given (groupid, printerid)."""
-        result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(cn=*))", ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=groupid)
+        
+    def getGroupPQuota(self, grouppquotaid, printerid) :
+        """Returns the Print Quota information for a given (grouppquotaid, printerid)."""
+        result = self.doSearch("objectClass=pykotaGroupPQuota", ["pykotaGroupName", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit"], base=grouppquotaid, scope=ldap.SCOPE_BASE)
         if result :
             fields = result[0][1]
+            groupname = fields["pykotaGroupName"][0]
             datelimit = fields["pykotaDateLimit"][0].strip()
             if (not datelimit) or (datelimit.upper() == "NONE") : 
@@ -278,5 +289,4 @@
                       "datelimit" : datelimit
                     }
-            groupname = groupid.split(",")[0].split("=")[1]
             members = self.getGroupMembersNames(groupname)
             pagecounter = lifepagecounter = 0
@@ -291,6 +301,4 @@
             quota.update({"pagecounter": pagecounter, "lifepagecounter": lifepagecounter})                
             return quota
-        else :
-            return
         
     def setUserDateLimit(self, userid, printerid, datelimit) :
@@ -316,6 +324,4 @@
         if result :
             pass # TODO
-        else :
-            return 
         
     def addUserToGroup(self, userid, groupid) :    
