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