From 99e3034c0da888115a4b551e224eb6e54de9d8f1 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 22 Sep 2020 15:40:45 +0000 Subject: [PATCH 1/5] check if etherpad server works and agenda and protocol links are available. --- fet2020/posts/views.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index 4c65f118..06e45324 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -1,21 +1,21 @@ from django.shortcuts import render from django.http import HttpResponse, JsonResponse, HttpResponseServerError -from django_filters.rest_framework import DjangoFilterBackend -# from django.core.cache import cache from django.utils.text import slugify from django.utils import timezone -from taggit.models import Tag +from collections import deque +from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets -# from rest_framework import permissions +from taggit.models import Tag from .models import Post, PostSerializer, FetMeeting -from members.models import Member, JobMember -# from documents import add_ep_to_response, get_ep_sessionid2 from documents import get_pad_link from documents.etherpadlib import add_ep_cookie +from members.models import Member, JobMember -from collections import deque +import logging + +logger = logging.getLogger(__name__) ################## @@ -78,18 +78,34 @@ def show(request, id=None): if post_author: author_image = post_author.image['avatar'].url + ep_agenda_link = None + ep_protocol_link = None + + if p.has_agenda and p.agenda_key: + ep_agenda_link = get_pad_link(p.agenda_key) + + if p.has_protocol and p.protocol_key: + ep_protocol_link = get_pad_link(p.protocol_key) + context = { "post": p, "author_image": author_image, "next": get_next_dict(p), "related_posts": p.tags.similar_objects(), - "ep_agenda_link": get_pad_link(p.agenda_key), - "ep_protocol_link": get_pad_link(p.protocol_key), + "ep_agenda_link": ep_agenda_link, + "ep_protocol_link": ep_protocol_link, } response = render(request, 'posts/show.html', context) - return add_ep_cookie(request, response) + # check if etherpad server works + if ep_agenda_link or ep_protocol_link: + try: + response = add_ep_cookie(request, response) + except Exception as e: + logger.info("Etherpad Server doesn't work. Error: {}".format(e)) + + return response ########### From 526bfcd989151bf78ddd10674a079f054bada6d9 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 23 Sep 2020 16:47:44 +0000 Subject: [PATCH 2/5] add 'delete etherpad cookie', optimize the 'checkPadExists' function --- fet2020/authentications/views.py | 7 +- fet2020/documents/__init__.py | 19 ---- fet2020/documents/api.py | 158 +++++++++++++++---------------- fet2020/documents/etherpadlib.py | 75 ++++++++++----- fet2020/posts/models.py | 13 +-- fet2020/posts/views.py | 2 +- 6 files changed, 142 insertions(+), 132 deletions(-) diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py index 2e6c489c..0d9981b2 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -5,6 +5,7 @@ from django.contrib.auth.models import User from .authentications import authentication from .decorators import unauthenticated_user, authenticated_user +from documents.etherpadlib import del_ep_cookie @unauthenticated_user @@ -33,4 +34,8 @@ def loginPage(request): @authenticated_user def logoutUser(request): logout(request) - return redirect('home') + + response = redirect('home') + response = del_ep_cookie(request, response) + + return response diff --git a/fet2020/documents/__init__.py b/fet2020/documents/__init__.py index 94d1ce48..e69de29b 100644 --- a/fet2020/documents/__init__.py +++ b/fet2020/documents/__init__.py @@ -1,19 +0,0 @@ -# from etherpad_lite import EtherpadLiteClient -# from django.conf import settings -from datetime import datetime, timedelta -# from django.utils.text import slugify -import urllib.parse -from django.conf import settings -# import os -from .api import ep -# from .etherpadlib import get_ep_sessionid2, add_ep_cookie -SERVER_URL = settings.ETHERPAD_CLIENT["exturl"] -t = datetime.now() + timedelta(days=1) - - -def get_pad_link(padID): - if padID is None: - return "#" - if ep.get_epc(): - return urllib.parse.urljoin(SERVER_URL, 'p/' + ep.group["groupID"] + '$' + str(padID)) - return "" diff --git a/fet2020/documents/api.py b/fet2020/documents/api.py index ecba3259..942619c3 100644 --- a/fet2020/documents/api.py +++ b/fet2020/documents/api.py @@ -1,93 +1,93 @@ from django.conf import settings + import os import urllib.parse -from datetime import datetime, timedelta from etherpad_lite import EtherpadLiteClient, EtherpadException +SERVER_URL = settings.ETHERPAD_CLIENT["exturl"] -class ep_api(): - def __init__(self): - self.group = None - self.epc = None - return - def load(self): - try: - with open(os.path.abspath(settings.ETHERPAD_CLIENT["apikey"]), "r") as f: - k = f.read() - epc = EtherpadLiteClient( - base_params={'apikey': k, }, - base_url=urllib.parse.urljoin(settings.ETHERPAD_CLIENT["url"], "api") - ) - g = epc.createGroupIfNotExistsFor(groupMapper="fet") - self.group = g - self.epc = epc - except Exception as e: - raise e - self.group = None - self.epc = None +def get_ep_client(): + epc = None + group = None - def getPadHTML(self, padID): - self.get_epc() - text = self.epc.getHTML(padID=self.group["groupID"] + "$" + padID)["html"] - return text - - def setPadHTML(self, padID, html): - self.get_epc() - self.epc.setHTML(padID=self.group["groupID"] + "$" + padID, html=html) - return html - - def checkPadExists(self, padID=None): - print(self.epc.listPads(groupID=self.group["groupID"])) - if not padID: - return False - try: - self.epc.getRevisionsCount(padID=self.group["groupID"] + "$" + padID) - except EtherpadException as e: - print(e) - return False - return True - - def createPadifNotExists(self, padid): - self.get_epc() - if not self.epc: - return None - if not self.checkPadExists(padID=padid): # Pad doesn't exist - self.epc.createGroupPad(groupID=self.group["groupID"], padName=padid, text="helloworld") - return padid - - def get_epc(self): - if self.epc: - return self.epc - self.load() - return self.epc - - def get_ep_sessionid2(self, request): - if request.user is None: - return None, None - if not request.user.is_authenticated: - return None, None - # server = SERVER_URL - ep.get_epc() - author = self.epc.createAuthorIfNotExistsFor( - name=str(request.user), - authorMapper=str(request.user) - )['authorID'] - expires = datetime.utcnow() + timedelta( - hours=3 - ) - try: - result = self.epc.createSession( - groupID=str(self.group['groupID']), - authorID=str(author), - validUntil=str(int(expires.timestamp())) + 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 None, None - - return result['sessionID'], expires + return padID -ep = ep_api() +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 "" diff --git a/fet2020/documents/etherpadlib.py b/fet2020/documents/etherpadlib.py index 7f7fb709..b3f83bed 100644 --- a/fet2020/documents/etherpadlib.py +++ b/fet2020/documents/etherpadlib.py @@ -1,36 +1,59 @@ -# from etherpad_lite import EtherpadLiteClient +from datetime import datetime, timedelta -# from datetime import datetime, timedelta -# from django.utils.text import slugify -# import urllib.parse -# from django.conf import settings -from .api import ep +from .api import get_ep_client +from authentications.decorators import authenticated_user + + +@authenticated_user +def __get_ep_sessionid(request): + epc, group = get_ep_client() + + 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): - response.set_cookie("HILib", "TestWert", domain="https://andis.2020.fet.at") - ep_sessid, expires = ep.get_ep_sessionid2(request) - if ep_sessid is None: + ep_sessid, expires = __get_ep_sessionid(request) + + if ep_sessid: response.set_cookie( "sessionID", - "NOTAUTHENTICATED", + ep_sessid, expires=expires, domain=".2020.fet.at", - path="/", + path="/" ) - return response - response.set_cookie( - "sessionID", - ep_sessid, - expires=expires, - domain=".2020.fet.at", - path="/" - ) - response.set_cookie( - "sessionID", - ep_sessid, - expires=expires, - domain=".2020.fet.at", - path="/p" - ) + + 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 diff --git a/fet2020/posts/models.py b/fet2020/posts/models.py index 0304e802..e5d1fe34 100644 --- a/fet2020/posts/models.py +++ b/fet2020/posts/models.py @@ -6,7 +6,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 import ep +from documents.api import getPadHTML, setPadHTML, createPadifNotExists from .managers import ( PostManager, ArticleManager, NewsManager, AllEventManager, EventManager, FetMeetingManager @@ -102,13 +102,14 @@ class Post(models.Model): def agenda_html(self): if not self.agenda_key: return None - h = ep.getPadHTML(self.agenda_key) - return h + + html = getPadHTML(self.agenda_key) + return html @agenda_html.setter def agenda_html(self, value): request_logger.info("Accessing etherpad ...") - ep.setPadHTML(self.agenda_key, value) + setPadHTML(self.agenda_key, value) request_logger.info("set etherpad!") return value @@ -259,7 +260,7 @@ class FetMeeting(Event): def __get_agenda_key(self): try: - self.agenda_key = ep.createPadifNotExists(self.slug + "-agenda") + self.agenda_key = createPadifNotExists(self.slug + "-agenda") except Exception as e: raise ValidationError( _('Die Agenda konnte nicht erstellt werden. Error: %(error)s'), @@ -268,7 +269,7 @@ class FetMeeting(Event): def __get_protocol_key(self): try: - self.protocol_key = ep.createPadifNotExists(self.slug + "-protocol") + self.protocol_key = createPadifNotExists(self.slug + "-protocol") except Exception as e: raise ValidationError( _('Das Protokoll konnte nicht erstellt werden. Error: %(error)s'), diff --git a/fet2020/posts/views.py b/fet2020/posts/views.py index 06e45324..59c75942 100644 --- a/fet2020/posts/views.py +++ b/fet2020/posts/views.py @@ -9,7 +9,7 @@ from rest_framework import viewsets from taggit.models import Tag from .models import Post, PostSerializer, FetMeeting -from documents import get_pad_link +from documents.api import get_pad_link from documents.etherpadlib import add_ep_cookie from members.models import Member, JobMember From 864a72e4dcb7fecf7d7c1c4305871094ac5c97cf Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 23 Sep 2020 20:28:39 +0000 Subject: [PATCH 3/5] add discord widget --- fet2020/templates/home.html | 1 + 1 file changed, 1 insertion(+) diff --git a/fet2020/templates/home.html b/fet2020/templates/home.html index 07fffbcf..61749880 100644 --- a/fet2020/templates/home.html +++ b/fet2020/templates/home.html @@ -57,6 +57,7 @@ {% for post in events %} {% include 'posts/partials/_date_box.html' %} {% endfor %} + From 77abe560c676aeb7c0cc31a9433cafad15fc3457 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 24 Sep 2020 09:02:59 +0000 Subject: [PATCH 4/5] if subtitle is None, it will be replacedwith a no-break space. --- fet2020/templates/home.html | 17 ++- .../templates/posts/partials/_posts_hero.html | 7 +- fet2020/templates/posts/show.html | 111 +++++++++--------- 3 files changed, 64 insertions(+), 71 deletions(-) diff --git a/fet2020/templates/home.html b/fet2020/templates/home.html index 61749880..1ec98047 100644 --- a/fet2020/templates/home.html +++ b/fet2020/templates/home.html @@ -8,17 +8,17 @@
{% with post=featured_post %} - -
-

Neuigkeiten

{% with post=featured_post %} {% include 'posts/partials/_article_row.html' %} - {% endwith %} + {% endwith %} {% for post in featured_meeting %} {% include 'posts/partials/_meeting_row.html' %} - {% endfor %} + {% endfor %}
-
@@ -46,7 +44,6 @@ {{ tags_list|tags_to_url }} -
{% for post in posts %} diff --git a/fet2020/templates/posts/partials/_posts_hero.html b/fet2020/templates/posts/partials/_posts_hero.html index fa2d7843..54e98043 100644 --- a/fet2020/templates/posts/partials/_posts_hero.html +++ b/fet2020/templates/posts/partials/_posts_hero.html @@ -1,8 +1,9 @@ -
+
-

{{post.title}}

-

{{post.subtitle}}

{{post}} +

{{ post.title }}

+

{{ post.subtitle|default_if_none:" " }}

+ {{ post }}
\ No newline at end of file diff --git a/fet2020/templates/posts/show.html b/fet2020/templates/posts/show.html index 7c18c9de..4394a2f0 100644 --- a/fet2020/templates/posts/show.html +++ b/fet2020/templates/posts/show.html @@ -1,39 +1,34 @@ - {% extends "layout.html" %} {% load post_helpers %} {% load admin_urls %} {% block content %} -
+
-

{{post.title |tags_to_url}}

+

{{ post.title|tags_to_url }}

- {{post.subtitle |tags_to_url}} + {{ post.subtitle|default_if_none:" "|tags_to_url }} - {% if post.author %} - - {% else %} - - {% endif %} - -
+ {% if post.author %} + + {% else %} + + {% endif %} +
@@ -41,48 +36,48 @@
-

{{post.title | tags_to_url}}

- {{post.subtitle | tags_to_url}} +

{{ post.title|tags_to_url }}

+ {{ post.subtitle|default_if_none:" "|tags_to_url }} +
Nächster Artikel
- -
- {% include 'posts/partials/_date_box.html' %} +
+ {% include 'posts/partials/_date_box.html' %} +
+ + Start: {{ post.event_start }}
+ Ende: {{ post.event_end }}
+ + {% if post.has_agenda %} + Agenda
+ {% endif %} + + {% if post.has_protocol %} + Protokoll + {% endif %} + + {% if request.user.is_authenticated %} +
------
+ Bearbeiten + {% endif %} + +
+ {% for tag in post.get_tagnames %} + {{ tag|tags_to_url }} + {% endfor %} + +
+ {% if post.has_agenda %} +

Agenda

+ {{ post.agenda_html|safe }} + {% endif %} + + {{ post.body|safe|add_internal_links|tags_to_url }} +
- - Start: {{post.event_start}}
- Ende: {{post.event_end}}
- - {% if post.has_agenda %} - Agenda
- {% endif %} - - {% if post.has_protocol %} - Protokoll - {% endif %} - - {% if request.user.is_authenticated %} -
------
- Bearbeiten - {% endif %} - -
- {% for tag in post.get_tagnames %} - {{tag |tags_to_url}} - {% endfor %} - -
- {% if post.has_agenda %} -

Agenda

- {{post.agenda_html |safe}} - {% endif %} - - {{post.body | safe | add_internal_links | tags_to_url}} -
-
-
+
{% for post in related_posts %}
From 18126f263fb94b008bbb91bfb140d5ba95ee1345 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 24 Sep 2020 09:49:32 +0000 Subject: [PATCH 5/5] add initial #fachschaft when a new fet-meeting is created. --- fet2020/posts/forms.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fet2020/posts/forms.py b/fet2020/posts/forms.py index 3bee2b9a..7e0f0765 100644 --- a/fet2020/posts/forms.py +++ b/fet2020/posts/forms.py @@ -2,6 +2,7 @@ from django import forms from django.utils.translation import gettext_lazy as _ from ckeditor_uploader.widgets import CKEditorUploadingWidget +from taggit.models import Tag from .models import Post, Event, News, FetMeeting @@ -91,7 +92,6 @@ class MyFetMeetingForm(MyEventForm): # agenda_html = forms.CharField(widget = forms.TextInput()) class Meta: model = FetMeeting - fields = ['event_start', 'event_end', 'tags', 'has_agenda', 'has_protocol'] labels = { 'event_start': _('Start der Sitzung'), @@ -112,3 +112,8 @@ class MyFetMeetingForm(MyEventForm): self.fields['has_agenda'].initial = True self.fields['has_protocol'].initial = True + + tags = [] + tags.append(Tag()) + tags[0].name = "fachschaft" + self.fields['tags'].initial = tags