diff --git a/progit/lib.py b/progit/lib.py index 9559a27..1049c8f 100644 --- a/progit/lib.py +++ b/progit/lib.py @@ -9,6 +9,7 @@ """ +import json import os import sqlalchemy @@ -602,3 +603,37 @@ def update_user_ssh(session, user, ssh_key): message = 'Public ssh key updated' return message + + +def issue_to_json(issue): + """ Convert all the data related to an issue (a ticket) as json object. + + """ + output = { + 'title': issue.title, + 'content': issue.content, + 'status': issue.status, + 'date_created': issue.date_created.strftime('%s'), + 'user': { + 'name': issue.user.user, + 'emails': [email.email for email in issue.user.emails], + } + } + + comments = [] + for comment in issue.comments: + cmt = { + 'id': comment.id, + 'comment': comment.comment, + 'parent': comment.parent_id, + 'date_created': comment.date_created.strftime('%s'), + 'user': { + 'name': comment.user.user, + 'emails': [email.email for email in comment.user.emails], + } + } + comments.append(cmt) + + output['comments'] = comments + + return json.dumps(output)