Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integration Email2Trac + Trac + GLPI
#-*- encoding: utf-8 -*-
def ticket_update_by_subject(self, subject):
"""
This list of Re: prefixes is probably incomplete. Taken from
wikipedia. Here is how the subject is matched
- Re: <subject>
- Re: (<Mail list label>:)+ <subject>
So we must have the last column
"""
self.logger.debug('function ticket_update_by_subject')
found_id = None
if self.parameters.ticket_update and self.parameters.ticket_update_by_subject:
SUBJECT_RE = re.compile(r'^(?:(?:RE|AW|VS|SV|FW|FWD):\s*)+(.*)', re.IGNORECASE)
result = SUBJECT_RE.search(subject)
# May be a response of ServiceDesk (GLPI).
if 'Service Desk #' in subject.encode('utf8'):
result = subject.encode('utf8')
if result:
## This is a reply
#
orig_subject = result if 'Service Desk #' in result else result.group(1)
self.logger.debug('subject search string: %s' %(orig_subject))
cursor = self.db.cursor()
summaries = [orig_subject, '%%: %s' % orig_subject]
## Time resolution is in micoseconds
#
search_date = datetime.now(util.datefmt.utc) - timedelta(days=self.parameters.ticket_update_by_subject_lookback)
if self.VERSION < 0.12:
lookback = util.datefmt.to_timestamp(search_date)
else:
lookback = util.datefmt.to_utimestamp(search_date)
for summary in summaries:
self.logger.debug('Looking for summary matching: "%s"' % summary)
sql = """SELECT id, reporter FROM ticket
WHERE changetime >= %s AND summary LIKE %s
ORDER BY changetime DESC"""
cursor.execute(sql, [lookback, summary.strip()])
for row in cursor:
(matched_id, sql_reporter) = row
## Save first entry.
#
if not found_id:
found_id = matched_id
## If subject and reporter are the same. The we certainly have found the right ticket
#
if sql_reporter == self.author:
self.logger.debug('Found matching reporter: %s with ticket id: %d' %(sql_reporter, matched_id))
found_id = matched_id
break
if found_id:
self.logger.debug('Found matching ticket id: %d' % found_id)
found_id = '#%d' % found_id
return (found_id, orig_subject)
# obsolete !?? 12 Aug 2011
#subject = orig_subject
return (found_id, subject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.