94 lines
2.2 KiB
Python
94 lines
2.2 KiB
Python
from django.conf import settings
|
|
|
|
import os
|
|
import urllib.parse
|
|
|
|
from etherpad_lite import EtherpadLiteClient, EtherpadException
|
|
|
|
SERVER_URL = settings.ETHERPAD_CLIENT["exturl"]
|
|
|
|
|
|
def get_ep_client():
|
|
epc = None
|
|
group = None
|
|
|
|
try:
|
|
with open(os.path.abspath(settings.ETHERPAD_CLIENT["apikey"]), "r") as f:
|
|
apikey = f.read()
|
|
epc = EtherpadLiteClient(
|
|
base_params={'apikey': apikey, },
|
|
base_url=urllib.parse.urljoin(settings.ETHERPAD_CLIENT["url"], "api"),
|
|
api_version='1.2.14',
|
|
)
|
|
group = epc.createGroupIfNotExistsFor(groupMapper="fet")
|
|
except Exception as e:
|
|
raise e
|
|
|
|
return epc, group
|
|
|
|
|
|
def __checkPadExists(padID=None):
|
|
if not padID:
|
|
return False
|
|
|
|
epc, group = get_ep_client()
|
|
if not epc:
|
|
return None
|
|
|
|
try:
|
|
lists = epc.listAllPads()
|
|
except Exception as e:
|
|
raise e
|
|
else:
|
|
string = group["groupID"] + "$" + str(padID)
|
|
if string in lists["padIDs"]:
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def createPadifNotExists(padID):
|
|
epc, group = get_ep_client()
|
|
if not epc:
|
|
return None
|
|
|
|
# Pad doesn't exist
|
|
if not __checkPadExists(padID=padID):
|
|
try:
|
|
epc.createGroupPad(groupID=group["groupID"], padName=padID, text="helloworld")
|
|
except EtherpadException as e:
|
|
# TODO: change it after Etherpad server is a better one than that because of http 500
|
|
print(e)
|
|
except Exception as e:
|
|
raise e
|
|
return padID
|
|
|
|
|
|
def getReadOnlyID(padID):
|
|
epc, group = get_ep_client()
|
|
url = epc.getReadOnlyID(padID=group["groupID"] + "$" + padID)['readOnlyID']
|
|
return url
|
|
|
|
|
|
def getPadHTML(padID):
|
|
epc, group = get_ep_client()
|
|
text = epc.getHTML(padID=group["groupID"] + "$" + padID)["html"]
|
|
return text
|
|
|
|
|
|
def setPadHTML(padID, html):
|
|
epc, group = get_ep_client()
|
|
epc.setHTML(padID=group["groupID"] + "$" + padID, html=html)
|
|
return html
|
|
|
|
|
|
def get_pad_link(padID):
|
|
if padID is None:
|
|
return "#"
|
|
|
|
epc, group = get_ep_client()
|
|
|
|
if epc:
|
|
return urllib.parse.urljoin(SERVER_URL, 'p/' + group["groupID"] + '$' + str(padID))
|
|
return ""
|