Changeset 789

Show
Ignore:
Timestamp:
05/26/06 07:57:10 (3 years ago)
Author:
bas
Message:

EmailtoTracScript:

email2trac.py.in:

  • Fixed some bugs in ticket merging and re-arrange some
    code.
  • Attachments are not listed when a new ticket is created
    with email. So added a line how many attachments there are.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • emailtotracscript/trunk/email2trac.py.in

    r787 r789  
    341341                body_text = '{{{\n%s\n}}}' %body_text 
    342342 
     343                # Get current time 
     344                # 
     345                when = int(time.time()) 
     346 
    343347                if self.VERSION  == 0.8: 
    344                         tkt.save_changes(self.db, self.author, body_text) 
    345                 else: 
    346                         # def save_changes(self, author, comment, when=0, self.db=None): 
    347                         tkt.save_changes(self.author, body_text) 
    348  
     348                        tkt.save_changes(self.db, self.author, body_text, when) 
     349                else: 
     350                        tkt.save_changes(self.author, body_text, when) 
    349351 
    350352                self.attachments(m, tkt) 
     353 
    351354                if self.notification: 
    352                         self.notify(tkt, False
     355                        self.notify(tkt, False, when
    353356 
    354357                return True 
    355358 
    356359 
    357         def parse(self, fp): 
    358                 msg = email.message_from_file(fp) 
    359                 if not msg: 
    360                         return 
    361  
    362                 if self.DEBUG > 1:        # save the entire e-mail message text 
    363                         self.save_email_for_debug(msg) 
    364                         self.debug_attachments(msg) 
    365  
    366                 self.db = self.env.get_db_cnx() 
    367                 self.get_author_emailaddrs(msg)  
    368  
    369                 if self.get_config('notification', 'smtp_enabled') in ['true']: 
    370                         self.notification = 1 
    371                 else: 
    372                         self.notification = 0 
    373  
    374                 # Must we update existing tickets 
    375                 # 
    376                 if self.TICKET_UPDATE > 0: 
    377                         if self.ticket_update(msg): 
    378                                 return True 
    379                  
     360        def new(self, msg): 
     361                """ 
     362                Create a new ticket 
     363                """ 
    380364                tkt = Ticket(self.env) 
    381365                tkt['status'] = 'new' 
     
    419403 
    420404                body_text = self.get_body_text(msg) 
    421                 tkt['description'] = '\n{{{\n%s\n}}}\n' %(body_text) 
    422  
    423                 # Insert ticket in database 
    424                 # 
     405                tkt['description'] = '' 
     406 
     407                # Insert ticket in database with empty description 
     408                # 
     409                when = int(time.time()) 
    425410                if self.VERSION == 0.8: 
    426411                        tkt['id'] = tkt.insert(self.db) 
     
    428413                        tkt['id'] = tkt.insert() 
    429414 
    430                 # Else we do no have a ticket id for the subject line 
     415                n =  self.attachments(msg, tkt) 
     416                if n: 
     417                        attach_str = '\nThis message has %d attachment(s)\n' %(n) 
     418                else: 
     419                        attach_str = '' 
     420 
     421                # Always update the description else we get two emails one for the new ticket 
     422                # and for the attachments. It is an ugly hack but with trac you can not add 
     423                # attachments without an ticket id 
    431424                # 
    432425                mailto = '' 
    433426                if self.MAILTO: 
    434427                        mailto = self.html_mailto_link(self.to_unicode(msg['subject']), tkt['id'], body_text) 
    435                         tkt['description'] = '%s%s\n{{{\n%s\n}}}\n' %(head, mailto, body_text) 
    436                         tkt.save_changes(self.db, self.author, "") 
    437  
    438                 self.attachments(msg, tkt) 
     428                        tkt['description'] = '%s%s%s\n{{{\n%s\n}}}\n' %(head, attach_str, mailto, body_text) 
     429                        comment = 'Added mailto: link + description' 
     430                else: 
     431                        tkt['description'] = '%s%s\n{{{\n%s\n}}}\n' %(head, attach_str, body_text) 
     432                        comment = 'Added description' 
     433 
     434                # Save the real description and other changes 
     435                # 
     436                if self.VERSION  == 0.8: 
     437                        tkt.save_changes(self.db, self.author, comment, when) 
     438                else: 
     439                        tkt.save_changes(self.author, comment, when) 
     440 
    439441                if self.notification: 
    440                         self.notify(tkt) 
     442                        self.notify(tkt, True, when) 
     443 
     444        def parse(self, fp): 
     445                m = email.message_from_file(fp) 
     446                if not m: 
     447                        return 
     448 
     449                if self.DEBUG > 1:        # save the entire e-mail message text 
     450                        self.save_email_for_debug(m) 
     451                        self.debug_attachments(m) 
     452 
     453                self.db = self.env.get_db_cnx() 
     454                self.get_author_emailaddrs(m)  
     455 
     456                if self.get_config('notification', 'smtp_enabled') in ['true']: 
     457                        self.notification = 1 
     458                else: 
     459                        self.notification = 0 
     460 
     461                # Must we update existing tickets 
     462                # 
     463                if self.TICKET_UPDATE > 0: 
     464                        if self.ticket_update(m): 
     465                                return True 
     466 
     467                self.new(m) 
    441468 
    442469        def get_body_text(self, msg): 
     
    481508                return ubody_text 
    482509 
    483         def notify(self, tkt , new=True): 
     510        def notify(self, tkt , new=True, modtime=0): 
    484511                try: 
    485512                        # create false {abs_}href properties, to trick Notify() 
     
    492519                                tn.template_name = self.notify_template; 
    493520 
    494                         tn.notify(tkt, new)  
     521                        tn.notify(tkt, new, modtime)  
    495522 
    496523                except Exception, e: 
     
    530557 
    531558                count = 0 
    532                 first = 0 
     559                first = 0  
     560                number = 0  
    533561                for part in message.walk(): 
    534562                        if part.get_content_maintype() == 'multipart':          # multipart/* is just a container 
     
    541569 
    542570                        filename = part.get_filename()  
     571                        count = count + 1 
    543572                        if not filename: 
    544                                 count = count + 1 
    545                                 filename = 'part%04d' % count 
     573                                number = number + 1 
     574                                filename = 'part%04d' % number 
    546575 
    547576                                ext = mimetypes.guess_extension(part.get_content_type()) 
     
    607636                                att.insert(url_filename, fd, filesize) 
    608637                                fd.close() 
     638 
     639                # Return how many attachments 
     640                # 
     641                return count 
    609642 
    610643