Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Format' support for search() (format' parameter of Ticket Search) #17

Merged
merged 1 commit into from
Jun 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 50 additions & 31 deletions rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def last_updated(self, since, queue=None):
"""
return self.search(Queue=queue, order='-LastUpdated', LastUpdatedBy__notexact=self.default_login, LastUpdated__gt=since)

def search(self, Queue=None, order=None, raw_query=None, **kwargs):
def search(self, Queue=None, order=None, raw_query=None, Format='l', **kwargs):
""" Search arbitrary needles in given fields and queue.

Example::
Expand All @@ -413,6 +413,10 @@ def search(self, Queue=None, order=None, raw_query=None, **kwargs):
kwargs, so use these instead of including them in
the raw query. You can refer to the RT query builder.
If passing raw_query, all other **kwargs will be ignored.
:keyword Format: Format of the query:
- i: only `id' fields are populated
- s: only `id' and `subject' fields are populated
- l: multi-line format, all fields are populated
:keyword kwargs: Other arguments possible to set if not passing raw_query:

Requestors, Subject, Cc, AdminCc, Owner, Status,
Expand Down Expand Up @@ -471,7 +475,7 @@ def search(self, Queue=None, order=None, raw_query=None, **kwargs):
get_params['query'] = ' AND '.join('(' + part + ')' for part in query)
if order:
get_params['orderby'] = order
get_params['format'] = 'l'
get_params['format'] = Format

msg = self.__request(url, get_params=get_params)
lines = msg.split('\n')
Expand All @@ -481,37 +485,52 @@ def search(self, Queue=None, order=None, raw_query=None, **kwargs):
if lines[2].startswith('No matching results.'):
return []

msgs = map(lambda x: x.split('\n'), msg.split('\n--\n'))
items = []
for msg in msgs:
pairs = {}
req_matching = [i for i, m in enumerate(msg) if self.RE_PATTERNS['requestors_pattern'].match(m)]
req_id = req_matching[0] if req_matching else None
if not req_id:
raise UnexpectedMessageFormat('Missing line starting with `Requestors:`.')
for i in range(req_id):
if ': ' in msg[i]:
header, content = msg[i].split(': ', 1)
pairs[header.strip()] = content.strip()
requestors = [msg[req_id][12:]]
req_id += 1
while (req_id < len(msg)) and (msg[req_id][:12] == ' ' * 12):
requestors.append(msg[req_id][12:])
if Format == 'l':
msgs = map(lambda x: x.split('\n'), msg.split('\n--\n'))
items = []
for msg in msgs:
pairs = {}
req_matching = [i for i, m in enumerate(msg) if self.RE_PATTERNS['requestors_pattern'].match(m)]
req_id = req_matching[0] if req_matching else None
if not req_id:
raise UnexpectedMessageFormat('Missing line starting with `Requestors:`.')
for i in range(req_id):
if ': ' in msg[i]:
header, content = msg[i].split(': ', 1)
pairs[header.strip()] = content.strip()
requestors = [msg[req_id][12:]]
req_id += 1
pairs['Requestors'] = self.__normalize_list(requestors)
for i in range(req_id, len(msg)):
if ': ' in msg[i]:
header, content = msg[i].split(': ', 1)
pairs[header.strip()] = content.strip()
if pairs:
items.append(pairs)
while (req_id < len(msg)) and (msg[req_id][:12] == ' ' * 12):
requestors.append(msg[req_id][12:])
req_id += 1
pairs['Requestors'] = self.__normalize_list(requestors)
for i in range(req_id, len(msg)):
if ': ' in msg[i]:
header, content = msg[i].split(': ', 1)
pairs[header.strip()] = content.strip()
if pairs:
items.append(pairs)

if 'Cc' in pairs:
pairs['Cc'] = self.__normalize_list(pairs['Cc'])
if 'AdminCc' in pairs:
pairs['AdminCc'] = self.__normalize_list(pairs['AdminCc'])
return items
elif Format == 's':
items = []
msgs = msg.splitlines()[2:]
for msg in msgs:
ticket_id, subject = msg.split(': ', 1)
items.append({ 'id': 'ticket/' + ticket_id, 'Subject': subject })
return items
elif Format == 'i':
items = []
msgs = msg.splitlines()[2:]
for msg in msgs:
_, ticket_id = msg.split('/', 1)
items.append({ 'id': 'ticket/' + ticket_id })
return items

if 'Cc' in pairs:
pairs['Cc'] = self.__normalize_list(pairs['Cc'])
if 'AdminCc' in pairs:
pairs['AdminCc'] = self.__normalize_list(pairs['AdminCc'])

return items

def get_ticket(self, ticket_id):
""" Fetch ticket by its ID.
Expand Down