Index: pykota/trunk/conf/pykota.conf.sample
===================================================================
--- pykota/trunk/conf/pykota.conf.sample (revision 707)
+++ pykota/trunk/conf/pykota.conf.sample (revision 713)
@@ -60,5 +60,5 @@
 #
 # Default policy for inexistant users (e.g. root)
-# either accept or deny
-policy: accept
+# either allow or deny
+policy: allow
 
Index: pykota/trunk/pykota/tool.py
===================================================================
--- pykota/trunk/pykota/tool.py (revision 712)
+++ pykota/trunk/pykota/tool.py (revision 713)
@@ -15,4 +15,9 @@
 #
 # $Log$
+# Revision 1.8  2003/02/06 09:19:02  jalet
+# More robust behavior (hopefully) when the user or printer is not managed
+# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
+# printer and/or user not 'yet?' in storage.
+#
 # Revision 1.7  2003/02/06 00:00:45  jalet
 # Now includes the printer name in email messages
@@ -91,30 +96,40 @@
         now = DateTime.now()
         quota = self.storage.getUserPQuota(username, printername)
-        pagecounter = quota["pagecounter"]
-        softlimit = quota["softlimit"]
-        hardlimit = quota["hardlimit"]
-        datelimit = quota["datelimit"]
-        if datelimit is not None :
-            datelimit = DateTime.ISO.ParseDateTime(datelimit)
-        if softlimit is not None :
-            if pagecounter < softlimit :
+        if quota is None :
+            # Unknown user or printer or combination
+            policy = self.config.getPrinterPolicy(printername)
+            if policy in [None, "ALLOW"] :
                 action = "ALLOW"
-            elif hardlimit is not None :
-                 gracedelay = self.config.getGraceDelay()
-                 if softlimit <= pagecounter < hardlimit :    
-                     if datelimit is None :
-                         datelimit = now + gracedelay
-                         self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
-                     if (now + gracedelay) < datelimit :
-                         action = "WARN"
-                     else :    
+            else :    
+                action = "DENY"
+            self.logger.log_message("Unable to match user %s on printer %s, applying default policy (%s)" % (username, printername, action), "warn")
+            return (action, None, None)
+        else :    
+            pagecounter = quota["pagecounter"]
+            softlimit = quota["softlimit"]
+            hardlimit = quota["hardlimit"]
+            datelimit = quota["datelimit"]
+            if datelimit is not None :
+                datelimit = DateTime.ISO.ParseDateTime(datelimit)
+            if softlimit is not None :
+                if pagecounter < softlimit :
+                    action = "ALLOW"
+                elif hardlimit is not None :
+                     gracedelay = self.config.getGraceDelay()
+                     if softlimit <= pagecounter < hardlimit :    
+                         if datelimit is None :
+                             datelimit = now + gracedelay
+                             self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
+                         if (now + gracedelay) < datelimit :
+                             action = "WARN"
+                         else :    
+                             action = "DENY"
+                     else :         
                          action = "DENY"
-                 else :         
-                     action = "DENY"
+                else :        
+                    action = "DENY"
             else :        
-                action = "DENY"
-        else :        
-            action = "ALLOW"
-        return (action, (hardlimit - pagecounter), datelimit)
+                action = "ALLOW"
+            return (action, (hardlimit - pagecounter), datelimit)
     
     def warnQuotaPrinter(self, username) :
@@ -122,8 +137,10 @@
         (action, grace, gracedate) = self.checkUserPQuota(username, self.printername)
         if action == "DENY" :
-            adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername)
-            self.logger.log_message(adminmessage)
-            self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername)
-            self.sendMessageToAdmin("Print Quota", adminmessage)
+            if (grace is not None) and (gracedate is not None) :
+                # only when both user and printer are known
+                adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername)
+                self.logger.log_message(adminmessage)
+                self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername)
+                self.sendMessageToAdmin("Print Quota", adminmessage)
         elif action == "WARN" :    
             adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername)
Index: pykota/trunk/pykota/storages/sql.py
===================================================================
--- pykota/trunk/pykota/storages/sql.py (revision 708)
+++ pykota/trunk/pykota/storages/sql.py (revision 713)
@@ -15,4 +15,9 @@
 #
 # $Log$
+# Revision 1.6  2003/02/06 09:19:02  jalet
+# More robust behavior (hopefully) when the user or printer is not managed
+# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
+# printer and/or user not 'yet?' in storage.
+#
 # Revision 1.5  2003/02/05 23:26:22  jalet
 # Incorrect handling of grace delay
@@ -60,27 +65,34 @@
         
     def addUserPQuota(self, username, printername) :
-        printerid = self.getPrinterId(printername)
+        (userid, printerid) = self.getUPIds(username, printername)
         if printerid is None :    
             self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername))
-            printerid = self.getPrinterId(printername)
-        userid = self.getUserId(username)
         if userid is None :    
             self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username))
-            userid = self.getUserId(username)
-        if (printerid is not None) and (userid is not None) :    
+        (userid, printerid) = self.getUPIds(username, printername)
+        if (userid is not None) and (printerid is not None) :
             return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid)))
         
+    def getUPIds(self, username, printername) :    
+        return (self.getUserId(username), self.getPrinterId(printername))
+        
     def getUserPQuota(self, username, printername) :
-        result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
-        try :
-            return self.doParseResult(result)[0]
-        except TypeError :      # Not found    
-            return
+        (userid, printerid) = self.getUPIds(username, printername)
+        if (userid is not None) and (printerid is not None) :
+            result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid)))
+            try :
+                return self.doParseResult(result)[0]
+            except TypeError :      # Not found    
+                pass
         
     def setUserPQuota(self, username, printername, softlimit, hardlimit) :
-        self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
+        (userid, printerid) = self.getUPIds(username, printername)
+        if (userid is not None) and (printerid is not None) :
+            self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid)))
         
     def updateUserPQuota(self, username, printername, pagecount) :
-        self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername))))
+        (userid, printerid) = self.getUPIds(username, printername)
+        if (userid is not None) and (printerid is not None) :
+            self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid)))
         
     def buyUserPQuota(self, username, printername, pagebought) :
Index: pykota/trunk/pykota/config.py
===================================================================
--- pykota/trunk/pykota/config.py (revision 708)
+++ pykota/trunk/pykota/config.py (revision 713)
@@ -15,4 +15,9 @@
 #
 # $Log$
+# Revision 1.4  2003/02/06 09:19:02  jalet
+# More robust behavior (hopefully) when the user or printer is not managed
+# correctly by the Quota System : e.g. cupsFilter added in ppd file, but
+# printer and/or user not 'yet?' in storage.
+#
 # Revision 1.3  2003/02/05 23:26:22  jalet
 # Incorrect handling of grace delay
@@ -77,6 +82,6 @@
                     raise PyKotaConfigError, "Option %s not found in section %s of %s" % (option, printer, self.filename)
                     
-            validpolicies = [ "accept", "deny" ]     
-            if self.config.get(printer, "policy").lower() not in validpolicies :
+            validpolicies = [ "ALLOW", "DENY" ]     
+            if self.config.get(printer, "policy").upper() not in validpolicies :
                 raise PyKotaConfigError, "Option policy in section %s only supports values in %s" % (printer, str(validrequesters))
             
@@ -119,5 +124,5 @@
     def getPrinterPolicy(self, printer) :    
         """Returns the default policy for the current printer."""
-        return self.config.get(printer, "policy").lower()
+        return self.config.get(printer, "policy").upper()
         
     def getSMTPServer(self) :    
