Show
Ignore:
Timestamp:
06/25/03 16:10:01 (21 years ago)
Author:
jalet
Message:

Hey, it may work (edpykota --reset excepted) !

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r1021 r1041  
    2121# 
    2222# $Log$ 
     23# Revision 1.41  2003/06/25 14:10:01  jalet 
     24# Hey, it may work (edpykota --reset excepted) ! 
     25# 
    2326# Revision 1.40  2003/06/10 16:37:54  jalet 
    2427# Deletion of the second user which is not needed anymore. 
     
    305308            touser = "%s@%s" % (touser, self.smtpserver) 
    306309        server = smtplib.SMTP(self.smtpserver) 
    307         server.sendmail(adminmail, [touser], fullmessage) 
     310        try : 
     311            server.sendmail(adminmail, [touser], fullmessage) 
     312        except smtplib.SMTPRecipientsRefused, answer :     
     313            for (k, v) in answer.recipients.items() : 
     314                self.logger.log_message(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") 
    308315        server.quit() 
    309316         
     
    317324        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) 
    318325         
    319     def checkGroupPQuota(self, groupname, printername) :     
     326    def checkGroupPQuota(self, grouppquota) :     
    320327        """Checks the group quota on a printer and deny or accept the job.""" 
    321         printerid = self.storage.getPrinterId(printername) 
    322         policy = self.config.getPrinterPolicy(printername) 
    323         groupid = self.storage.getGroupId(groupname) 
    324         limitby = self.storage.getGroupLimitBy(groupid) 
    325         if limitby == "balance" :  
    326             balance = self.storage.getGroupBalance(groupid) 
    327             if balance is None : 
     328        group = grouppquota.Group 
     329        printer = grouppquota.Printer 
     330        if group.LimitBy.lower() == "balance" :  
     331            # TODO : there's no warning (no account balance soft limit) 
     332            if float(group.AccountBalance) <= 0.0 : 
     333                action = "DENY" 
     334            else :     
     335                action = "ALLOW" 
     336        else : 
     337            if grouppquota.SoftLimit is not None : 
     338                softlimit = int(grouppquota.SoftLimit) 
     339                if grouppquota.PageCounter < softlimit : 
     340                    action = "ALLOW" 
     341                else :     
     342                    if grouppquota.HardLimit is None : 
     343                        # only a soft limit, this is equivalent to having only a hard limit 
     344                        action = "DENY" 
     345                    else :     
     346                        hardlimit = int(grouppquota.HardLimit) 
     347                        if softlimit <= grouppquota.PageCounter < hardlimit :     
     348                            now = DateTime.now() 
     349                            if grouppquota.DateLimit is not None : 
     350                                datelimit = DateTime.ISO.ParseDateTime(grouppquota.DateLimit) 
     351                            else : 
     352                                datelimit = now + self.config.getGraceDelay(printer.Name) 
     353                                grouppquota.setDateLimit(datelimit) 
     354                            if now < datelimit : 
     355                                action = "WARN" 
     356                            else :     
     357                                action = "DENY" 
     358                        else :          
     359                            action = "DENY" 
     360            else :         
     361                if grouppquota.HardLimit is not None : 
     362                    # no soft limit, only a hard one. 
     363                    hardlimit = int(grouppquota.HardLimit) 
     364                    if grouppquota.PageCounter < hardlimit : 
     365                        action = "ALLOW" 
     366                    else :       
     367                        action = "DENY" 
     368                else : 
     369                    # Both are unset, no quota, i.e. accounting only 
     370                    action = "ALLOW" 
     371        return action 
     372     
     373    def checkUserPQuota(self, userpquota) : 
     374        """Checks the user quota on a printer and deny or accept the job.""" 
     375        user = userpquota.User 
     376        printer = userpquota.Printer 
     377         
     378        # first we check any group the user is a member of 
     379        for group in self.storage.getUserGroups(user) : 
     380            grouppquota = self.storage.getGroupPQuota(group, printer) 
     381            if grouppquota.Exists : 
     382                action = self.checkGroupPQuota(grouppquota) 
     383                if action == "DENY" : 
     384                    return action 
     385                 
     386        # then we check the user's own quota 
     387        policy = self.config.getPrinterPolicy(printer.Name) 
     388        if user.LimitBy.lower() == "balance" :  
     389            if user.AccountBalance is None : 
    328390                if policy == "ALLOW" : 
    329391                    action = "POLICY_ALLOW" 
    330392                else :     
    331393                    action = "POLICY_DENY" 
    332                 self.logger.log_message(_("Unable to find group %s's account balance, applying default policy (%s) for printer %s") % (groupname, action, printername)) 
     394                self.logger.log_message(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name)) 
    333395            else :     
    334396                # TODO : there's no warning (no account balance soft limit) 
    335                 (balance, lifetimepaid) = balance 
    336                 if balance <= 0.0 : 
     397                if float(user.AccountBalance or 0.0) <= 0.0 : 
    337398                    action = "DENY" 
    338399                else :     
    339400                    action = "ALLOW" 
    340401        else : 
    341             quota = self.storage.getGroupPQuota(groupid, printerid) 
    342             if quota is None : 
    343                 # Unknown group or printer or combination 
     402            if not userpquota.Exists : 
     403                # Unknown userquota  
    344404                if policy == "ALLOW" : 
    345405                    action = "POLICY_ALLOW" 
    346406                else :     
    347407                    action = "POLICY_DENY" 
    348                 self.logger.log_message(_("Unable to match group %s on printer %s, applying default policy (%s)") % (groupname, printername, action)) 
     408                self.logger.log_message(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) 
    349409            else :     
    350                 pagecounter = quota["pagecounter"] 
    351                 softlimit = quota["softlimit"] 
    352                 hardlimit = quota["hardlimit"] 
    353                 datelimit = quota["datelimit"] 
    354                 if softlimit is not None : 
     410                pagecounter = int(userpquota.PageCounter or 0) 
     411                if userpquota.SoftLimit is not None : 
     412                    softlimit = int(userpquota.SoftLimit) 
    355413                    if pagecounter < softlimit : 
    356414                        action = "ALLOW" 
    357415                    else :     
    358                         if hardlimit is None : 
     416                        if userpquota.HardLimit is None : 
    359417                            # only a soft limit, this is equivalent to having only a hard limit 
    360418                            action = "DENY" 
    361419                        else :     
     420                            hardlimit = int(userpquota.HardLimit) 
    362421                            if softlimit <= pagecounter < hardlimit :     
    363422                                now = DateTime.now() 
    364                                 if datelimit is not None : 
    365                                     datelimit = DateTime.ISO.ParseDateTime(datelimit) 
     423                                if userpquota.DateLimit is not None : 
     424                                    datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit) 
    366425                                else : 
    367                                     datelimit = now + self.config.getGraceDelay(printername) 
    368                                     self.storage.setGroupDateLimit(groupid, printerid, datelimit) 
     426                                    datelimit = now + self.config.getGraceDelay(printer.Name) 
     427                                    userpquota.setDateLimit(datelimit) 
    369428                                if now < datelimit : 
    370429                                    action = "WARN" 
     
    374433                                action = "DENY" 
    375434                else :         
    376                     if hardlimit is not None : 
     435                    if userpquota.HardLimit is not None : 
    377436                        # no soft limit, only a hard one. 
     437                        hardlimit = int(userpquota.HardLimit) 
    378438                        if pagecounter < hardlimit : 
    379439                            action = "ALLOW" 
     
    385445        return action 
    386446     
    387     def checkUserPQuota(self, username, printername) : 
    388         """Checks the user quota on a printer and deny or accept the job.""" 
    389         # first we check any group the user is a member of 
    390         userid = self.storage.getUserId(username) 
    391         for groupname in self.storage.getUserGroupsNames(userid) : 
    392             action = self.checkGroupPQuota(groupname, printername) 
    393             if action in ("DENY", "POLICY_DENY") : 
    394                 return action 
    395                  
    396         # then we check the user's own quota 
    397         printerid = self.storage.getPrinterId(printername) 
    398         policy = self.config.getPrinterPolicy(printername) 
    399         limitby = self.storage.getUserLimitBy(userid) 
    400         if limitby == "balance" :  
    401             balance = self.storage.getUserBalance(userid) 
    402             if balance is None : 
    403                 if policy == "ALLOW" : 
    404                     action = "POLICY_ALLOW" 
    405                 else :     
    406                     action = "POLICY_DENY" 
    407                 self.logger.log_message(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (username, action, printername)) 
    408             else :     
    409                 # TODO : there's no warning (no account balance soft limit) 
    410                 (balance, lifetimepaid) = balance 
    411                 if balance <= 0.0 : 
    412                     action = "DENY" 
    413                 else :     
    414                     action = "ALLOW" 
    415         else : 
    416             quota = self.storage.getUserPQuota(userid, printerid) 
    417             if quota is None : 
    418                 # Unknown user or printer or combination 
    419                 if policy == "ALLOW" : 
    420                     action = "POLICY_ALLOW" 
    421                 else :     
    422                     action = "POLICY_DENY" 
    423                 self.logger.log_message(_("Unable to match user %s on printer %s, applying default policy (%s)") % (username, printername, action)) 
    424             else :     
    425                 pagecounter = quota["pagecounter"] 
    426                 softlimit = quota["softlimit"] 
    427                 hardlimit = quota["hardlimit"] 
    428                 datelimit = quota["datelimit"] 
    429                 if softlimit is not None : 
    430                     if pagecounter < softlimit : 
    431                         action = "ALLOW" 
    432                     else :     
    433                         if hardlimit is None : 
    434                             # only a soft limit, this is equivalent to having only a hard limit 
    435                             action = "DENY" 
    436                         else :     
    437                             if softlimit <= pagecounter < hardlimit :     
    438                                 now = DateTime.now() 
    439                                 if datelimit is not None : 
    440                                     datelimit = DateTime.ISO.ParseDateTime(datelimit) 
    441                                 else : 
    442                                     datelimit = now + self.config.getGraceDelay(printername) 
    443                                     self.storage.setUserDateLimit(userid, printerid, datelimit) 
    444                                 if now < datelimit : 
    445                                     action = "WARN" 
    446                                 else :     
    447                                     action = "DENY" 
    448                             else :          
    449                                 action = "DENY" 
    450                 else :         
    451                     if hardlimit is not None : 
    452                         # no soft limit, only a hard one. 
    453                         if pagecounter < hardlimit : 
    454                             action = "ALLOW" 
    455                         else :       
    456                             action = "DENY" 
    457                     else : 
    458                         # Both are unset, no quota, i.e. accounting only 
    459                         action = "ALLOW" 
    460         return action 
    461      
    462     def warnGroupPQuota(self, groupname, printername) : 
     447    def warnGroupPQuota(self, grouppquota) : 
    463448        """Checks a group quota and send messages if quota is exceeded on current printer.""" 
    464         admin = self.config.getAdmin(printername) 
    465         adminmail = self.config.getAdminMail(printername) 
    466         mailto = self.config.getMailTo(printername) 
    467         action = self.checkGroupPQuota(groupname, printername) 
    468         groupmembers = self.storage.getGroupMembersNames(groupname) 
     449        group = grouppquota.Group 
     450        printer = grouppquota.Printer 
     451        admin = self.config.getAdmin(printer.Name) 
     452        adminmail = self.config.getAdminMail(printer.Name) 
     453        mailto = self.config.getMailTo(printer.Name) 
     454        action = self.checkGroupPQuota(grouppquota, printer) 
    469455        if action.startswith("POLICY_") : 
    470456            action = action[7:] 
    471457        if action == "DENY" : 
    472             adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, printername) 
     458            adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name) 
    473459            self.logger.log_message(adminmessage) 
    474460            if mailto in [ "BOTH", "ADMIN" ] : 
    475461                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    476             for username in groupmembers : 
     462            for user in self.storage.getGroupMembers(group) : 
    477463                if mailto in [ "BOTH", "USER" ] : 
    478                     self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour group Print Quota is exceeded on printer %s.") % printername) 
     464                    self.sendMessageToUser(admin, adminmail, user.Name, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour group Print Quota is exceeded on printer %s.") % printer.Name) 
    479465        elif action == "WARN" :     
    480             adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, printername) 
     466            adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (group.Name, printer.Name) 
    481467            self.logger.log_message(adminmessage) 
    482468            if mailto in [ "BOTH", "ADMIN" ] : 
    483469                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    484             for username in groupmembers : 
     470            for user in self.storage.getGroupMembers(group) : 
    485471                if mailto in [ "BOTH", "USER" ] : 
    486                     self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour group Print Quota is almost reached on printer %s.") % printername) 
     472                    self.sendMessageToUser(admin, adminmail, user.Name, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour group Print Quota is almost reached on printer %s.") % printer.Name) 
    487473        return action         
    488474         
    489     def warnUserPQuota(self, username, printername) : 
     475    def warnUserPQuota(self, userpquota) : 
    490476        """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
    491         admin = self.config.getAdmin(printername) 
    492         adminmail = self.config.getAdminMail(printername) 
    493         mailto = self.config.getMailTo(printername) 
    494         action = self.checkUserPQuota(username, printername) 
     477        user = userpquota.User 
     478        printer = userpquota.Printer 
     479        admin = self.config.getAdmin(printer.Name) 
     480        adminmail = self.config.getAdminMail(printer.Name) 
     481        mailto = self.config.getMailTo(printer.Name) 
     482        action = self.checkUserPQuota(userpquota) 
    495483        if action.startswith("POLICY_") : 
    496484            action = action[7:] 
    497485        if action == "DENY" : 
    498             adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, printername) 
     486            adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name) 
    499487            self.logger.log_message(adminmessage) 
    500488            if mailto in [ "BOTH", "USER" ] : 
    501                 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printername) 
     489                self.sendMessageToUser(admin, adminmail, user.Name, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printer.Name) 
    502490            if mailto in [ "BOTH", "ADMIN" ] : 
    503491                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    504492        elif action == "WARN" :     
    505             adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, printername) 
     493            adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (user.Name, printer.Name) 
    506494            self.logger.log_message(adminmessage) 
    507495            if mailto in [ "BOTH", "USER" ] : 
    508                 self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printername) 
     496                self.sendMessageToUser(admin, adminmail, user.Name, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printer.Name) 
    509497            if mailto in [ "BOTH", "ADMIN" ] : 
    510498                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)