Show
Ignore:
Timestamp:
04/16/03 14:35:49 (21 years ago)
Author:
jalet
Message:

Groups quota work now !

Files:
1 modified

Legend:

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

    r925 r927  
    2121# 
    2222# $Log$ 
     23# Revision 1.33  2003/04/16 12:35:49  jalet 
     24# Groups quota work now ! 
     25# 
    2326# Revision 1.32  2003/04/16 08:53:14  jalet 
    2427# Printing can now be limited either by user's account balance or by 
     
    287290        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) 
    288291         
     292    def checkGroupPQuota(self, groupname, printername) :     
     293        """Checks the group quota on a printer and deny or accept the job.""" 
     294        printerid = self.storage.getPrinterId(printername) 
     295        groupid = self.storage.getGroupId(groupname) 
     296        limitby = self.storage.getGroupLimitBy(groupid) 
     297        if limitby == "balance" :  
     298            balance = self.storage.getGroupBalance(groupid) 
     299            if balance is None : 
     300                policy = self.config.getPrinterPolicy(printername) 
     301                if policy in [None, "ALLOW"] : 
     302                    action = "POLICY_ALLOW" 
     303                else :     
     304                    action = "POLICY_DENY" 
     305                self.logger.log_message(_("Unable to find group %s's account balance, applying default policy (%s) for printer %s") % (groupname, action, printername)) 
     306            else :     
     307                # TODO : there's no warning (no account balance soft limit) 
     308                if balance <= 0.0 : 
     309                    action = "DENY" 
     310                else :     
     311                    action = "ALLOW" 
     312        else : 
     313            quota = self.storage.getGroupPQuota(groupid, printerid) 
     314            if quota is None : 
     315                # Unknown group or printer or combination 
     316                policy = self.config.getPrinterPolicy(printername) 
     317                if policy in [None, "ALLOW"] : 
     318                    action = "POLICY_ALLOW" 
     319                else :     
     320                    action = "POLICY_DENY" 
     321                self.logger.log_message(_("Unable to match group %s on printer %s, applying default policy (%s)") % (groupname, printername, action)) 
     322            else :     
     323                pagecounter = quota["pagecounter"] 
     324                softlimit = quota["softlimit"] 
     325                hardlimit = quota["hardlimit"] 
     326                datelimit = quota["datelimit"] 
     327                if softlimit is not None : 
     328                    if pagecounter < softlimit : 
     329                        action = "ALLOW" 
     330                    else :     
     331                        if hardlimit is None : 
     332                            # only a soft limit, this is equivalent to having only a hard limit 
     333                            action = "DENY" 
     334                        else :     
     335                            if softlimit <= pagecounter < hardlimit :     
     336                                now = DateTime.now() 
     337                                if datelimit is not None : 
     338                                    datelimit = DateTime.ISO.ParseDateTime(datelimit) 
     339                                else : 
     340                                    datelimit = now + self.config.getGraceDelay(printername) 
     341                                    self.storage.setGroupDateLimit(groupid, printerid, datelimit) 
     342                                if now < datelimit : 
     343                                    action = "WARN" 
     344                                else :     
     345                                    action = "DENY" 
     346                            else :          
     347                                action = "DENY" 
     348                else :         
     349                    if hardlimit is not None : 
     350                        # no soft limit, only a hard one. 
     351                        if pagecounter < hardlimit : 
     352                            action = "ALLOW" 
     353                        else :       
     354                            action = "DENY" 
     355                    else : 
     356                        # Both are unset, no quota, i.e. accounting only 
     357                        action = "ALLOW" 
     358        return action 
     359     
    289360    def checkUserPQuota(self, username, printername) : 
    290361        """Checks the user quota on a printer and deny or accept the job.""" 
     362        # first we check any group the user is a member of 
     363        userid = self.storage.getUserId(username) 
     364        for groupname in self.storage.getUserGroupsNames(userid) : 
     365            action = self.checkGroupPQuota(groupname, printername) 
     366            if action in ("DENY", "POLICY_DENY") : 
     367                return action 
     368                 
     369        # then we check the user's own quota 
    291370        printerid = self.storage.getPrinterId(printername) 
    292         userid = self.storage.getUserId(username) 
    293371        limitby = self.storage.getUserLimitBy(userid) 
    294372        if limitby == "balance" :  
     
    355433        return action 
    356434     
    357     def warnGroupPQuota(self, username, printername=None) : 
    358         """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
     435    def warnGroupPQuota(self, groupname, printername=None) : 
     436        """Checks a group quota and send messages if quota is exceeded on current printer.""" 
    359437        pname = printername or self.printername 
    360         raise PyKotaToolError, _("Group quotas are currently not implemented.") 
     438        admin = self.config.getAdmin(pname) 
     439        adminmail = self.config.getAdminMail(pname) 
     440        mailto = self.config.getMailTo(pname) 
     441        action = self.checkGroupPQuota(groupname, pname) 
     442        groupmembers = self.storage.getGroupMembersNames(groupname) 
     443        if action.startswith("POLICY_") : 
     444            action = action[7:] 
     445        if action == "DENY" : 
     446            adminmessage = _("Print Quota exceeded for group %s on printer %s") % (groupname, pname) 
     447            self.logger.log_message(adminmessage) 
     448            if mailto in [ "BOTH", "ADMIN" ] : 
     449                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
     450            for username in groupmembers : 
     451                if mailto in [ "BOTH", "USER" ] : 
     452                    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.") % pname) 
     453        elif action == "WARN" :     
     454            adminmessage = _("Print Quota soft limit exceeded for group %s on printer %s") % (groupname, pname) 
     455            self.logger.log_message(adminmessage) 
     456            if mailto in [ "BOTH", "ADMIN" ] : 
     457                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
     458            for username in groupmembers : 
     459                if mailto in [ "BOTH", "USER" ] : 
     460                    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.") % pname) 
     461        return action         
    361462         
    362463    def warnUserPQuota(self, username, printername=None) :