Changeset 250 for trunk


Ignore:
Timestamp:
01/27/09 15:35:40 (15 years ago)
Author:
bas
Message:

email2trac.py.in:

  • removed obsolete set_owner function
  • get_sender_info is now case insensitive, closes #118 will also set the proper author of a ticket if email is known
  • ticket updates with attachments did not work anymore closes #120
  • We have one update less if a ticket is inserted, the ticket_id line has been removed
  • Added some proper DRY_RUN functions
  • removed obsolete code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/email2trac.py.in

    r249 r250  
    327327                except KeyError, detail:
    328328                        if self.DEBUG > 2 :
    329                                 print '%s not defined, all messages are allowed.' %(keyword)
     329                                print 'TD: %s not defined, all messages are allowed.' %(keyword)
    330330
    331331                        return default
     
    434434
    435435
    436         def set_owner(self, ticket):
    437                 """
    438                 Select default owner for ticket component
    439                 """
    440                 #### return self.get_config('ticket', 'default_component')
    441                 cursor = self.db.cursor()
    442                 sql = "SELECT owner FROM component WHERE name='%s'" % ticket['component']
    443                 cursor.execute(sql)
    444                 try:
    445                         ticket['owner'] = cursor.fetchone()[0]
    446                 except TypeError, detail:
    447                         ticket['owner'] = None
    448 
    449436        def get_sender_info(self, message):
    450437                """
     
    471458                #   e : email address that the user has set in the settings tab
    472459                #
    473             tmp_email_addrs = self.email_addr.lower()
    474460                users = [ (u,n,e) for (u, n, e) in self.env.get_known_users(self.db)
    475                                 if e == tmp_email_addr ]
     461                        if e and (e.lower() == self.email_addr.lower()) ]
    476462
    477463                if len(users) == 1:
    478464                        self.email_from = users[0][0]
     465                        self.author = users[0][0]
    479466
    480467        def set_reply_fields(self, ticket, message):
     
    614601                creating a new one.
    615602                """
     603                if self.DEBUG:
     604                        print "TD: ticket_update"
    616605
    617606                if not m['Subject']:
     
    677666
    678667                message_parts = self.get_message_parts(m)
    679                 message_parts = self.unique_attachment_names(message_parts, tkt)
     668                message_parts = self.unique_attachment_names(message_parts, ticket_id)
    680669
    681670                if self.EMAIL_HEADER:
     
    685674
    686675                if body_text.strip() or update_fields:
    687                         tkt.save_changes(self.author, body_text, when)
    688 
    689                 tkt['id'] = ticket_id
     676                        if self.DRY_RUN:
     677                                print 'DRY_RUN: tkt.save_changes(self.author, comment) ', self.author
     678                        else:
     679                                tkt.save_changes(self.author, body_text, when)
     680
    690681
    691682                if self.VERSION  == 0.9:
    692                         str = self.attachments(message_parts, tkt, True)
    693                 else:
    694                         str = self.attachments(message_parts, tkt)
     683                        str = self.attachments(message_parts, ticket_id, True)
     684                else:
     685                        str = self.attachments(message_parts, ticket_id)
    695686
    696687                if self.notification and not spam:
    697                         self.notify(tkt, False, when)
     688                        self.notify(tkt, ticket_id, False, when)
    698689
    699690                return True
     
    755746                Create a new ticket
    756747                """
     748                if self.DEBUG:
     749                        print "TD: new_ticket"
     750
    757751                tkt = Ticket(self.env)
    758 
    759752                self.set_ticket_fields(tkt)
    760 
    761                 # Some defaults
    762                 #
    763                 #tkt['status'] = 'new'
    764                 #tkt['milestone'] = self.get_config('ticket', 'default_milestone')
    765                 #tkt['priority'] = self.get_config('ticket', 'default_priority')
    766                 #tkt['severity'] = self.get_config('ticket', 'default_severity')
    767                 #tkt['version'] = self.get_config('ticket', 'default_version')
    768                 #tkt['type'] = self.get_config('ticket', 'default_type')
    769753
    770754                # Old style setting for component, will be removed
     
    780764                else:
    781765                        tkt['summary'] = self.email_to_unicode(msg['Subject'])
    782 
    783766
    784767                self.set_reply_fields(tkt, msg)
     
    809792                else:
    810793                        ticket_id = tkt.insert()
    811                        
    812                 tkt['id'] = ticket_id
    813 
     794               
    814795                changed = False
    815796                comment = ''
     
    825806                                %(head, mailto, body_text)
    826807
    827                 str =  self.attachments(message_parts, tkt)
     808                str =  self.attachments(message_parts, ticket_id)
    828809                if str:
    829810                        changed = True
     
    832813                if changed:
    833814                        if self.DRY_RUN:
    834                                 print 'DRY_RUN: tkt.save_changes(self.author, comment)'
     815                                print 'DRY_RUN: tkt.save_changes(self.author, comment) ', self.author
    835816                        else:
    836817                                tkt.save_changes(self.author, comment)
    837818                                #print tkt.get_changelog(self.db, when)
    838819
    839                 if self.notification:
    840                         if self.DRY_RUN:
    841                                 print 'DRY_RUN: self.notify(tkt, True)'
    842                         else:
    843                                 if not spam:
    844                                         self.notify(tkt, True)
    845                                 #self.notify(tkt, False)
     820                if self.notification and not spam:
     821                        self.notify(tkt, ticket_id, True)
     822                        #self.notify(ticket_id, False)
    846823
    847824        def parse(self, fp):
     
    852829                if not m:
    853830                        if self.DEBUG:
    854                                 print "This is not a valid email message format"
     831                                print "TD: This is not a valid email message format"
    855832                        return
    856833                       
     
    11031080                return message_parts
    11041081               
    1105         def unique_attachment_names(self, message_parts, tkt = None):
     1082        def unique_attachment_names(self, message_parts, tkt_id = None):
    11061083                renamed_parts = []
    11071084                attachment_names = set()
     
    11531130                        filename, ext = os.path.splitext(filename)
    11541131
    1155                         while unique_filename in attachment_names or self.attachment_exists(tkt, unique_filename):
     1132                        while unique_filename in attachment_names or self.attachment_exists(tkt_id, unique_filename):
    11561133                                num += 1
    11571134                                unique_filename = "%s-%s%s" % (filename, num, ext)
     
    11701147               
    11711148                       
    1172         def attachment_exists(self, tkt, filename):
    1173                 if tkt is None:
     1149        def attachment_exists(self, tkt_id, filename):
     1150
     1151                if self.DEBUG:
     1152                        print "TD: attachment_exists: Ticket number : %s, Filename : %s" %(tkt_id, filename)
     1153
     1154                # We have no valid ticket id
     1155                #
     1156                if not tkt_id:
    11741157                        return False
    1175                        
     1158
    11761159                try:
    1177                         Attachment(self.env, 'ticket', tkt['id'], filename)
     1160                        att = attachment.Attachment(self.env, 'ticket', tkt_id, filename)
    11781161                        return True
    1179                 except ResourceNotFound:
     1162                except attachment.ResourceNotFound:
    11801163                        return False
    11811164                       
     
    12031186                return body_text
    12041187
    1205         def notify(self, tkt , new=True, modtime=0):
     1188        def notify(self, tkt, id, new=True, modtime=0):
    12061189                """
    12071190                A wrapper for the TRAC notify function. So we can use templates
    12081191                """
     1192                if self.DRY_RUN:
     1193                                print 'DRY_RUN: self.notify(tkt, True) ', self.author
     1194                                return
    12091195                try:
    12101196                        # create false {abs_}href properties, to trick Notify()
     
    12361222
    12371223                except Exception, e:
    1238                         print 'TD: Failure sending notification on creation of ticket #%s: %s' %(tkt['id'], e)
     1224                        print 'TD: Failure sending notification on creation of ticket #%s: %s' %(id, e)
    12391225
    12401226        def html_mailto_link(self, subject, id, body):
     
    12621248                return str
    12631249
    1264         def attachments(self, message_parts, ticket, update=False):
     1250        def attachments(self, message_parts, tkt_id, update=False):
    12651251                '''
    12661252                save any attachments as files in the ticket's directory
    12671253                '''
    12681254                if self.DRY_RUN:
     1255                        print "DRY_RUN: no attachments saved"
    12691256                        return ''
    12701257
     
    13131300                        #
    13141301                        fd = open(path, 'rb')
    1315                         att = attachment.Attachment(self.env, 'ticket', ticket['id'])
     1302                        att = attachment.Attachment(self.env, 'ticket', tkt_id)
    13161303
    13171304                        # This will break the ticket_update system, the body_text is vaporized
Note: See TracChangeset for help on using the changeset viewer.