11from .discussion import DSSObjectDiscussions
2+ from dataikuapi .utils import DataikuException
23import json
34import sys
45
@@ -14,19 +15,16 @@ class DSSWiki(object):
1415 A handle to manage the wiki of a project
1516 """
1617 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- """
18+ """Do not call directly, use :meth:`dataikuapi.dss.project.DSSProject.get_wiki`"""
2119 self .client = client
2220 self .project_key = project_key
2321
2422 def get_settings (self ):
2523 """
2624 Get wiki settings
2725
28- :returns: an handle to manage the wiki settings (taxonomy, home article)
29- :rtype: DSSWikiSettings
26+ :returns: a handle to manage the wiki settings (taxonomy, home article)
27+ :rtype: :class:`dataikuapi.dss.wiki. DSSWikiSettings`
3028 """
3129 return DSSWikiSettings (self .client , self .project_key , self .client ._perform_json ("GET" , "/projects/%s/wiki/" % (self .project_key )))
3230
@@ -35,8 +33,8 @@ def get_article(self, article_id):
3533 Get a wiki article
3634
3735 :param str article_id: the article ID
38- :returns: an handle to manage the Article
39- :rtype: DSSWikiArticle
36+ :returns: a handle to manage the Article
37+ :rtype: :class:`dataikuapi.dss.wiki. DSSWikiArticle`
4038 """
4139 return DSSWikiArticle (self .client , self .project_key , article_id )
4240
@@ -46,7 +44,7 @@ def __flatten_taxonomy__(self, taxonomy):
4644
4745 :param list taxonomy:
4846 :returns: list of articles
49- :rtype: list
47+ :rtype: list of :class:`dataikuapi.dss.wiki.DSSWikiArticle`
5048 """
5149 article_list = []
5250 for article in taxonomy :
@@ -56,10 +54,10 @@ def __flatten_taxonomy__(self, taxonomy):
5654
5755 def list_articles (self ):
5856 """
59- Get a list of all the articles
57+ Get a list of all the articles in form of :class:`dataikuapi.dss.wiki.DSSWikiArticle` objects
6058
6159 :returns: list of articles
62- :rtype: list
60+ :rtype: list of :class:`dataikuapi.dss.wiki.DSSWikiArticle`
6361 """
6462 return self .__flatten_taxonomy__ (self .get_settings ().get_taxonomy ())
6563
@@ -71,7 +69,7 @@ def create_article(self, article_id, parent_id=None, content=None):
7169 :param str parent_id: the parent article ID (or None if the article has to be at root level)
7270 :param str content: the article content
7371 :returns: the created article
74- :rtype: DSSWikiArticle
72+ :rtype: :class:`dataikuapi.dss.wiki. DSSWikiArticle`
7573 """
7674 body = {
7775 "projectKey" : self .project_key ,
@@ -94,24 +92,81 @@ class DSSWikiSettings(object):
9492 Global settings for the wiki, including taxonomy. Call save() to save
9593 """
9694 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- """
95+ """Do not call directly, use :meth:`dataikuapi.dss.wiki.DSSWiki.get_settings`"""
10296 self .client = client
10397 self .project_key = project_key
10498 self .settings = settings
10599
106100 def get_taxonomy (self ):
107101 """
108102 Get the taxonomy
103+ The taxonomy is an array listing at top level the root article IDs and their children in a tree format.
104+ Every existing article of the wiki has to be in the taxonomy once and only once.
105+ For instance:
106+ [
107+ {
108+ 'id': 'article1',
109+ 'children': []
110+ },
111+ {
112+ 'id': 'article2',
113+ 'children': [
114+ {
115+ 'id': 'article3',
116+ 'children': []
117+ }
118+ ]
119+ }
120+ ]
121+ Note that this is a direct reference, not a copy, so modifications to the returned object will be reflected when saving
109122
110123 :returns: The taxonomy
111124 :rtype: list
112125 """
113126 return self .settings ["taxonomy" ]
114127
128+ def __retrieve_article_in_taxonomy__ (self , taxonomy , article_id , remove = False ):
129+ """
130+ Private recusive method that get the sub tree structure from the taxonomy for a specific article
131+
132+ :param list taxonomy: the current level of taxonomy
133+ :param str article_id: the article to retrieve
134+ :param bool remove: either remove the sub tree structure or not
135+ """
136+ idx = 0
137+ for tax_article in taxonomy :
138+ if tax_article ["id" ] == article_id :
139+ ret = taxonomy .pop (idx ) if remove else taxonomy [idx ]
140+ return ret
141+ children_ret = self .__retrieve_article_in_taxonomy__ (tax_article ["children" ], article_id , remove )
142+ if children_ret is not None :
143+ return children_ret
144+ idx += 1
145+ return None
146+
147+ def move_article_in_taxonomy (self , article_id , parent_article_id = None ):
148+ """
149+ An helper to update the taxonomy by moving an article with its children as a child of another article
150+
151+ :param str article_id: the main article ID
152+ :param str parent_article_id: the new parent article ID or None for root level
153+ """
154+ old_taxonomy = list (self .settings ["taxonomy" ])
155+
156+ tax_article = self .__retrieve_article_in_taxonomy__ (self .settings ["taxonomy" ], article_id , True )
157+ if tax_article is None :
158+ raise DataikuException ("Article not found: %s" % (article_id ))
159+
160+ if parent_article_id is None :
161+ self .settings ["taxonomy" ].append (tax_article )
162+ else :
163+ tax_parent_article = self .__retrieve_article_in_taxonomy__ (self .settings ["taxonomy" ], parent_article_id , False )
164+ if tax_article is None :
165+ self .settings ["taonomy" ] = old_taxonomy
166+ raise DataikuException ("Parent article not found (or is one of the article descendants): %s" % (parent_article_id ))
167+ tax_parent_article ["children" ].append (tax_article )
168+
169+
115170 def set_taxonomy (self , taxonomy ):
116171 """
117172 Set the taxonomy
@@ -148,11 +203,7 @@ class DSSWikiArticle(object):
148203 A handle to manage an article
149204 """
150205 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- """
206+ """Do not call directly, use :meth:`dataikuapi.dss.wiki.DSSWiki.get_article`"""
156207 self .client = client
157208 self .project_key = project_key
158209 self .article_id = article_id
@@ -165,18 +216,20 @@ def get_data(self):
165216 Get article data handle
166217
167218 :returns: the article data handle
168- :rtype: DSSWikiArticleData
219+ :rtype: :class:`dataikuapi.dss.wiki. DSSWikiArticleData`
169220 """
170221 article_data = self .client ._perform_json ("GET" , "/projects/%s/wiki/%s" % (self .project_key , dku_quote_fn (self .article_id )))
171222 return DSSWikiArticleData (self .client , self .project_key , self .article_id , article_data )
172223
173- def upload_attachement (self , fp ):
224+ def upload_attachement (self , fp , filename ):
174225 """
175226 Upload an attachment file and attaches it to the article
227+ Note that the type of file will be determined by the filename extension
176228
177229 :param file fp: A file-like object that represents the upload file
230+ :param str filename: The attachement filename
178231 """
179- self .client ._perform_json ("POST" , "/projects/%s/wiki/%s/upload" % (self .project_key , dku_quote_fn (self .article_id )), files = {"file" :fp })
232+ self .client ._perform_json ("POST" , "/projects/%s/wiki/%s/upload" % (self .project_key , dku_quote_fn (self .article_id )), files = {"file" :( filename , fp ) })
180233
181234 def delete (self ):
182235 """
@@ -189,7 +242,7 @@ def get_object_discussions(self):
189242 Get a handle to manage discussions on the article
190243
191244 :returns: the handle to manage discussions
192- :rtype: DSSObjectDiscussions
245+ :rtype: :class:`dataikuapi.dss.wiki. DSSObjectDiscussions`
193246 """
194247 return DSSObjectDiscussions (self .client , self .project_key , "ARTICLE" , self .article_id )
195248
@@ -198,12 +251,7 @@ class DSSWikiArticleData(object):
198251 A handle to manage an article
199252 """
200253 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- """
254+ """Do not call directly, use :meth:`dataikuapi.dss.wiki.DSSWikiArticle.get_data`"""
207255 self .client = client
208256 self .project_key = project_key
209257 self .article_id = article_id # don't need to check unicode here (already done in DSSWikiArticle)
@@ -229,6 +277,7 @@ def set_body(self, content):
229277 def get_metadata (self ):
230278 """
231279 Get the article metadata
280+ Note that this is a direct reference, not a copy, so modifications to the returned object will be reflected when saving
232281
233282 :returns: the article metadata
234283 :rtype: dict
0 commit comments