def EtherMap(self): epclient = EtherpadLiteClient({'apikey': self.server.apikey}, self.server.apiurl) result = epclient.createAuthorIfNotExistsFor( authorMapper=self.user.id.__str__(), name=self.__str__()) self.authorID = result['authorID'] return result
class Etherpad(object): def __init__(self): self.api = EtherpadLiteClient( api_version='1.2.1', base_url='{0}/api/'.format(settings.ETHERPAD_URL), base_params={'apikey': settings.ETHERPAD_API_KEY}) def _get_ep_user(self, user): ldap_user = user.get_ldapuser() return self.api.createAuthorIfNotExistsFor( authorMapper=user.id, name=ldap_user.display_name.encode('utf-8'), )['authorID'] def create_group_pad(self, group_name, pad_name): """ Creates a Pad for Group """ try: self.api.createGroupPad( groupID=self.get_group_id(group_name), padName=pad_name.encode('utf-8')) except EtherpadException as e: # test if pad was already created, if so it's ok if e.message == "padName does already exist": return raise def create_session(self, user, group_name): group = self.get_group_id(group_name) user_ep = self._get_ep_user(user) # first we delete old sessions activ_sessions = self.api.listSessionsOfGroup(groupID=group) if activ_sessions: for sessionID, data in activ_sessions.items(): if data['authorID'] == user_ep: if data['validUntil'] > time.time() + MIN_SESSION_REMAIN_TIME: # There is a valid session with over 6 hours # remaining time return else: # we delete the old Session so the user has not two # on the same group. (technickal no problem, # but the cookies will have more data self.api.deleteSession(sessionID=sessionID) # we create a new session self.api.createSession( groupID = group, authorID = user_ep, validUntil = time.time() + SESSION_DURATION) def get_session_cookie(self, user): sessions = self.api.listSessionsOfAuthor( authorID= self._get_ep_user(user) ) # We have to escape the comma with %2C because otherwise Django's # HttpResponse.set_cookie escapes it, but Etherpad can't read # the escaped value correctly. sessions_cookie = '%2C'.join(sessions.keys()) return sessions_cookie def get_group_id(self, group_name): return self.api.createGroupIfNotExistsFor( groupMapper=group_name.encode('utf-8') )["groupID"] def get_group_pads(self, group_name): try: return self.api.listPads(groupID=self.get_group_id(group_name))['padIDs'] except EtherpadException as e: if e.message == "groupID does not exist": # no pads for this group return [] raise def delete_pad(self, pad_name): try: self.api.deletePad(padID=pad_name.encode('utf-8')) except EtherpadException as e: if e.message == "padID does not exist": return raise def delete_group(self, group_name): self.api.deleteGroup(groupID=self.get_group_id(group_name)) def get_last_edit(self, pad_name): try: return float(self.api.getLastEdited(padID=pad_name.encode('utf-8'))['lastEdited']) / 1000 except EtherpadException as e: if e.message == "padID does not exist": return 0 raise except TypeError: # LastEdited is None return 0 def get_html(self, pad_name): try: return self.api.getHTML(padID=pad_name.encode('utf-8'))['html'] except EtherpadException as e: if e.message == "padID does not exist": return "" raise
class EtherpadEditor(AbstractEditor): def __init__(self, config): AbstractEditor.__init__(self, config) self.client = EtherpadLiteClient( base_params={'apikey':self.secret}, base_url=self.apiurl, api_version="1.2.12" ) def _get_authorid_for_user(self, user): name = user.username color = user.color if color is not None and len(color) > 0: color = '#' + color return self.client.createAuthorIfNotExistsFor(authorMapper=name, name=name, color=color)["authorID"] def _get_groupid_for_document(self, document): if isinstance(document, str): docname = document else: docname = document.name return self.client.createGroupIfNotExistsFor(groupMapper=docname)["groupID"] def _get_padid_for_document(self, document): group = self._get_groupid_for_document(document) return group + "$" + document.name def get_urlname_for_document(self, document): return self._get_padid_for_document(document) def generate_session(self, document, user): tomorrow = datetime.today() + timedelta(days=1) author = self._get_authorid_for_user(user) group = self._get_groupid_for_document(document) session = self.client.createSession(groupID=group, authorID=author, validUntil=tomorrow.timestamp())["sessionID"] return session def delete_session(self, sid): self.client.deleteSession(sessionID=sid) def delete_sessions(self, user): author = self._get_authorid_for_user(user) sessions = self.client.listSessionsOfAuthor(authorID=author) for sid in sessions: self.delete_session(sid) def create_document(self, document): group = self._get_groupid_for_document(document) try: self.client.createGroupPad(groupID=group, padName=document.name) except EtherpadException as e: if str(e) == 'padName does already exist': self.set_document_text(document, '') else: raise def delete_document(self, document): pad_id = self._get_padid_for_document(document) self.client.deletePad(padID=pad_id) def set_document_text(self, document, text): pad_id = self._get_padid_for_document(document) self.client.setText(padID=pad_id, text=text) def get_document_text(self, document): pad_id = self._get_padid_for_document(document) return self.client.getText(padID=pad_id)["text"]
class Etherpad(object): def __init__(self): self.api = EtherpadLiteClient( api_version='1.2.1', base_url='{0}/api'.format(settings.ETHERPAD_URL), base_params={'apikey': settings.ETHERPAD_API_KEY}) def _get_ep_user(self, user): ldap_user = user.get_ldapuser() return self.api.createAuthorIfNotExistsFor( authorMapper=str(user.id), name=ldap_user.display_name.encode('utf-8'), )['authorID'] def create_group_pad(self, group_name, pad_name): """ Creates a Pad for Group """ try: self.api.createGroupPad(groupID=self.get_group_id(group_name), padName=pad_name.encode('utf-8')) except EtherpadException as e: # test if pad was already created, if so it's ok if str(e) == "padName does already exist": return raise def create_session(self, user, group_name): group = self.get_group_id(group_name) user_ep = self._get_ep_user(user) # first we delete old sessions activ_sessions = self.api.listSessionsOfGroup(groupID=group) if activ_sessions: for sessionID, data in list(activ_sessions.items()): if data['authorID'] == user_ep: if data['validUntil'] > time.time( ) + MIN_SESSION_REMAIN_TIME: # There is a valid session with over 6 hours # remaining time return else: # we delete the old Session so the user has not two # on the same group. (technickal no problem, # but the cookies will have more data self.api.deleteSession(sessionID=sessionID) # we create a new session self.api.createSession(groupID=group, authorID=user_ep, validUntil=str(time.time() + SESSION_DURATION)) def get_session_cookie(self, user): sessions = self.api.listSessionsOfAuthor( authorID=self._get_ep_user(user)) # We have to escape the comma with %2C because otherwise Django's # HttpResponse.set_cookie escapes it, but Etherpad can't read # the escaped value correctly. sessions_cookie = '%2C'.join(list(sessions.keys())) return sessions_cookie def get_group_id(self, group_name): return self.api.createGroupIfNotExistsFor( groupMapper=group_name.encode('utf-8'))["groupID"] def get_group_pads(self, group_name): try: return self.api.listPads( groupID=self.get_group_id(group_name))['padIDs'] except EtherpadException as e: if str(e) == "groupID does not exist": # no pads for this group return [] raise def delete_pad(self, pad_name): try: self.api.deletePad(padID=pad_name.encode('utf-8')) except EtherpadException as e: if str(e) == "padID does not exist": return raise def delete_group(self, group_name): self.api.deleteGroup(groupID=self.get_group_id(group_name)) def get_last_edit(self, pad_name): try: return float( self.api.getLastEdited( padID=pad_name.encode('utf-8'))['lastEdited']) / 1000 except EtherpadException as e: if str(e) == "padID does not exist": return 0 raise except TypeError: # LastEdited is None return 0 def get_html(self, pad_name): try: return self.api.getHTML(padID=pad_name.encode('utf-8'))['html'] except EtherpadException as e: if str(e) == "padID does not exist": return "" raise
class EtherpadEditor(AbstractEditor): def __init__(self, config): AbstractEditor.__init__(self, config) self.client = EtherpadLiteClient(base_params={'apikey': self.secret}, base_url=self.apiurl, api_version="1.2.12") def _get_authorid_for_user(self, user): name = user.username color = user.color if color is not None and len(color) > 0: color = '#' + color return self.client.createAuthorIfNotExistsFor(authorMapper=name, name=name, color=color)["authorID"] def _get_groupid_for_document(self, document): if isinstance(document, str): docname = document else: docname = document.name return self.client.createGroupIfNotExistsFor( groupMapper=docname)["groupID"] def _get_padid_for_document(self, document): group = self._get_groupid_for_document(document) return group + "$" + document.name def get_urlname_for_document(self, document): return self._get_padid_for_document(document) def generate_session(self, document, user): tomorrow = datetime.today() + timedelta(days=1) author = self._get_authorid_for_user(user) group = self._get_groupid_for_document(document) session = self.client.createSession( groupID=group, authorID=author, validUntil=tomorrow.timestamp())["sessionID"] return session def delete_session(self, sid): self.client.deleteSession(sessionID=sid) def delete_sessions(self, user): author = self._get_authorid_for_user(user) sessions = self.client.listSessionsOfAuthor(authorID=author) for sid in sessions: self.delete_session(sid) def create_document(self, document): group = self._get_groupid_for_document(document) try: self.client.createGroupPad(groupID=group, padName=document.name) except EtherpadException as e: if str(e) == 'padName does already exist': self.set_document_text(document, '') else: raise def delete_document(self, document): pad_id = self._get_padid_for_document(document) self.client.deletePad(padID=pad_id) def set_document_text(self, document, text): pad_id = self._get_padid_for_document(document) self.client.setText(padID=pad_id, text=text) def get_document_text(self, document): pad_id = self._get_padid_for_document(document) return self.client.getText(padID=pad_id)["text"]