From 7379e4a44c5b1f0435eae9259fd65dd889f281c4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 27 2015 10:25:09 +0000 Subject: Move the code translating an issue to JSON into the Issue object itself --- diff --git a/progit/lib/__init__.py b/progit/lib/__init__.py index e0cb57b..1ee9015 100644 --- a/progit/lib/__init__.py +++ b/progit/lib/__init__.py @@ -10,7 +10,6 @@ import datetime -import json import os import random import shutil @@ -884,40 +883,6 @@ def update_user_ssh(session, user, ssh_key): 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) - - def update_git_ticket(issue, repo, ticketfolder): """ Update the given issue in its git. diff --git a/progit/lib/model.py b/progit/lib/model.py index c1fb2e7..7c22550 100644 --- a/progit/lib/model.py +++ b/progit/lib/model.py @@ -13,6 +13,7 @@ import pkg_resources import datetime import logging +import json import sqlalchemy as sa from sqlalchemy import create_engine @@ -326,6 +327,39 @@ class Issue(BASE): ''' return '%s-ticket-%s@progit' % (self.project.name, self.id) + def to_json(self): + ''' Returns a JSON representation of the issue using the JSON module + + ''' + 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) + class IssueToIssue(BASE): """ Stores the parent/child relationship between two issues.