Skip to content

Commit

Permalink
BibCatalog RT4 attachment handling
Browse files Browse the repository at this point in the history
  * The output from the REST/1.0 interface has changed between rt3 and rt4
    Previously unnamed attachments would simply have an empty Name string.
    Now unnamed attachments result in the string '(Unnamed)'.
    Adjusting the regexp to handle both cases.

  * various code improvements along the way
  • Loading branch information
tsgit committed Jan 9, 2015
1 parent 47f4d80 commit 83c131a
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions modules/bibcatalog/lib/bibcatalog_system_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def ticket_search(self, uid, recordid=-1, subject="", text="", creator="",
matching the subject, creator or owner of the ticket."""

search_atoms = [] # the search expression will be made by and'ing these
if (recordid > -1):
if recordid > -1:
#search by recid
search_atoms.append("CF.{RecordID} = " + escape_shell_arg(str(recordid)))
if subject:
Expand Down Expand Up @@ -337,7 +337,7 @@ def ticket_get_attribute(self, uid, ticketid, attribute):
return ticinfo[attribute]
return None

def ticket_get_info(self, uid, ticketid, attributes = None):
def ticket_get_info(self, uid, ticketid, attributes=None):
"""return ticket info as a dictionary of pre-defined attribute names.
Or just those listed in attrlist.
Returns None on failure"""
Expand All @@ -355,9 +355,9 @@ def ticket_get_info(self, uid, ticketid, attributes = None):
return 0

tdict = {}
for line in command_out.split("\n"):
if line.count(": ") > 0:
tattr, tvaluen = line.split(": ")[0], ": ".join(line.split(": ")[1:])
for line in command_out.splitlines():
if line.find(": ") > -1:
tattr, tvaluen = line.split(": ", 1)
tvalue = tvaluen.rstrip()
tdict[tattr] = tvalue

Expand All @@ -368,14 +368,29 @@ def ticket_get_info(self, uid, ticketid, attributes = None):
return 0

attachments = []
regex = re.compile(r"[0-9]*:\s{2}[(]") # attachment's format: 557408: (text/plain / 131b)
for line in command_out.split("\n"):
for match in regex.findall(line):
attachments.append(match.split(":")[0])

# attachment's format:
# 557408: (?:Name|\(Unnamed\))? (text/plain / 131b)
# (Unnamed) is new in rt4, in rt3 no Name resulted in empty string,
# hence 2 spaces
#
# example:
# id: ticket/443299/attachments
#
# Attachments: 1410963: (Unnamed) (multipart/alternative / 0b),
# 1410964: (Unnamed) (text/plain / 257b),
# 1410965: (Unnamed) (text/html / 2.9k),
# 1410966: (Unnamed) (text/plain / 534b)

attregex = re.compile(r"(?P<attachmentid>\d+):\s{1,2}[(\w]")
for line in command_out.splitlines():
amatch = attregex.search(line)
if amatch:
attachments.append(amatch.group('attachmentid'))

# Query again for each attachment
for att in attachments:
command = CFG_BIBCATALOG_SYSTEM_RT_CLI + " show ticket/" + str(ticketid) + "/attachments/" + att
for attid in attachments:
command = "%s show ticket/%s/attachments/%s" % (CFG_BIBCATALOG_SYSTEM_RT_CLI, ticketid, attid)
command_out = self._run_rt_command(command, uid)
if command_out is None:
return 0
Expand All @@ -385,7 +400,7 @@ def ticket_get_info(self, uid, ticketid, attributes = None):
cstuff = line.split("Content: ")
tdict['Text'] = cstuff[1].rstrip()

if (len(tdict) > 0):
if len(tdict) > 0:
# Iterate over TICKET_ATTRIBUTES to make a canonical ticket
candict = {}
for f in BibCatalogSystem.TICKET_ATTRIBUTES:
Expand Down

0 comments on commit 83c131a

Please sign in to comment.