Unverified Commit 590d8030 authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Use etherpad API for pad content push/pull

parent b4860e32
...@@ -376,7 +376,7 @@ def check_printing(PRINTING_SERVER, PRINTING_USER, PRINTING_PRINTERS): ...@@ -376,7 +376,7 @@ def check_printing(PRINTING_SERVER, PRINTING_USER, PRINTING_PRINTERS):
printer, value, name, available_options[name])) printer, value, name, available_options[name]))
def check_etherpad(ETHERPAD_URL, EMPTY_ETHERPAD): def check_etherpad(ETHERPAD_URL, ETHERPAD_APIKEY, EMPTY_ETHERPAD):
import requests import requests
try: try:
answer = requests.get(ETHERPAD_URL) answer = requests.get(ETHERPAD_URL)
...@@ -822,6 +822,11 @@ CONFIG_SECTIONS = [ ...@@ -822,6 +822,11 @@ CONFIG_SECTIONS = [
description=( description=(
"URL of the etherpad installation. " "URL of the etherpad installation. "
"Do not include the '/p'!")), "Do not include the '/p'!")),
ConfigEntry(
name="ETHERPAD_APIKEY",
default="abc123",
required=True, internal=True, no_sane_default=True,
description="Key to access the etherpad API"),
ConfigEntry( ConfigEntry(
name="EMPTY_ETHERPAD", name="EMPTY_ETHERPAD",
default="\n".join([ default="\n".join([
......
...@@ -16,6 +16,8 @@ from uuid import uuid4 ...@@ -16,6 +16,8 @@ from uuid import uuid4
import subprocess import subprocess
import contextlib import contextlib
from etherpad_lite import EtherpadLiteClient as EtherpadClient
from shared import config from shared import config
...@@ -136,31 +138,29 @@ def get_etherpad_url(pad): ...@@ -136,31 +138,29 @@ def get_etherpad_url(pad):
return "{}/p/{}".format(config.ETHERPAD_URL, normalize_pad(pad)) return "{}/p/{}".format(config.ETHERPAD_URL, normalize_pad(pad))
def get_etherpad_export_url(pad): def get_etherpad_api_url():
return "{}/p/{}/export/txt".format(config.ETHERPAD_URL, normalize_pad(pad)) return "{}/api".format(config.ETHERPAD_URL)
def get_etherpad_import_url(pad): def get_etherpad_api_client():
return "{}/p/{}/import".format(config.ETHERPAD_URL, normalize_pad(pad)) return EtherpadClient(base_url=get_etherpad_api_url(), api_version="1.2.14", base_params=dict(apikey=config.ETHERPAD_APIKEY))
def get_etherpad_text(pad): def get_etherpad_text(pad):
req = requests.get(get_etherpad_export_url(pad)) client = get_etherpad_api_client()
return req.text return client.getText(padID=pad)["text"]
def set_etherpad_text(pad, text, only_if_default=True): def set_etherpad_text(pad, text, only_if_default=True):
client = get_etherpad_api_client()
if only_if_default: if only_if_default:
current_text = get_etherpad_text(pad).strip() current_text = get_etherpad_text(pad).strip()
if (current_text != config.EMPTY_ETHERPAD.strip() if (current_text != config.EMPTY_ETHERPAD.strip()
and "<pre>Cannot GET /p" not in current_text and "<pre>Cannot GET /p" not in current_text
and len(current_text) > 0): and len(current_text) > 0):
return False return False
file_like = BytesIO(text.encode("utf-8")) client = get_etherpad_api_client()
files = {"file": ("content.txt", file_like, "text/plain")} client.setText(padID=pad, text=text)
url = get_etherpad_import_url(pad) return True
req = requests.post(url, files=files)
return req.status_code == 200
def split_terms(text, quote_chars="\"'", separators=" \t\n"): def split_terms(text, quote_chars="\"'", separators=" \t\n"):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment