|
14 | 14 | # limitations under the License.
|
15 | 15 | #
|
16 | 16 | """
|
| 17 | +unsupported.wiki |
| 18 | +~~~~~~~~~~~~~~~~ |
17 | 19 | WARNING: This module is not officially supported! Use at your own risk.
|
18 | 20 |
|
19 |
| -############################################################################ |
20 |
| -NAME: |
21 |
| -LabKey Collaboration API |
22 |
| -
|
23 |
| -SUMMARY: |
24 |
| -This module provides functions for interacting with Wikis on LabKey Server. |
25 |
| -
|
26 |
| -DESCRIPTION: |
27 |
| -This module is designed to simply programmatic editing of wikis on the LabKey Server |
28 |
| -
|
29 |
| -Documentation: |
30 |
| -LabKey Python API: |
31 |
| -https://www.labkey.org/wiki/home/Documentation/page.view?name=python |
32 |
| -
|
33 |
| -Setup, configuration of the LabKey Python API: |
34 |
| -https://www.labkey.org/wiki/home/Documentation/page.view?name=setupPython |
35 |
| -
|
36 |
| -Using the LabKey Python API: |
37 |
| -https://www.labkey.org/wiki/home/Documentation/page.view?name=usingPython |
38 |
| -
|
39 |
| -Documentation for the LabKey client APIs: |
40 |
| -https://www.labkey.org/wiki/home/Documentation/page.view?name=viewAPIs |
41 |
| -
|
42 |
| -Support questions should be directed to the LabKey forum: |
43 |
| -https://www.labkey.org/announcements/home/Server/Forum/list.view? |
44 |
| -
|
45 |
| -############################################################################ |
| 21 | +This module provides functions for interacting with Wiki's on the |
| 22 | +LabKey Server. |
46 | 23 | """
|
47 |
| - |
48 | 24 | import json
|
49 | 25 | from labkey.utils import build_url, handle_response
|
50 |
| -import requests |
51 | 26 | from requests.exceptions import SSLError
|
52 | 27 |
|
53 |
| -def updateWiki(server_context, wikiName, wikiBody, container_path=None): |
54 |
| - """ |
55 |
| -############################################################################ |
56 |
| -updateWiki() |
57 |
| -
|
58 |
| -updateWiki() can be used to update an existing wiki page |
59 |
| -
|
60 |
| -The following are the minimum required params: |
61 |
| -
|
62 |
| -myresults = labkeyApi.updateWiki( |
63 |
| - baseUrl = 'https://hosted.labkey.com', |
64 |
| - containerPath = 'PythonProject', |
65 |
| - wikiName = 'MyWiki', |
66 |
| - wikiBody = 'New Content for my wiki') |
67 | 28 |
|
68 |
| -The following are optional: |
69 |
| -
|
70 |
| - debug = True #will result in a more verbose output |
71 |
| -
|
72 |
| -
|
73 |
| -This API does not support the ability to change the Render Type for the wiki to be updated. |
74 |
| -
|
75 |
| -This API returns a dictionary containing the response from the server. The 'success' key |
76 |
| -in the dictionary will be true when the wiki was successfully updated. It will be false |
77 |
| -in the case of a failure. In the case of a failure, the 'error' key contains the error |
78 |
| -message returned by the server. |
79 |
| -
|
80 |
| ----------------------------------------------------------------------------- |
81 |
| -Test Code: |
82 |
| -
|
83 |
| -[NOT AVAILABLE] |
84 |
| -
|
85 |
| -############################################################################ |
86 |
| -""" |
87 |
| - |
| 29 | +def update_wiki(server_context, wiki_name, wiki_body, container_path=None): |
| 30 | + """ |
| 31 | + Used to update an existing wiki page |
| 32 | + :param server_context: A LabKey server context. See labkey.utils.create_server_context. |
| 33 | + :param wiki_name: The name of the wiki. |
| 34 | + :param wiki_body: The body of the wiki. |
| 35 | + :param container_path: Optional container path that can be used to override the server_context container path |
| 36 | + :return: returns a dictionary containing the response from the server. The 'success' key |
| 37 | + in the dictionary will be true when the wiki was successfully updated. It will be false |
| 38 | + in the case of a failure. In the case of a failure, the 'error' key contains the error |
| 39 | + message returned by the server. |
| 40 | + """ |
88 | 41 | # Build the URL for reading the wiki page
|
89 | 42 | read_wiki_url = build_url(server_context, 'wiki', 'editWiki.api', container_path=container_path)
|
90 |
| - payload = {'name': wikiName} |
| 43 | + payload = { |
| 44 | + 'name': wiki_name |
| 45 | + } |
91 | 46 | headers = {
|
92 | 47 | 'Content-type': 'application/json'
|
93 | 48 | }
|
94 | 49 |
|
95 |
| - data = None |
| 50 | + session = server_context['session'] |
96 | 51 |
|
97 | 52 | try:
|
98 |
| - read_response = requests.get(read_wiki_url, params=payload, headers=headers) # editWiki action only takes URL parameters, not JSON (JSON is not bound to form) |
| 53 | + read_response = session.get(read_wiki_url, params=payload, headers=headers) |
99 | 54 | except SSLError as e:
|
100 |
| - print("There was a problem while attempting to submit the read for the wiki page " + str(wikiName) + " via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode())) |
101 |
| - print("The HTTP client error was: "+ format(e)) |
102 |
| - return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
| 55 | + print("There was a problem while attempting to submit the read for the wiki page " + str(wiki_name) + " via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode())) |
| 56 | + print("The HTTP client error was: " + format(e)) |
| 57 | + return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
103 | 58 |
|
104 | 59 | data = read_response.text
|
105 | 60 |
|
106 | 61 | # Search HTML response for required information on wiki. This is stored in the javascript
|
107 | 62 | # variable named
|
108 | 63 | # - _wikiProps: for 14.3 and earlier
|
109 | 64 | # - LABKEY._wiki.setProps for 15.1 and later
|
110 |
| - dataList = data.split('\n') |
| 65 | + data_list = data.split('\n') |
111 | 66 |
|
112 | 67 | # If LabKey Server is v14.3 or earlier find line containing '_wikiProp'
|
113 |
| - v = next((i for i in range(len(dataList)) if '_wikiProp' in dataList[i]), None) |
| 68 | + v = next((i for i in range(len(data_list)) if '_wikiProp' in data_list[i]), None) |
114 | 69 |
|
115 | 70 | # If v = None, then server is running 15.1 or later and find the line
|
116 | 71 | # containing 'LABKEY._wiki.setProps'
|
117 |
| - if v == None: |
118 |
| - v = next((i for i in range(len(dataList)) if 'LABKEY._wiki.setProps' in dataList[i]), None) |
| 72 | + if v is None: |
| 73 | + v = next((i for i in range(len(data_list)) if 'LABKEY._wiki.setProps' in data_list[i]), None) |
119 | 74 |
|
120 | 75 | # Verify that we found the variable in the HTML response. If not
|
121 | 76 | # do not proceed
|
122 |
| - if v == None: |
123 |
| - print("There was a problem while attempting to read the data for the wiki page '" + str(wikiName) + "'.") |
| 77 | + if v is None: |
| 78 | + print("There was a problem while attempting to read the data for the wiki page '" + str(wiki_name) + "'.") |
124 | 79 | print("The script is unable to find the wiki properties in the HTML response")
|
125 |
| - return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
| 80 | + return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
126 | 81 |
|
127 |
| - wikiVars = {} |
| 82 | + wiki_vars = {} |
128 | 83 | for j in range(100):
|
129 | 84 | # Read each line, until find a javascript closing bracket.
|
130 |
| - if '};' in dataList[v+j+1]: |
| 85 | + if '};' in data_list[v+j+1]: |
131 | 86 | break
|
132 |
| - if '});' in dataList[v+j+1]: |
| 87 | + if '});' in data_list[v+j+1]: |
133 | 88 | break
|
134 |
| - wvar = dataList[v+j+1].rstrip().lstrip().replace('\'','').replace(',','') |
135 |
| - wikiVars[wvar.split(':')[0]] = wvar.split(':')[1] |
| 89 | + wvar = data_list[v+j+1].rstrip().lstrip().replace('\'', '').replace(',', '') |
| 90 | + wiki_vars[wvar.split(':')[0]] = wvar.split(':')[1] |
136 | 91 |
|
137 | 92 | # Build the URL for updating the wiki page
|
138 | 93 | update_wiki_url = build_url(server_context, 'wiki', 'saveWiki.api', container_path=container_path)
|
139 | 94 | headers = {
|
140 | 95 | 'Content-type': 'application/json'
|
141 | 96 | }
|
142 |
| - data = None |
143 | 97 |
|
144 |
| - # Update wikiVars to use the new wiki content. |
145 |
| - wikiVars['name'] = wikiName |
146 |
| - wikiVars['body'] = wikiBody |
| 98 | + # Update wiki_vars to use the new wiki content. |
| 99 | + wiki_vars['name'] = wiki_name |
| 100 | + wiki_vars['body'] = wiki_body |
147 | 101 |
|
148 | 102 | try:
|
149 |
| - response = requests.post(update_wiki_url, data=json.dumps(wikiVars, sort_keys=True), headers=headers) |
| 103 | + response = session.post(update_wiki_url, data=json.dumps(wiki_vars, sort_keys=True), headers=headers) |
150 | 104 | data = handle_response(response)
|
151 | 105 | except SSLError as e:
|
152 |
| - print("There was a problem while attempting to submit the read for the wiki page '" + str(wikiName) + "' via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode())) |
153 |
| - print("The HTTP client error was: "+ format(e)) |
154 |
| - return(1) # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
| 106 | + print("There was a problem while attempting to submit the read for the wiki page '" + str(wiki_name) + "' via the URL " + str(e.geturl()) + ". The HTTP response code was " + str(e.getcode())) |
| 107 | + print("The HTTP client error was: " + format(e)) |
| 108 | + return 1 # TODO: this is incorrect, should return 'success'/'error' properly like the docs say |
155 | 109 |
|
156 |
| - return(data) |
| 110 | + return data |
0 commit comments