|
1 | 1 | from .discussion import DSSObjectDiscussions |
2 | 2 | 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 |
4 | 10 |
|
5 | 11 | class DSSWiki(object): |
6 | 12 | """ |
7 | 13 | 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 |
8 | 17 | """ |
9 | 18 | def __init__(self, client, project_key): |
10 | 19 | self.client = client |
11 | 20 | self.project_key = project_key |
12 | 21 |
|
13 | | - ############################### |
14 | | - # Wiki definition |
15 | | - ############################### |
16 | | - def get(self): |
| 22 | + def get_settings(self): |
17 | 23 | """ |
18 | | - Get wiki definition |
| 24 | + Get wiki settings |
19 | 25 |
|
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 |
22 | 28 | """ |
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))) |
24 | 30 |
|
25 | | - def set(self, wiki): |
| 31 | + def get_article(self, article_id): |
26 | 32 | """ |
27 | | - Set wiki definition |
| 33 | + Get a wiki article |
28 | 34 |
|
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) |
31 | 40 |
|
32 | | - Returns: |
33 | | - the updated wiki |
| 41 | + def __flatten_taxonomy__(self, taxonomy): |
34 | 42 | """ |
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 |
36 | 44 |
|
37 | | - def create_article(self, article_id, parent_id): |
| 45 | + :param list taxonomy: |
| 46 | + :returns: list of articles |
| 47 | + :rtype: list |
38 | 48 | """ |
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 |
40 | 58 |
|
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()) |
44 | 63 |
|
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 |
47 | 73 | """ |
48 | 74 | body = { |
49 | 75 | "projectKey": self.project_key, |
50 | 76 | "id": article_id, |
51 | 77 | "parent": parent_id |
52 | 78 | } |
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) |
54 | 81 |
|
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): |
56 | 104 | """ |
57 | | - Get a wiki article |
| 105 | + Get the taxonomy |
58 | 106 |
|
59 | | - Args: |
60 | | - the article ID |
| 107 | + :returns: The taxonomy |
| 108 | + :rtype: list |
| 109 | + """ |
| 110 | + return self.settings["taxonomy"] |
61 | 111 |
|
62 | | - Returns: |
63 | | - the wiki article object |
| 112 | + def set_taxonomy(self, taxonomy): |
64 | 113 | """ |
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) |
66 | 142 |
|
67 | 143 | class DSSWikiArticle(object): |
68 | 144 | """ |
69 | 145 | 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 |
70 | 150 | """ |
71 | 151 | def __init__(self, client, project_key, article_id): |
72 | 152 | self.client = client |
73 | 153 | self.project_key = project_key |
74 | 154 | 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): |
76 | 157 | self.article_id = self.article_id.encode('utf-8') |
77 | 158 |
|
78 | | - ########################## |
79 | | - # Article definition |
80 | | - ########################## |
81 | | - def get(self): |
| 159 | + def get_data(self): |
82 | 160 | """" |
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 |
96 | 162 |
|
97 | | - Returns: |
98 | | - the updated article |
| 163 | + :returns: the article data handle |
| 164 | + :rtype: DSSWikiArticleData |
99 | 165 | """ |
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) |
101 | 168 |
|
102 | | - def upload(self, fp): |
| 169 | + def upload_attachement(self, fp): |
103 | 170 | """ |
104 | 171 | Upload an attachment file and attaches it to the article |
105 | 172 |
|
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 |
111 | 174 | """ |
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}) |
113 | 176 |
|
114 | 177 | def delete(self): |
115 | 178 | """ |
116 | 179 | Delete the article |
117 | 180 | """ |
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))) |
119 | 182 |
|
120 | | - ######################################################## |
121 | | - # Discussions |
122 | | - ######################################################## |
123 | 183 | def get_object_discussions(self): |
124 | 184 | """ |
125 | 185 | Get a handle to manage discussions on the article |
126 | 186 |
|
127 | | - Returns: |
128 | | - the DSSObjectDiscussions of this article |
| 187 | + :returns: the discussion handle for this article |
| 188 | + :rtype: DSSObjectDiscussions |
129 | 189 | """ |
130 | 190 | return DSSObjectDiscussions(self.client, self.project_key, "ARTICLE", self.article_id) |
131 | 191 |
|
| 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