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)
