Edgewall Software

Ticket #3517: trac-0.10-cia.diff

File trac-0.10-cia.diff, 4.6 KB (added by wichert@…, 14 months ago)

CIA for trac 0.10.4

  • trac/notification.py

     
    2020from trac import __version__ 
    2121from trac.config import BoolOption, IntOption, Option 
    2222from trac.core import * 
     23from trac.util.cia import CIA 
    2324from trac.util.text import CRLF 
    2425from trac.web.chrome import Chrome 
    2526from trac.web.clearsilver import HDFWrapper 
     
    140141        """ 
    141142 
    142143 
    143 class NotifyEmail(Notify): 
     144class NotifyCIA(Notify): 
     145    """Baseclass for notification via CIA.""" 
     146 
     147    def notify(self, resid): 
     148        """Send message to recipients.""" 
     149 
     150        # stupid code logic... 
     151        if self.config.getbool('notification', 'smtp_enabled'): 
     152            Notify.notify(self, resid) 
     153 
     154        if not self.config.getbool('notification', 'cia_enabled'): 
     155            return 
     156 
     157        project = self.config['notification'].get('cia_project') 
     158        server = self.config['notification'].get('cia_server') 
     159 
     160        if not project or not server: 
     161            raise TracError(Markup('Unable to notify CIA server: ' 
     162                    'neither cia project nor server specified.'), 
     163                    'CIA Notification error') 
     164 
     165        if self.newticket: 
     166            updater = self.hdf["ticket.reporter"] 
     167        else: 
     168            updater = self.hdf["ticket.change.author"] 
     169 
     170        if self.newticket: 
     171            action = "opened" 
     172        else: 
     173            oldvalue = self.hdf.get("ticket.change.status.oldvalue") 
     174            newvalue = self.hdf.get("ticket.change.status.newvalue") 
     175            if oldvalue == "closed": 
     176                action = "reopened" 
     177            elif newvalue == "closed": 
     178                action = "closed (%s)" % self.ticket["resolution"] 
     179            else: 
     180                return 
     181 
     182        cia = CIA(project=project, server=server, module="ticket " + action) 
     183        cia(resid, updater, 
     184                "[%s] %s %s" % 
     185                   (self.hdf["ticket.component"], 
     186                    self.hdf["ticket.summary"], 
     187                    self.hdf['ticket.link'])) 
     188 
     189 
     190class NotifyEmail(NotifyCIA): 
    144191    """Baseclass for notification by email.""" 
    145192 
    146193    smtp_server = 'localhost' 
     
    195242    def notify(self, resid, subject): 
    196243        self.subject = subject 
    197244 
    198         if not self.config.getbool('notification', 'smtp_enabled'): 
    199             return 
    200         self.smtp_server = self.config['notification'].get('smtp_server') 
    201         self.smtp_port = self.config['notification'].getint('smtp_port') 
    202         self.from_email = self.config['notification'].get('smtp_from') 
    203         self.replyto_email = self.config['notification'].get('smtp_replyto') 
    204         self.from_email = self.from_email or self.replyto_email 
    205         if not self.from_email and not self.replyto_email: 
    206             raise TracError(Markup('Unable to send email due to identity ' 
    207                                    'crisis.<p>Neither <b>notification.from</b> ' 
    208                                    'nor <b>notification.reply_to</b> are ' 
    209                                    'specified in the configuration.</p>'), 
    210                             'SMTP Notification Error') 
     245        if self.config.getbool('notification', 'smtp_enabled'): 
     246            self.smtp_server = self.config['notification'].get('smtp_server') 
     247            self.smtp_port = self.config['notification'].getint('smtp_port') 
     248            self.from_email = self.config['notification'].get('smtp_from') 
     249            self.replyto_email = self.config['notification'].get('smtp_replyto') 
     250            self.from_email = self.from_email or self.replyto_email 
     251            if not self.from_email and not self.replyto_email: 
     252                raise TracError(Markup('Unable to send email due to identity ' 
     253                                       'crisis.<p>Neither <b>notification.from</b> ' 
     254                                       'nor <b>notification.reply_to</b> are ' 
     255                                       'specified in the configuration.</p>'), 
     256                                'SMTP Notification Error') 
    211257 
    212         # Authentication info (optional) 
    213         self.user_name = self.config['notification'].get('smtp_user') 
    214         self.password = self.config['notification'].get('smtp_password') 
     258            # Authentication info (optional) 
     259            self.user_name = self.config['notification'].get('smtp_user') 
     260            self.password = self.config['notification'].get('smtp_password') 
    215261 
    216         Notify.notify(self, resid) 
     262        NotifyCIA.notify(self, resid) 
    217263 
    218264    def format_header(self, key, name, email=None): 
    219265        from email.Header import Header