wiki trial
This commit is contained in:
7
wiki/__init__.py
Normal file
7
wiki/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from misc import SaveFileMapping, SaveFileObject
|
||||
|
||||
class Page(SaveFileObject):
|
||||
pass
|
||||
|
||||
class PageManager(SaveFileMapping):
|
||||
pass
|
||||
101
wiki/api.py
Normal file
101
wiki/api.py
Normal file
@@ -0,0 +1,101 @@
|
||||
import os
|
||||
import urllib.parse
|
||||
from etherpad_lite import EtherpadLiteClient, EtherpadException
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
#SERVER_URL = settings.ETHERPAD_CLIENT["exturl"]
|
||||
logger.info("loading api.py")
|
||||
class EtherpadLiteLazyAPI():
|
||||
"""This is a small wrapper for EtherpadLiteClient
|
||||
url is the url with port the client should connect to.
|
||||
keyfile is the file path to the APIKEY
|
||||
"""
|
||||
def __init__(self,url="http://etherpad", exturl="https://bot.2020.fet.at", keyfile=None, groupmapper="mygroup"):
|
||||
self.url=url
|
||||
self.keyfile=keyfile
|
||||
self.groupmapper=groupmapper
|
||||
self.epc=None
|
||||
self.group=None
|
||||
self.connect()
|
||||
self.exturl=exturl
|
||||
#self.epc, self.group = self.get_ep_client()
|
||||
|
||||
@property
|
||||
def isConnected(self):
|
||||
if self.epc and self.group: return True
|
||||
return False
|
||||
|
||||
def connect(self):
|
||||
""" Try to establish the connection.
|
||||
This sets the internal variables self.epc = Etherpad Client
|
||||
and self.group which represents the group this instance is associated with
|
||||
"""
|
||||
if self.epc and self.group: return True # if connection exists return
|
||||
logger.info("connecting")
|
||||
try:
|
||||
with open(os.path.abspath(self.keyfile), "r") as f:
|
||||
apikey = f.read().rstrip()
|
||||
epc = EtherpadLiteClient(
|
||||
base_params={'apikey': apikey, },
|
||||
base_url=urllib.parse.urljoin(self.url, "api"),
|
||||
api_version='1.2.14',
|
||||
)
|
||||
group = epc.createGroupIfNotExistsFor(groupMapper=self.groupmapper)
|
||||
except Exception as e:
|
||||
logger.error(f"Can't get connection to Etherpad Server. Error: {e}")
|
||||
return None
|
||||
self.epc=epc
|
||||
self.group= group
|
||||
return True
|
||||
|
||||
|
||||
def padExists(self, padID=None):
|
||||
if not padID: return False # padID needed
|
||||
if not self.connect(): return None
|
||||
try:
|
||||
lists = self.epc.listAllPads()
|
||||
except Exception as e:
|
||||
raise e
|
||||
return None
|
||||
string = self.group["groupID"] + "$" + str(padID)
|
||||
if string in lists["padIDs"]: return True
|
||||
return False
|
||||
|
||||
def createPadifNotExists(self,padID):
|
||||
if not self.connect(): return None
|
||||
# Pad doesn't exist
|
||||
if not self.padExists(padID=padID):
|
||||
try:
|
||||
self.epc.createGroupPad(groupID=self.group["groupID"], padName=padID, text="helloworld")
|
||||
except EtherpadException as e:
|
||||
logger.error("Can't create Pad '{}'. EtherpadException: {}".format(padID, e))
|
||||
return None
|
||||
except Exception as e:
|
||||
raise e
|
||||
return None
|
||||
return padID
|
||||
|
||||
def getPadHTML(self,padID):
|
||||
if not self.connect():
|
||||
return None
|
||||
try:
|
||||
text = self.epc.getHTML(padID=self.group["groupID"] + "$" + padID)["html"]
|
||||
except EtherpadException as e:
|
||||
logger.error("Can't get HTML from padID '{}'. EtherpadException: {}".format(padID, e))
|
||||
return None
|
||||
except Exception as e:
|
||||
raise e
|
||||
return None
|
||||
return text
|
||||
|
||||
|
||||
def setPadHTML(self,padID, html):
|
||||
if not self.connect(): return None
|
||||
self.epc.setHTML(padID=self.group["groupID"] + "$" + padID, html=html)
|
||||
return html
|
||||
|
||||
def getPadLink(self,padID):
|
||||
if padID is None: return "#"
|
||||
if not self.connect(): return "#"
|
||||
return urllib.parse.urljoin(self.exturl, 'p/' + self.group["groupID"] + '$' + str(padID))
|
||||
65
wiki/etherpadlib.py
Normal file
65
wiki/etherpadlib.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from .api import get_ep_client
|
||||
from authentications.decorators import ep_authenticated_user
|
||||
|
||||
|
||||
@ep_authenticated_user
|
||||
def __get_ep_sessionid(request):
|
||||
epc, group = get_ep_client()
|
||||
if not epc or not group:
|
||||
return None, None
|
||||
|
||||
author = epc.createAuthorIfNotExistsFor(
|
||||
name=str(request.user),
|
||||
authorMapper=str(request.user)
|
||||
)['authorID']
|
||||
|
||||
expires = datetime.utcnow() + timedelta(hours=3)
|
||||
try:
|
||||
result = epc.createSession(
|
||||
groupID=str(group['groupID']),
|
||||
authorID=str(author),
|
||||
validUntil=str(int(expires.timestamp()))
|
||||
)
|
||||
except Exception as e:
|
||||
raise e
|
||||
return None, None
|
||||
|
||||
return result['sessionID'], expires
|
||||
|
||||
|
||||
def add_ep_cookie(request, response):
|
||||
ep_sessid, expires = __get_ep_sessionid(request)
|
||||
|
||||
if ep_sessid:
|
||||
response.set_cookie(
|
||||
"sessionID",
|
||||
ep_sessid,
|
||||
expires=expires,
|
||||
domain=".2020.fet.at",
|
||||
path="/"
|
||||
)
|
||||
response.set_cookie(
|
||||
"sessionID",
|
||||
ep_sessid,
|
||||
expires=expires,
|
||||
path="/etherpad"
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def del_ep_cookie(request, response):
|
||||
|
||||
if 'sessionID' in request.COOKIES:
|
||||
ep_sessionID = request.COOKIES['sessionID']
|
||||
|
||||
epc, group = get_ep_client()
|
||||
epc.deleteSession(sessionID=ep_sessionID)
|
||||
|
||||
response.delete_cookie(
|
||||
"sessionID",
|
||||
domain=".2020.fet.at",
|
||||
path="/"
|
||||
)
|
||||
return response
|
||||
Reference in New Issue
Block a user