This commit is contained in:
2021-02-08 18:08:22 +00:00
6 changed files with 75 additions and 59 deletions

View File

@@ -0,0 +1 @@
from .api import getPadHTML, setPadHTML, createPadifNotExists

View File

@@ -5,18 +5,29 @@ import urllib.parse
from etherpad_lite import EtherpadLiteClient, EtherpadException
from contextlib import contextmanager
import logging
logger = logging.getLogger(__name__)
SERVER_URL = settings.ETHERPAD_CLIENT["exturl"]
@contextmanager
def ignore_ep_exception(msg="", *exceptions):
try:
yield
except exceptions as e:
logger.error(msg)
logger.error("%s", e)
def get_ep_client():
def get_ep_client(groupName="fet"):
epc = None
group = None
try:
with ignore_ep_exception("Error connecting to Etherpad", Exception):
with open(os.path.abspath(settings.ETHERPAD_CLIENT["apikey"]), "r") as f:
apikey = f.read()
apikey = apikey.rstrip()
@@ -27,73 +38,52 @@ def get_ep_client():
base_url=urllib.parse.urljoin(settings.ETHERPAD_CLIENT["url"], "api"),
api_version="1.2.14",
)
group = epc.createGroupIfNotExistsFor(groupMapper="fet")
except Exception as e:
logger.info("Can't get connection to Etherpad Server. Error: {}".format(e))
raise e
return None, None
group = epc.createGroupIfNotExistsFor(groupMapper=groupName)
return epc, group
@contextmanager
def ep_client(groupName="fet"):
epc, group = get_ep_client(groupName)
if not epc or not group:
yield None, None
else:
with ignore_ep_exception("EtherpadException!!", EtherpadException):
yield epc, group
def __checkPadExists(padID=None):
if not padID:
return False
epc, group = get_ep_client()
with ep_client() as (epc, group):
if not epc or not group:
return None
try:
lists = epc.listAllPads()
except Exception as e:
raise e
else:
string = group["groupID"] + "$" + str(padID)
if string in lists["padIDs"]:
lists = epc.listPads(groupID=group["groupID"])
if str(padID) in lists["padIDs"]:
return True
return False
def createPadifNotExists(padID):
epc, group = get_ep_client()
if not epc:
logger.info("if not epc")
return None
# Pad doesn't exist
if not __checkPadExists(padID=padID):
try:
with ep_client() as (epc, group):
if not epc or not group:
return None
epc.createGroupPad(
groupID = group["groupID"], padName=padID, text="helloworld"
)
except EtherpadException as e:
logger.info("Can't create Pad '{}'. EtherpadException: {}".format(padID, e))
return None
except Exception as e:
raise e
return None
return padID
def getPadHTML(padID):
epc, group = get_ep_client()
text = None
with ep_client() as (epc, group):
if not epc or not group:
return None
try:
text = epc.getHTML(padID = group["groupID"] + "$" + padID)["html"]
except EtherpadException as e:
logger.info(
"Can't get HTML from padID '{}'. EtherpadException: {}".format(padID, e)
)
return None
except Exception as e:
raise e
return None
return text
@@ -109,11 +99,9 @@ def setPadHTML(padID, html):
def get_pad_link(padID):
if padID is None:
return "#"
epc, group = get_ep_client()
with ep_client() as (epc, group):
if not epc or not group:
return "#"
return urllib.parse.urljoin(
settings.ETHERPAD_CLIENT["exturl"], "p/" + group["groupID"] + "$" + str(padID)
)

View File

@@ -12,7 +12,7 @@ env = environ.Env(
MYSQL_PASSWORD=(str),
HOST_NAME=(str, "localhost"),
ETHERPAD_PORT=(str, "9001"),
ETHERPAD_HOST=(str, "etherpad2.2020.fet.at"),
ETHERPAD_HOST=(str, ""),
)
# Prints and logs are written to console
@@ -217,12 +217,16 @@ THUMBNAIL_ALIASES = {
"portrait": {"size": (200, 300), "crop": False},
},
}
ETHERPAD_HOST = env("ETHERPAD_HOST").strip()
if not ETHERPAD_HOST or ETHERPAD_HOST=="":
ETHERPAD_HOST = urljoin("https://" + env("HOST_NAME"), "etherpad/")
# ETHERPAD CLIENT
if DEBUG:
ETHERPAD_CLIENT = {
"url": "http://etherpad:" + env("ETHERPAD_PORT"),
"exturl": env("ETHERPAD_HOST"),
"exturl": ETHERPAD_HOST,
"apikey": "/srv/etherpad/APIKEY.txt",
}
else:

View File

@@ -10,7 +10,7 @@ from django.utils import timezone
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from taggit.managers import TaggableManager
from documents.api import getPadHTML, setPadHTML, createPadifNotExists
from documents import getPadHTML, setPadHTML, createPadifNotExists
from .managers import (
PostManager,
ArticleManager,

View File

@@ -67,7 +67,7 @@
<hr>
{% endif %}
{% if post.body %}
{% if post.body and post.body != "None" %}
{{ post.body|safe|add_internal_links|tags_to_url }}
<hr>
{% endif %}

View File

@@ -0,0 +1,23 @@
import pytest
from django.urls import reverse
from django.core.validators import ValidationError
import datetime
from documents.api import ep_client, createPadifNotExists
class TestEtherpad:
def test(self):
with ep_client() as (epc, group):
if not epc:
return
pds = epc.listPads(groupID=str(group["groupID"]))
assert isinstance(pds, dict)
def test_listpads(self):
pds = None
with ep_client() as (epc, group):
pds = epc.listPads(groupID=group["groupID"])
assert isinstance(pds, dict)
def test_padexists(self):
assert createPadifNotExists("sdf")