|
1 | 1 | import json |
| 2 | +import sys |
| 3 | + |
| 4 | +if sys.version_info >= (3,0): |
| 5 | + import urllib.parse |
| 6 | + dku_quote_fn = urllib.parse.quote |
| 7 | +else: |
| 8 | + import urllib |
| 9 | + dku_quote_fn = urllib.quote |
2 | 10 |
|
3 | 11 | class DSSObjectDiscussions(object): |
4 | 12 | """ |
5 | 13 | A handle to manage discussions on a DSS object |
6 | 14 | """ |
7 | 15 | def __init__(self, client, project_key, object_type, object_id): |
| 16 | + """ |
| 17 | + :param DSSClient client: an api client to connect to the DSS backend |
| 18 | + :param str project_key: identifier of the project to access |
| 19 | + ;param str object_type: DSS object type |
| 20 | + :param str object_id: DSS object ID |
| 21 | + """ |
8 | 22 | self.client = client |
9 | 23 | self.project_key = project_key |
10 | 24 | self.object_type = object_type |
11 | 25 | self.object_id = object_id |
| 26 | + # encode in UTF-8 if its python2 and unicode |
| 27 | + if sys.version_info < (3,0) and isinstance(self.object_id, unicode): |
| 28 | + self.object_id = self.object_id.encode('utf-8') |
12 | 29 |
|
13 | | - ########################## |
14 | | - # Discussions on an object |
15 | | - ########################## |
16 | 30 | def list_discussions(self): |
17 | 31 | """ |
18 | | - Get list of discussions |
| 32 | + Get the list of discussions on the object |
19 | 33 |
|
20 | | - Returns: |
21 | | - array of discussions on a DSS object |
| 34 | + :returns: list of discussions on the object |
| 35 | + :rtype: list |
22 | 36 | """ |
23 | | - return self.client._perform_json("GET", "/projects/%s/discussions/%s/%s/" % (self.project_key, self.object_type, self.object_id)) |
| 37 | + data = self.client._perform_json("GET", "/projects/%s/discussions/%s/%s/" % (self.project_key, self.object_type, self.object_id)) |
24 | 38 |
|
25 | | - def create_discussion(self, discussion_creator): |
26 | | - """ |
27 | | - Create a discussion |
| 39 | + discu_list = [] |
| 40 | + for discu_data in data: |
| 41 | + discu_list.append(DSSDiscussion(self.client, self.project_key, self.object_type, self.object_id, discu_data['id'], discu_data, False)) |
| 42 | + return discu_list |
28 | 43 |
|
29 | | - Args: |
30 | | - A DSSDiscussionCreator object containing the discussion definition |
| 44 | + def create_discussion(self, topic, message): |
| 45 | + """ |
| 46 | + Create a new discussion |
31 | 47 |
|
32 | | - Returns: |
33 | | - The newly created discussion |
| 48 | + :param str topic: the discussion topic |
| 49 | + :param str message: the markdown formatted first message |
| 50 | + :returns: the newly created discussion |
| 51 | + :rtype: DSSDiscussion |
34 | 52 | """ |
35 | | - return self.client._perform_json("POST", "/projects/%s/discussions/%s/%s/" % (self.project_key, self.object_type, self.object_id), body=discussion_creator.get_data()) |
| 53 | + creation_data = { |
| 54 | + "topic" : topic, |
| 55 | + "reply" : message |
| 56 | + } |
| 57 | + discu_data = self.client._perform_json("POST", "/projects/%s/discussions/%s/%s/" % (self.project_key, self.object_type, self.object_id), body=creation_data) |
| 58 | + return DSSDiscussion(self.client, self.project_key, self.object_type, self.object_id, discu_data['id'], discu_data, True) |
36 | 59 |
|
37 | 60 | def get_discussion(self, discussion_id): |
38 | 61 | """ |
39 | 62 | Get a specific discussion |
40 | 63 |
|
41 | | - Returns: |
42 | | - a discussion |
| 64 | + :param str discussion_id: the discussion ID |
| 65 | + :returns: the discussion |
| 66 | + :rtype: DSSDiscussion |
43 | 67 | """ |
44 | | - return DSSDiscussion(self.client, self.project_key, self.object_type, self.object_id, discussion_id) |
| 68 | + discu_data = self.client._perform_json("GET", "/projects/%s/discussions/%s/%s/%s" % (self.project_key, self.object_type, self.object_id, discussion_id)) |
| 69 | + return DSSDiscussion(self.client, self.project_key, self.object_type, self.object_id, discussion_id, discu_data, True) |
45 | 70 |
|
46 | 71 | class DSSDiscussion(object): |
47 | 72 | """ |
48 | | - A handle to manage a single discussion |
| 73 | + A handle to manage a discussion on a DSS object |
49 | 74 | """ |
50 | | - def __init__(self, client, project_key, object_type, object_id, discussion_id): |
| 75 | + def __init__(self, client, project_key, object_type, object_id, discussion_id, discussion_data, discussion_data_has_replies): |
| 76 | + """ |
| 77 | + :param DSSClient client: an api client to connect to the DSS backend |
| 78 | + :param str project_key: identifier of the project to access |
| 79 | + ;param str object_type: DSS object type |
| 80 | + :param str object_id: DSS object ID |
| 81 | + :param str discussion_id: identified of the discussion |
| 82 | + :param dict discussion_data: the discussion data |
| 83 | + :param bool discussion_data_has_replies: a flag that indicates if the replies has been loaded |
| 84 | + """ |
51 | 85 | self.client = client |
52 | 86 | self.project_key = project_key |
53 | 87 | self.object_type = object_type |
54 | 88 | self.object_id = object_id |
55 | 89 | self.discussion_id = discussion_id |
| 90 | + self.discussion_data = discussion_data |
| 91 | + self.discussion_data_has_replies = discussion_data_has_replies |
| 92 | + |
| 93 | + def _get_with_replies(self): |
| 94 | + """ |
| 95 | + Reload the discussion data from the backend including the replies |
| 96 | + """ |
| 97 | + self.discussion_data = self.client._perform_json("GET", "/projects/%s/discussions/%s/%s/%s" % (self.project_key, self.object_type, self.object_id, self.discussion_id)) |
| 98 | + self.discussion_data_has_replies = True |
56 | 99 |
|
57 | | - ######################## |
58 | | - # Single discussion |
59 | | - ######################## |
60 | | - def get(self): |
| 100 | + def get_metadata(self): |
61 | 101 | """ |
62 | | - Get the discussion details |
| 102 | + Get the discussion metadata |
63 | 103 |
|
64 | | - Returns: |
65 | | - The specific discussion |
| 104 | + :returns: the discussion metadata |
| 105 | + :rtype: dict |
66 | 106 | """ |
67 | | - return self.client._perform_json("GET", "/projects/%s/discussions/%s/%s/%s" % (self.project_key, self.object_type, self.object_id, self.discussion_id)) |
| 107 | + metadata = dict(self.discussion_data) |
| 108 | + if "replies" in metadata: |
| 109 | + del metadata["replies"] |
| 110 | + return metadata |
68 | 111 |
|
69 | | - def set(self, discussion_details): |
| 112 | + def set_metadata(self, discussion_metadata): |
70 | 113 | """ |
71 | | - Update the discussion |
| 114 | + Update the discussion metadata |
| 115 | +
|
| 116 | + :param dict discussion_metadata: the discussion metadata |
| 117 | + """ |
| 118 | + if not self.discussion_data_has_replies: |
| 119 | + self._get_with_replies() |
| 120 | + |
| 121 | + edited_metadata = dict(discussion_metadata) |
| 122 | + edited_metadata["replies"] = self.discussion_data["replies"] |
72 | 123 |
|
73 | | - Args: |
74 | | - The discussion definition as retrieved by the getter |
| 124 | + self.discussion_data = self.client._perform_json("PUT", "/projects/%s/discussions/%s/%s/%s" % (self.project_key, self.object_type, self.object_id, self.discussion_id), body=edited_metadata) |
| 125 | + self.discussion_data_has_replies = True |
75 | 126 |
|
76 | | - Returns: |
77 | | - The updated discussion |
| 127 | + def get_replies(self): |
78 | 128 | """ |
79 | | - return self.client._perform_json("PUT", "/projects/%s/discussions/%s/%s/%s" % (self.project_key, self.object_type, self.object_id, self.discussion_id), body=discussion_details) |
| 129 | + Get the list of replies in this discussion |
80 | 130 |
|
81 | | - def add_reply(self, reply): |
| 131 | + :returns: a list of replies |
| 132 | + :rtype: list |
82 | 133 | """ |
83 | | - Add a reply to a discussion |
| 134 | + if not self.discussion_data_has_replies: |
| 135 | + self._get_with_replies() |
84 | 136 |
|
85 | | - Args: |
86 | | - The reply message content |
| 137 | + reply_list = [] |
| 138 | + for reply_data in self.discussion_data["replies"]: |
| 139 | + reply_list.append(DSSDiscussionReply(reply_data)) |
| 140 | + return reply_list |
87 | 141 |
|
88 | | - Returns: |
89 | | - The updated discussion with details |
| 142 | + def add_reply(self, text): |
| 143 | + """ |
| 144 | + Add a reply to a discussion |
| 145 | +
|
| 146 | + :param str text: the markdown formatted text to reply |
90 | 147 | """ |
91 | 148 | reply_data = { |
92 | | - "reply": reply |
| 149 | + "reply": text |
93 | 150 | } |
94 | | - return self.client._perform_json("POST", "/projects/%s/discussions/%s/%s/%s/replies/" % (self.project_key, self.object_type, self.object_id, self.discussion_id), body=reply_data) |
| 151 | + self.discussion_data = self.client._perform_json("POST", "/projects/%s/discussions/%s/%s/%s/replies/" % (self.project_key, self.object_type, self.object_id, self.discussion_id), body=reply_data) |
| 152 | + self.discussion_data_has_replies = True |
95 | 153 |
|
96 | | -class DSSDiscussionCreator(object): |
| 154 | +class DSSDiscussionReply(object): |
97 | 155 | """ |
98 | | - A helper to create discussion |
| 156 | + A read-only handle to access a discussion reply |
99 | 157 | """ |
100 | | - def __init__(self, topic, first_message): |
101 | | - self.data = { |
102 | | - "topic" : topic, |
103 | | - "reply" : first_message |
104 | | - } |
| 158 | + def __init__(self, reply_data): |
| 159 | + """ |
| 160 | + :param dict reply_data: the reply data from the discussion |
| 161 | + """ |
| 162 | + self.reply_data = reply_data |
| 163 | + |
| 164 | + def get_reply_data(self): |
| 165 | + """ |
| 166 | + Get the reply data |
| 167 | +
|
| 168 | + :returns: the reply data |
| 169 | + :rtype: dict |
| 170 | + """ |
| 171 | + return self.reply_data |
| 172 | + |
| 173 | + def get_text(self): |
| 174 | + """ |
| 175 | + Get the reply text |
| 176 | +
|
| 177 | + :returns: the reply text |
| 178 | + :rtype: str |
| 179 | + """ |
| 180 | + return self.reply_data["text"] |
| 181 | + |
| 182 | + def get_author(self): |
| 183 | + """ |
| 184 | + Get the reply author |
| 185 | +
|
| 186 | + :returns: the author ID |
| 187 | + :rtype: str |
| 188 | + """ |
| 189 | + return self.reply_data["author"] |
105 | 190 |
|
106 | | - def get_data(self): |
107 | | - return self.data |
| 191 | + def get_timestamp(self): |
| 192 | + """ |
| 193 | + Get the reply timestamp |
108 | 194 |
|
109 | | - def set_topic(self, topic): |
110 | | - self.data["topic"] = topic |
| 195 | + :returns: the reply timestamp |
| 196 | + :rtype: long |
| 197 | + """ |
| 198 | + return self.reply_data["time"] |
111 | 199 |
|
112 | | - def set_first_message(self, first_message): |
113 | | - self.data["reply"] = first_message |
| 200 | + def get_edited_timestamp(self): |
| 201 | + """ |
| 202 | + Get the last edition timestamp |
| 203 | +
|
| 204 | + :returns: the last edition timestamp |
| 205 | + :rtype: long |
| 206 | + """ |
| 207 | + return self.reply_data["editedOn"] |
0 commit comments