From b1d66f527869236b877207fb617c2660ef68e95e Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Fri, 25 Sep 2020 20:28:58 -0500 Subject: Add edit page command --- .gitignore | 1 + custom_requests.py | 30 ++++++++++++++++++++++++++++ graphql_queries.py | 20 +++++++++++++------ graphql_requests.py | 31 ----------------------------- main.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++------- 5 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 custom_requests.py delete mode 100644 graphql_requests.py diff --git a/.gitignore b/.gitignore index 2380aaa..a254dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ config.ini *.swp + diff --git a/custom_requests.py b/custom_requests.py new file mode 100644 index 0000000..65673cf --- /dev/null +++ b/custom_requests.py @@ -0,0 +1,30 @@ +import requests +import sys +import json +from config import config + +def handle_errors(r): + error = False + if r.status_code != 200: + error = True + print("Error status code: %s" % r.status_code) + json = r.json() + if "errors" in json: + error = True + for e in json["errors"]: + print(e["message"]) + print(e) + if error: + sys.exit(1) + + +def get_headers(): + return { "Authorization": "Bearer %s" % config["wiki"]["key"] } + +def send_query(query, query_vars): + '''Returns status code, json''' + payload = { "query": query, "variables": query_vars} + r = requests.post(config["wiki"]["url"], json=payload, headers = get_headers()) + handle_errors(r) + return r.json() + diff --git a/graphql_queries.py b/graphql_queries.py index 92e3734..6d7094f 100644 --- a/graphql_queries.py +++ b/graphql_queries.py @@ -1,13 +1,21 @@ -import graphql_requests +import custom_requests def get_single_page(page_id): - query = 'query {\npages {\nsingle (id: %s) {\nid\npath\ntitle\ncontent\n}\n}\n}' % page_id - return graphql_requests.send_query(query) + query = 'query ($id: Int!) {\npages {\nsingle (id: $id) {\nid\npath\ntitle\ncontent\n}\n}\n}' + query_vars = {"id": page_id} + return custom_requests.send_query(query, query_vars) def create_page(content, title, path): - query = 'mutation {\npages {\ncreate(\ncontent: "%s"\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: "%s"\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: "%s"\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' % ( content, path, title ) - return graphql_requests.send_query(query) + query = 'mutation ($content: String!, $path: String!, $title: String!) {\npages {\ncreate(\ncontent: $content\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: $path\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: $title\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' + query_vars = {"content": content, "title": title, "path": path} + return custom_requests.send_query(query, query_vars) def get_tree(): query = 'query {\n pages {\n list (orderBy: PATH) {\n id\npath\ntitle\n}\n}\n}' - return graphql_requests.send_query(query) + query_vars = { } + return custom_requests.send_query(query, query_vars) + +def edit_page(page_id, content, title, path): + query = 'mutation ($id: Int!, $content: String!, $path: String!, $title: String!){\npages {\nupdate(\nid: $id\ncontent: $content\ndescription: ""\neditor: "markdown"\nisPrivate: false\nisPublished: true\nlocale: "en"\npath: $path\npublishEndDate: ""\npublishStartDate: ""\nscriptCss: ""\nscriptJs: ""\ntags: []\ntitle: $title\n) {\nresponseResult {\nsucceeded\nerrorCode\nslug\nmessage\n__typename\n}\npage {\nid\nupdatedAt\n__typename\n}\n__typename\n}\n__typename\n}\n}' + query_vars = {"id": page_id, "content": content, "title": title, "path": path} + return custom_requests.send_query(query, query_vars) diff --git a/graphql_requests.py b/graphql_requests.py deleted file mode 100644 index f802cdd..0000000 --- a/graphql_requests.py +++ /dev/null @@ -1,31 +0,0 @@ -import requests -import sys -from config import config - -def handle_errors(r): - error = False - if r.status_code != 200: - error = True - print("Error status code: %s" % r.status_code) - json = r.json() - if "errors" in json: - error = True - for e in json["errors"]: - print(e["message"]) - if error: - sys.exit(1) - - -def get_headers(): - return { "Authorization": "Bearer %s" % config["wiki"]["key"] } - -def escape_query(query): - return query.replace('"', '\"') - -def send_query(query): - '''Returns status code, json''' - payload = { "query": query } - r = requests.post(config["wiki"]["url"], json=payload, headers = get_headers()) - handle_errors(r) - return r.json() - diff --git a/main.py b/main.py index 3f3f170..d78b26f 100755 --- a/main.py +++ b/main.py @@ -1,8 +1,9 @@ #!/usr/bin/python3 import sys - +import os +import subprocess from config import config -import graphql_queries, graphql_requests +import graphql_queries def print_item(item): trimmmed_path = item["path"][:17]+"..." if len(item["path"]) > 20 else item["path"] @@ -29,11 +30,10 @@ def tree(): continue print_item(item) -def single(): - if len(sys.argv) < 3: - print("Usage: ./main.py single ") - sys.exit(0) +def get_single_page(): if not sys.argv[2].isdigit(): + if sys.argv[2].startswith("/"): + sys.argv[2] = sys.argv[2][1:] found = False for item in graphql_queries.get_tree()["data"]["pages"]["list"]: if sys.argv[2] == item["path"]: @@ -44,12 +44,52 @@ def single(): sys.exit(0) page_id = int(sys.argv[2]) response = graphql_queries.get_single_page(page_id) - page = response["data"]["pages"]["single"] + return response["data"]["pages"]["single"] + +def single(): + if len(sys.argv) < 3: + print("Usage: ./main.py single ") + sys.exit(0) + page = get_single_page() print("-" * 80) print_item(page) print("-" * 80) print(page["content"]) +def edit(): + # Load content to edit + if len(sys.argv) < 3: + print("Usage: ./main.py edit ") + sys.exit(0) + page = get_single_page() + body = page["content"] + + # Open it in editor + if "VISUAL" in os.environ: + editor = os.environ['VISUAL'] + else: + editor = os.environ['EDITOR'] + edit_filename = "/tmp/wikijscmd-edit" + with open(edit_filename, "w") as f: + f.write(body) + subprocess.run([editor, edit_filename]) + + # Prompt user to save it to the wiki + with open(edit_filename, "r") as f: + new_body = f.read() + print_item(page) + print("-" * 80) + print(new_body) + print("-" * 80) + if input("Save changes? (y/n) ") == "y": + response = graphql_queries.edit_page(page["id"], new_body, page["title"], page["path"]) + result = response["data"]["pages"]["update"]["responseResult"] + if not result["succeeded"]: + print("Error!", result["message"]) + sys.exit(1) + print(result["message"]) + os.remove(edit_filename) + def main(): if len(sys.argv) < 2: print("Usage: ./main.py ") @@ -57,6 +97,7 @@ def main(): print("\tcreate <content>") print("\ttree <contains?>") print("\tsingle <id|path>") + print("\tedit <id|path>") sys.exit(0) if sys.argv[1] == "create": @@ -65,6 +106,8 @@ def main(): tree() elif sys.argv[1] == "single": single() + elif sys.argv[1] == "edit": + edit() else: print("Unknown command: %s" % sys.argv[1]) -- cgit v1.2.3