Show
Ignore:
Timestamp:
02/02/08 20:47:11 (16 years ago)
Author:
jerome
Message:

warnpykota converted to new style.

Files:
1 modified

Legend:

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

    r3324 r3327  
    345345                return 0 
    346346        return 1         
    347          
    348     def sendMessage(self, adminmail, touser, fullmessage) : 
    349         """Sends an email message containing headers to some user.""" 
    350         try :     
    351             server = smtplib.SMTP(self.smtpserver) 
    352         except socket.error, msg :     
    353             self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") 
    354         else : 
    355             try : 
    356                 server.sendmail(adminmail, [touser], fullmessage) 
    357             except smtplib.SMTPException, answer :     
    358                 for (k, v) in answer.recipients.items() : 
    359                     self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") 
    360             server.quit() 
    361          
    362     def sendMessageToUser(self, admin, adminmail, user, subject, message) : 
    363         """Sends an email message to a user.""" 
    364         message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail) 
    365         usermail = user.Email or user.Name 
    366         if "@" not in usermail : 
    367             usermail = "%s@%s" % (usermail, self.maildomain or self.smtpserver or "localhost") 
    368         msg = MIMEText(message, _charset=self.charset) 
    369         msg["Subject"] = Header(subject, charset=self.charset) 
    370         msg["From"] = adminmail 
    371         msg["To"] = usermail 
    372         msg["Date"] = email.Utils.formatdate(localtime=True) 
    373         self.sendMessage(adminmail, usermail, msg.as_string()) 
    374          
    375     def sendMessageToAdmin(self, adminmail, subject, message) : 
    376         """Sends an email message to the Print Quota administrator.""" 
    377         if "@" not in adminmail : 
    378             adminmail = "%s@%s" % (adminmail, self.maildomain or self.smtpserver or "localhost") 
    379         msg = MIMEText(message, _charset=self.charset) 
    380         msg["Subject"] = Header(subject, charset=self.charset) 
    381         msg["From"] = adminmail 
    382         msg["To"] = adminmail 
    383         self.sendMessage(adminmail, adminmail, msg.as_string()) 
    384347         
    385348    def _checkUserPQuota(self, userpquota) :             
     
    584547        return cmd % locals() 
    585548         
    586     def warnGroupPQuota(self, grouppquota) : 
    587         """Checks a group quota and send messages if quota is exceeded on current printer.""" 
    588         group = grouppquota.Group 
    589         printer = grouppquota.Printer 
    590         admin = self.config.getAdmin(printer.Name) 
    591         adminmail = self.config.getAdminMail(printer.Name) 
    592         (mailto, arguments) = self.config.getMailTo(printer.Name) 
    593         if group.LimitBy in ("noquota", "nochange") : 
    594             action = "ALLOW" 
    595         else :     
    596             action = self.checkGroupPQuota(grouppquota) 
    597             if action.startswith("POLICY_") : 
    598                 action = action[7:] 
    599             if action == "DENY" : 
    600                 adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name) 
    601                 self.printInfo(adminmessage) 
    602                 if mailto in [ "BOTH", "ADMIN" ] : 
    603                     self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    604                 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 
    605                     for user in self.storage.getGroupMembers(group) : 
    606                         if mailto != "EXTERNAL" : 
    607                             self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name)) 
    608                         else :     
    609                             self.externalMailTo(arguments, action, user, printer, self.config.getHardWarn(printer.Name)) 
    610             elif action == "WARN" :     
    611                 adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name) 
    612                 self.printInfo(adminmessage) 
    613                 if mailto in [ "BOTH", "ADMIN" ] : 
    614                     self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    615                 if group.LimitBy and (group.LimitBy.lower() == "balance") :  
    616                     message = self.config.getPoorWarn() 
    617                 else :      
    618                     message = self.config.getSoftWarn(printer.Name) 
    619                 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 
    620                     for user in self.storage.getGroupMembers(group) : 
    621                         if mailto != "EXTERNAL" : 
    622                             self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 
    623                         else :     
    624                             self.externalMailTo(arguments, action, user, printer, message) 
    625         return action         
    626          
    627     def warnUserPQuota(self, userpquota) : 
    628         """Checks a user quota and send him a message if quota is exceeded on current printer.""" 
    629         user = userpquota.User 
    630         printer = userpquota.Printer 
    631         admin = self.config.getAdmin(printer.Name) 
    632         adminmail = self.config.getAdminMail(printer.Name) 
    633         (mailto, arguments) = self.config.getMailTo(printer.Name) 
    634          
    635         if user.LimitBy in ("noquota", "nochange") : 
    636             action = "ALLOW" 
    637         elif user.LimitBy == "noprint" : 
    638             action = "DENY" 
    639             message = _("User %s is not allowed to print at this time.") % user.Name 
    640             self.printInfo(message) 
    641             if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 
    642                 if mailto != "EXTERNAL" : 
    643                     self.sendMessageToUser(admin, adminmail, user, _("Printing denied."), message) 
    644                 else :     
    645                     self.externalMailTo(arguments, action, user, printer, message) 
    646             if mailto in [ "BOTH", "ADMIN" ] : 
    647                 self.sendMessageToAdmin(adminmail, _("Print Quota"), message) 
    648         else : 
    649             action = self.checkUserPQuota(userpquota) 
    650             if action.startswith("POLICY_") : 
    651                 action = action[7:] 
    652                  
    653             if action == "DENY" : 
    654                 adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name) 
    655                 self.printInfo(adminmessage) 
    656                 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 
    657                     message = self.config.getHardWarn(printer.Name) 
    658                     if mailto != "EXTERNAL" : 
    659                         self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 
    660                     else :     
    661                         self.externalMailTo(arguments, action, user, printer, message) 
    662                 if mailto in [ "BOTH", "ADMIN" ] : 
    663                     self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    664             elif action == "WARN" :     
    665                 adminmessage = _("Print Quota low for user %s on printer %s") % (user.Name, printer.Name) 
    666                 self.printInfo(adminmessage) 
    667                 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 
    668                     if user.LimitBy and (user.LimitBy.lower() == "balance") :  
    669                         message = self.config.getPoorWarn() 
    670                     else :      
    671                         message = self.config.getSoftWarn(printer.Name) 
    672                     if mailto != "EXTERNAL" :     
    673                         self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) 
    674                     else :     
    675                         self.externalMailTo(arguments, action, user, printer, message) 
    676                 if mailto in [ "BOTH", "ADMIN" ] : 
    677                     self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 
    678         return action         
    679