Skip to content

Commit 5ac2406

Browse files
committed
improved wiki public api
1 parent 0d12091 commit 5ac2406

1 file changed

Lines changed: 179 additions & 65 deletions

File tree

dataikuapi/dss/wiki.py

Lines changed: 179 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,245 @@
11
from .discussion import DSSObjectDiscussions
22
import json
3-
import urllib
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
410

511
class DSSWiki(object):
612
"""
713
A handle to manage the wiki of a project
14+
15+
:param client: an api client to connect to the DSS backend
16+
:param project_key: identifier of the project to access
817
"""
918
def __init__(self, client, project_key):
1019
self.client = client
1120
self.project_key = project_key
1221

13-
###############################
14-
# Wiki definition
15-
###############################
16-
def get(self):
22+
def get_settings(self):
1723
"""
18-
Get wiki definition
24+
Get wiki settings
1925
20-
Returns:
21-
the wiki (dict with project key, home article and taxonomy)
26+
:returns: an handle to manage the wiki settings (taxonomy, home article)
27+
:rtype: DSSWikiSettings
2228
"""
23-
return self.client._perform_json("GET", "/projects/%s/wiki/" % (self.project_key))
29+
return DSSWikiSettings(self.client, self.project_key, self.client._perform_json("GET", "/projects/%s/wiki/" % (self.project_key)))
2430

25-
def set(self, wiki):
31+
def get_article(self, article_id):
2632
"""
27-
Set wiki definition
33+
Get a wiki article
2834
29-
Args:
30-
the wiki (dict with project key, home article and taxonomy)
35+
:param str article_id: the article ID
36+
:returns: an handle to manage the Article
37+
:rtype: DSSWikiArticle
38+
"""
39+
return DSSWikiArticle(self.client, self.project_key, article_id)
3140

32-
Returns:
33-
the updated wiki
41+
def __flatten_taxonomy__(self, taxonomy):
3442
"""
35-
return self.client._perform_json("PUT", "/projects/%s/wiki/" % (self.project_key), body=wiki)
43+
Private recursive method to get the flatten list of article IDs from the taxonomy
3644
37-
def create_article(self, article_id, parent_id):
45+
:param list taxonomy:
46+
:returns: list of articles
47+
:rtype: list
3848
"""
39-
Create a wiki article
49+
article_list = []
50+
for article in taxonomy:
51+
article_list.append(self.get_article(article['id']))
52+
article_list += self.__flatten_taxonomy__(article['children'])
53+
return article_list
54+
55+
def list_articles(self):
56+
"""
57+
Get a list of all the articles
4058
41-
Args:
42-
the article ID
43-
the parent article ID or None if no parent (at wiki root scope)
59+
:returns: list of articles
60+
:rtype: list
61+
"""
62+
return self.__flatten_taxonomy__(self.get_settings().get_taxonomy())
4463

45-
Returns:
46-
the newly created article definition with payload (article content)
64+
def create_article(self, article_id, parent_id=None, content=None):
65+
"""
66+
Create a wiki article
67+
68+
:param str article_id: the article ID
69+
:param str parent_id: the parent article ID (or None if the article has to be at root level)
70+
:param str content: the article content
71+
:returns: the created article
72+
:rtype: DSSWikiArticle
4773
"""
4874
body = {
4975
"projectKey": self.project_key,
5076
"id": article_id,
5177
"parent": parent_id
5278
}
53-
return self.client._perform_json("POST", "/projects/%s/wiki/" % (self.project_key), body=body)
79+
self.client._perform_json("POST", "/projects/%s/wiki/" % (self.project_key), body=body)
80+
article = DSSWikiArticle(self.client, self.project_key, article_id)
5481

55-
def get_article(self, article_id):
82+
# set article content if given
83+
if content is not None:
84+
article_data = article.get_data()
85+
article_data.set_body(content)
86+
article_data.save()
87+
88+
return article
89+
90+
class DSSWikiSettings(object):
91+
"""
92+
Global settings for the wiki, including taxonomy. Call save() to save
93+
94+
:param client: an api client to connect to the DSS backend
95+
:param project_key: identifier of the project to access
96+
:param dict settings: current wiki settings (containing taxonomy and home article)
97+
"""
98+
def __init__(self, client, project_key, settings):
99+
self.client = client
100+
self.project_key = project_key
101+
self.settings = settings
102+
103+
def get_taxonomy(self):
56104
"""
57-
Get a wiki article
105+
Get the taxonomy
58106
59-
Args:
60-
the article ID
107+
:returns: The taxonomy
108+
:rtype: list
109+
"""
110+
return self.settings["taxonomy"]
61111

62-
Returns:
63-
the wiki article object
112+
def set_taxonomy(self, taxonomy):
64113
"""
65-
return DSSWikiArticle(self.client, self.project_key, article_id)
114+
Set the taxonomy
115+
116+
:param list taxonomy: the taxonomy
117+
"""
118+
self.settings["taxonomy"] = taxonomy
119+
120+
def get_home_article_id(self):
121+
"""
122+
Get the home article ID
123+
124+
:returns: The home article ID
125+
:rtype: str
126+
"""
127+
return self.settings["homeArticleId"]
128+
129+
def set_home_article_id(self, home_article_id):
130+
"""
131+
Set the home article ID
132+
133+
:param str home_article_id: the home article ID
134+
"""
135+
self.settings["homeArticleId"] = home_article_id
136+
137+
def save(self):
138+
"""
139+
Save the current settings to the backend
140+
"""
141+
self.settings = self.client._perform_json("PUT", "/projects/%s/wiki/" % (self.project_key), body=self.settings)
66142

67143
class DSSWikiArticle(object):
68144
"""
69145
A handle to manage an article
146+
147+
:param DSSClient client: an api client to connect to the DSS backend
148+
:param str project_key: identifier of the project to access
149+
:param str article_id: the article ID
70150
"""
71151
def __init__(self, client, project_key, article_id):
72152
self.client = client
73153
self.project_key = project_key
74154
self.article_id = article_id
75-
if isinstance(self.article_id, unicode):
155+
# encode in UTF-8 if its python2 and unicode
156+
if sys.version_info < (3,0) and isinstance(uni, unicode):
76157
self.article_id = self.article_id.encode('utf-8')
77158

78-
##########################
79-
# Article definition
80-
##########################
81-
def get(self):
159+
def get_data(self):
82160
""""
83-
Get article definition
84-
85-
Returns:
86-
the article definition with payload (article content)
87-
"""
88-
return self.client._perform_json("GET", "/projects/%s/wiki/%s" % (self.project_key, urllib.quote(self.article_id)))
89-
90-
def set(self, article_with_payload):
91-
"""
92-
Set article definition
93-
94-
Args:
95-
the article definition with payload (article content)
161+
Get article data handle
96162
97-
Returns:
98-
the updated article
163+
:returns: the article data handle
164+
:rtype: DSSWikiArticleData
99165
"""
100-
return self.client._perform_json("PUT", "/projects/%s/wiki/%s" % (self.project_key, urllib.quote(self.article_id)), body=article_with_payload)
166+
article_data = self.client._perform_json("GET", "/projects/%s/wiki/%s" % (self.project_key, dku_quote_fn(self.article_id)))
167+
return DSSWikiArticleData(self.client, self.project_key, self.article_id, article_data)
101168

102-
def upload(self, fp):
169+
def upload_attachement(self, fp):
103170
"""
104171
Upload an attachment file and attaches it to the article
105172
106-
Args:
107-
A file-like object that represents the upload file
108-
109-
Returns:
110-
the updated article
173+
:param filetype fp: A file-like object that represents the upload file
111174
"""
112-
return self.client._perform_json("POST", "/projects/%s/wiki/%s/upload" % (self.project_key, urllib.quote(self.article_id)), files={"file":fp})
175+
self.client._perform_json("POST", "/projects/%s/wiki/%s/upload" % (self.project_key, dku_quote_fn(self.article_id)), files={"file":fp})
113176

114177
def delete(self):
115178
"""
116179
Delete the article
117180
"""
118-
self.client._perform_empty("DELETE", "/projects/%s/wiki/%s" % (self.project_key, urllib.quote(self.article_id)))
181+
self.client._perform_empty("DELETE", "/projects/%s/wiki/%s" % (self.project_key, dku_quote_fn(self.article_id)))
119182

120-
########################################################
121-
# Discussions
122-
########################################################
123183
def get_object_discussions(self):
124184
"""
125185
Get a handle to manage discussions on the article
126186
127-
Returns:
128-
the DSSObjectDiscussions of this article
187+
:returns: the discussion handle for this article
188+
:rtype: DSSObjectDiscussions
129189
"""
130190
return DSSObjectDiscussions(self.client, self.project_key, "ARTICLE", self.article_id)
131191

192+
class DSSWikiArticleData(object):
193+
"""
194+
A handle to manage an article
195+
196+
:param DSSClient client: an api client to connect to the DSS backend
197+
:param str project_key: identifier of the project to access
198+
:param str article_id: the article ID
199+
:param dict article_data: the article data got from the backend
200+
"""
201+
def __init__(self, client, project_key, article_id, article_data):
202+
self.client = client
203+
self.project_key = project_key
204+
self.article_id = article_id # don't need to check unicode here (already done in DSSWikiArticle)
205+
self.article_data = article_data
206+
207+
def get_body(self):
208+
"""
209+
Get the markdown body as string
210+
211+
:returns: the article body
212+
:rtype: str
213+
"""
214+
return self.article_data["payload"]
215+
216+
def set_body(self, content):
217+
"""
218+
Set the markdown body
219+
220+
:param str content: the article content
221+
"""
222+
self.article_data["payload"] = content
223+
224+
def get_metadata(self):
225+
"""
226+
Get the article metadata
227+
228+
:returns: the article metadata
229+
:rtype: dict
230+
"""
231+
return self.article_data["article"]
232+
233+
def set_metadata(self, metadata):
234+
"""
235+
Set the article metadata
236+
237+
:param dict article: the article metadata
238+
"""
239+
self.article_data["article"] = article
240+
241+
def save(self):
242+
"""
243+
Save the current article data to the backend
244+
"""
245+
self.article_data = self.client._perform_json("PUT", "/projects/%s/wiki/%s" % (self.project_key, dku_quote_fn(self.article_id)), body=self.article_data)

0 commit comments

Comments
 (0)