Skip to content

Commit 9caf751

Browse files
committed
improved discu public api + few minor things on wiki
1 parent aa2bef5 commit 9caf751

2 files changed

Lines changed: 171 additions & 73 deletions

File tree

dataikuapi/dss/discussion.py

Lines changed: 150 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,207 @@
11
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
210

311
class DSSObjectDiscussions(object):
412
"""
513
A handle to manage discussions on a DSS object
614
"""
715
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+
"""
822
self.client = client
923
self.project_key = project_key
1024
self.object_type = object_type
1125
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')
1229

13-
##########################
14-
# Discussions on an object
15-
##########################
1630
def list_discussions(self):
1731
"""
18-
Get list of discussions
32+
Get the list of discussions on the object
1933
20-
Returns:
21-
array of discussions on a DSS object
34+
:returns: list of discussions on the object
35+
:rtype: list
2236
"""
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))
2438

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
2843

29-
Args:
30-
A DSSDiscussionCreator object containing the discussion definition
44+
def create_discussion(self, topic, message):
45+
"""
46+
Create a new discussion
3147
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
3452
"""
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)
3659

3760
def get_discussion(self, discussion_id):
3861
"""
3962
Get a specific discussion
4063
41-
Returns:
42-
a discussion
64+
:param str discussion_id: the discussion ID
65+
:returns: the discussion
66+
:rtype: DSSDiscussion
4367
"""
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)
4570

4671
class DSSDiscussion(object):
4772
"""
48-
A handle to manage a single discussion
73+
A handle to manage a discussion on a DSS object
4974
"""
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+
"""
5185
self.client = client
5286
self.project_key = project_key
5387
self.object_type = object_type
5488
self.object_id = object_id
5589
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
5699

57-
########################
58-
# Single discussion
59-
########################
60-
def get(self):
100+
def get_metadata(self):
61101
"""
62-
Get the discussion details
102+
Get the discussion metadata
63103
64-
Returns:
65-
The specific discussion
104+
:returns: the discussion metadata
105+
:rtype: dict
66106
"""
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
68111

69-
def set(self, discussion_details):
112+
def set_metadata(self, discussion_metadata):
70113
"""
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"]
72123

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
75126

76-
Returns:
77-
The updated discussion
127+
def get_replies(self):
78128
"""
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
80130
81-
def add_reply(self, reply):
131+
:returns: a list of replies
132+
:rtype: list
82133
"""
83-
Add a reply to a discussion
134+
if not self.discussion_data_has_replies:
135+
self._get_with_replies()
84136

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
87141

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
90147
"""
91148
reply_data = {
92-
"reply": reply
149+
"reply": text
93150
}
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
95153

96-
class DSSDiscussionCreator(object):
154+
class DSSDiscussionReply(object):
97155
"""
98-
A helper to create discussion
156+
A read-only handle to access a discussion reply
99157
"""
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"]
105190

106-
def get_data(self):
107-
return self.data
191+
def get_timestamp(self):
192+
"""
193+
Get the reply timestamp
108194
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"]
111199

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"]

dataikuapi/dss/wiki.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
class DSSWiki(object):
1313
"""
1414
A handle to manage the wiki of a project
15-
16-
:param client: an api client to connect to the DSS backend
17-
:param project_key: identifier of the project to access
1815
"""
1916
def __init__(self, client, project_key):
17+
"""
18+
:param DSSClient client: an api client to connect to the DSS backend
19+
:param str project_key: identifier of the project to access
20+
"""
2021
self.client = client
2122
self.project_key = project_key
2223

@@ -91,12 +92,13 @@ def create_article(self, article_id, parent_id=None, content=None):
9192
class DSSWikiSettings(object):
9293
"""
9394
Global settings for the wiki, including taxonomy. Call save() to save
94-
95-
:param client: an api client to connect to the DSS backend
96-
:param project_key: identifier of the project to access
97-
:param dict settings: current wiki settings (containing taxonomy and home article)
9895
"""
9996
def __init__(self, client, project_key, settings):
97+
"""
98+
:param DSSClient client: an api client to connect to the DSS backend
99+
:param str project_key: identifier of the project to access
100+
:param dict settings: current wiki settings (containing taxonomy and home article)
101+
"""
100102
self.client = client
101103
self.project_key = project_key
102104
self.settings = settings
@@ -144,12 +146,13 @@ def save(self):
144146
class DSSWikiArticle(object):
145147
"""
146148
A handle to manage an article
147-
148-
:param DSSClient client: an api client to connect to the DSS backend
149-
:param str project_key: identifier of the project to access
150-
:param str article_id: the article ID
151149
"""
152150
def __init__(self, client, project_key, article_id):
151+
"""
152+
:param DSSClient client: an api client to connect to the DSS backend
153+
:param str project_key: identifier of the project to access
154+
:param str article_id: the article ID
155+
"""
153156
self.client = client
154157
self.project_key = project_key
155158
self.article_id = article_id
@@ -171,7 +174,7 @@ def upload_attachement(self, fp):
171174
"""
172175
Upload an attachment file and attaches it to the article
173176
174-
:param filetype fp: A file-like object that represents the upload file
177+
:param file fp: A file-like object that represents the upload file
175178
"""
176179
self.client._perform_json("POST", "/projects/%s/wiki/%s/upload" % (self.project_key, dku_quote_fn(self.article_id)), files={"file":fp})
177180

@@ -193,13 +196,14 @@ def get_object_discussions(self):
193196
class DSSWikiArticleData(object):
194197
"""
195198
A handle to manage an article
196-
197-
:param DSSClient client: an api client to connect to the DSS backend
198-
:param str project_key: identifier of the project to access
199-
:param str article_id: the article ID
200-
:param dict article_data: the article data got from the backend
201199
"""
202200
def __init__(self, client, project_key, article_id, article_data):
201+
"""
202+
:param DSSClient client: an api client to connect to the DSS backend
203+
:param str project_key: identifier of the project to access
204+
:param str article_id: the article ID
205+
:param dict article_data: the article data got from the backend
206+
"""
203207
self.client = client
204208
self.project_key = project_key
205209
self.article_id = article_id # don't need to check unicode here (already done in DSSWikiArticle)

0 commit comments

Comments
 (0)