Merge branch 'master' of https://git.fet.at/bofh/fet2020
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
from .api import getPadHTML, setPadHTML, createPadifNotExists
|
||||||
|
|||||||
@@ -5,18 +5,29 @@ import urllib.parse
|
|||||||
|
|
||||||
from etherpad_lite import EtherpadLiteClient, EtherpadException
|
from etherpad_lite import EtherpadLiteClient, EtherpadException
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
SERVER_URL = settings.ETHERPAD_CLIENT["exturl"]
|
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
|
epc = None
|
||||||
group = 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:
|
with open(os.path.abspath(settings.ETHERPAD_CLIENT["apikey"]), "r") as f:
|
||||||
apikey = f.read()
|
apikey = f.read()
|
||||||
apikey = apikey.rstrip()
|
apikey = apikey.rstrip()
|
||||||
@@ -27,73 +38,52 @@ def get_ep_client():
|
|||||||
base_url=urllib.parse.urljoin(settings.ETHERPAD_CLIENT["url"], "api"),
|
base_url=urllib.parse.urljoin(settings.ETHERPAD_CLIENT["url"], "api"),
|
||||||
api_version="1.2.14",
|
api_version="1.2.14",
|
||||||
)
|
)
|
||||||
group = epc.createGroupIfNotExistsFor(groupMapper="fet")
|
group = epc.createGroupIfNotExistsFor(groupMapper=groupName)
|
||||||
except Exception as e:
|
|
||||||
logger.info("Can't get connection to Etherpad Server. Error: {}".format(e))
|
|
||||||
raise e
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
return epc, group
|
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):
|
def __checkPadExists(padID=None):
|
||||||
if not padID:
|
if not padID:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
epc, group = get_ep_client()
|
with ep_client() as (epc, group):
|
||||||
if not epc or not group:
|
if not epc or not group:
|
||||||
return None
|
return None
|
||||||
|
lists = epc.listPads(groupID=group["groupID"])
|
||||||
try:
|
if str(padID) in lists["padIDs"]:
|
||||||
lists = epc.listAllPads()
|
|
||||||
except Exception as e:
|
|
||||||
raise e
|
|
||||||
else:
|
|
||||||
string = group["groupID"] + "$" + str(padID)
|
|
||||||
if string in lists["padIDs"]:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def createPadifNotExists(padID):
|
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):
|
if not __checkPadExists(padID=padID):
|
||||||
try:
|
with ep_client() as (epc, group):
|
||||||
|
if not epc or not group:
|
||||||
|
return None
|
||||||
epc.createGroupPad(
|
epc.createGroupPad(
|
||||||
groupID = group["groupID"], padName=padID, text="helloworld"
|
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
|
return padID
|
||||||
|
|
||||||
|
|
||||||
def getPadHTML(padID):
|
def getPadHTML(padID):
|
||||||
epc, group = get_ep_client()
|
text = None
|
||||||
|
with ep_client() as (epc, group):
|
||||||
if not epc or not group:
|
if not epc or not group:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
|
||||||
text = epc.getHTML(padID = group["groupID"] + "$" + padID)["html"]
|
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
|
return text
|
||||||
|
|
||||||
|
|
||||||
@@ -109,11 +99,9 @@ def setPadHTML(padID, html):
|
|||||||
def get_pad_link(padID):
|
def get_pad_link(padID):
|
||||||
if padID is None:
|
if padID is None:
|
||||||
return "#"
|
return "#"
|
||||||
|
with ep_client() as (epc, group):
|
||||||
epc, group = get_ep_client()
|
|
||||||
if not epc or not group:
|
if not epc or not group:
|
||||||
return "#"
|
return "#"
|
||||||
|
|
||||||
return urllib.parse.urljoin(
|
return urllib.parse.urljoin(
|
||||||
settings.ETHERPAD_CLIENT["exturl"], "p/" + group["groupID"] + "$" + str(padID)
|
settings.ETHERPAD_CLIENT["exturl"], "p/" + group["groupID"] + "$" + str(padID)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ env = environ.Env(
|
|||||||
MYSQL_PASSWORD=(str),
|
MYSQL_PASSWORD=(str),
|
||||||
HOST_NAME=(str, "localhost"),
|
HOST_NAME=(str, "localhost"),
|
||||||
ETHERPAD_PORT=(str, "9001"),
|
ETHERPAD_PORT=(str, "9001"),
|
||||||
ETHERPAD_HOST=(str, "etherpad2.2020.fet.at"),
|
ETHERPAD_HOST=(str, ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prints and logs are written to console
|
# Prints and logs are written to console
|
||||||
@@ -217,12 +217,16 @@ THUMBNAIL_ALIASES = {
|
|||||||
"portrait": {"size": (200, 300), "crop": False},
|
"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
|
# ETHERPAD CLIENT
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
ETHERPAD_CLIENT = {
|
ETHERPAD_CLIENT = {
|
||||||
"url": "http://etherpad:" + env("ETHERPAD_PORT"),
|
"url": "http://etherpad:" + env("ETHERPAD_PORT"),
|
||||||
"exturl": env("ETHERPAD_HOST"),
|
"exturl": ETHERPAD_HOST,
|
||||||
"apikey": "/srv/etherpad/APIKEY.txt",
|
"apikey": "/srv/etherpad/APIKEY.txt",
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from django.utils import timezone
|
|||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
from documents.api import getPadHTML, setPadHTML, createPadifNotExists
|
from documents import getPadHTML, setPadHTML, createPadifNotExists
|
||||||
from .managers import (
|
from .managers import (
|
||||||
PostManager,
|
PostManager,
|
||||||
ArticleManager,
|
ArticleManager,
|
||||||
|
|||||||
@@ -67,7 +67,7 @@
|
|||||||
<hr>
|
<hr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if post.body %}
|
{% if post.body and post.body != "None" %}
|
||||||
{{ post.body|safe|add_internal_links|tags_to_url }}
|
{{ post.body|safe|add_internal_links|tags_to_url }}
|
||||||
<hr>
|
<hr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
23
fet2020/tests/test_etherpad.py
Normal file
23
fet2020/tests/test_etherpad.py
Normal 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")
|
||||||
Reference in New Issue
Block a user