93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
import logging
|
|
|
|
from django.contrib import messages
|
|
from django.db.models import F, Q
|
|
from django.http import HttpResponseRedirect
|
|
from django.shortcuts import render
|
|
from documents.api import get_pad_link
|
|
from documents.etherpadlib import add_ep_cookie
|
|
from collections import deque
|
|
|
|
from .forms import DocumentForm
|
|
from .models import TopicGroup, Topic, Documentation, Document
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def index(request):
|
|
topic = deque(Topic.objects.filter(archive=False).order_by(F('topic_group__order').asc(nulls_last=True), 'topic_group', 'title'))
|
|
|
|
context = {
|
|
"topic": topic,
|
|
}
|
|
|
|
return render(request, "intern/index.html", context)
|
|
|
|
|
|
def show_topic(request, slug=None):
|
|
active_topic = Topic.objects.filter(slug=slug).first()
|
|
docu = deque(Documentation.objects.filter(topic__slug=slug).order_by('title'))
|
|
|
|
context = {
|
|
"active_topic": active_topic,
|
|
"docus": docu,
|
|
}
|
|
|
|
return render(request, "intern/topic.html", context)
|
|
|
|
|
|
def show_docu(request, topic_slug=None, slug=None):
|
|
active_docu = Documentation.objects.filter(Q(topic__slug=topic_slug) & Q(slug=slug)).first()
|
|
active_topic = Topic.objects.filter(slug=topic_slug).first()
|
|
|
|
if request.method == "POST":
|
|
if "btn_input" in request.POST:
|
|
form = DocumentForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
docu = form.save(commit=False)
|
|
docu.created_by = request.user
|
|
docu.documentation = active_docu
|
|
docu.save()
|
|
|
|
return HttpResponseRedirect(request.path)
|
|
|
|
else:
|
|
for elem in list(form.errors.values()):
|
|
messages.info(request, '; '.join(elem))
|
|
|
|
initial = {
|
|
"title": active_docu.placeholder,
|
|
}
|
|
|
|
form = DocumentForm(initial=initial)
|
|
|
|
docus = deque(Document.objects.filter(documentation=active_docu).order_by('-date'))
|
|
documents = deque([])
|
|
|
|
# list of etherpad url-link of any documents
|
|
for elem in docus:
|
|
documents.append(
|
|
{
|
|
"title": elem.title,
|
|
"date": elem.date,
|
|
"etherpad_key": get_pad_link(elem.etherpad_key),
|
|
}
|
|
)
|
|
|
|
context = {
|
|
"formset": form,
|
|
"active_topic": active_topic,
|
|
"active_docu": active_docu,
|
|
"documents": documents,
|
|
}
|
|
|
|
response = render(request, "intern/docu.html", context)
|
|
|
|
try:
|
|
response = add_ep_cookie(request, response)
|
|
except Exception as e:
|
|
logger.info("Etherpad Server doesn't work. Error: %s", e)
|
|
|
|
return response
|