piki/pages/views.py

114 lines
3.9 KiB
Python
Raw Normal View History

2024-10-08 22:46:18 +02:00
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import gettext as _
import logging
2024-10-05 16:22:40 +02:00
from . import access
from . import messages
from . import url_page
2024-10-05 16:22:40 +02:00
import config
from .context import context_adaption
2024-10-05 16:22:40 +02:00
from .forms import EditForm
from .help import help_pages
2024-10-05 16:22:40 +02:00
import mycreole
from .page import creol_page
from themes import Context
2024-10-08 22:46:18 +02:00
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
def root(request):
return HttpResponseRedirect(url_page(request, config.STARTPAGE))
2024-10-05 16:22:40 +02:00
def page(request, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
#
p = creol_page(request, rel_path)
2024-10-05 16:22:40 +02:00
if access.read_page(request, rel_path):
page_content = p.render_to_html()
2024-10-05 16:22:40 +02:00
else:
messages.permission_denied_msg_page(request, rel_path)
page_content = ""
#
context_adaption(
context,
request,
2024-10-05 16:22:40 +02:00
rel_path=rel_path,
title=p.title,
upload_path=p.attachment_path,
2024-10-05 16:22:40 +02:00
page_content=page_content
)
return render(request, 'pages/page.html', context=context)
2024-10-05 16:22:40 +02:00
def edit(request, rel_path):
if access.write_page(request, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
#
p = creol_page(request, rel_path)
2024-10-05 16:22:40 +02:00
#
if not request.POST:
form = EditForm(page_data=p.raw_page_src)
#
context_adaption(
context,
request,
form=form,
# TODO: Add translation
title=_("Edit page %s") % repr(p.title),
upload_path=p.attachment_path,
)
return render(request, 'pages/page_form.html', context=context)
else:
save = request.POST.get("save")
page_txt = request.POST.get("page_txt")
preview = request.POST.get("preview")
#
if save is not None:
p.update_page(page_txt)
return HttpResponseRedirect(url_page(request, rel_path))
elif preview is not None:
form = EditForm(page_data=page_txt)
#
context_adaption(
context,
request,
form=form,
# TODO: Add translation
title=_("Edit page %s") % repr(p.title),
upload_path=p.attachment_path,
2024-10-05 20:20:28 +02:00
page_content=p.render_text(request, page_txt)
2024-10-05 16:22:40 +02:00
)
return render(request, 'pages/page_form.html', context=context)
else:
return HttpResponseRedirect(url_page(request, rel_path))
else:
messages.permission_denied_msg_page(request, rel_path)
return HttpResponseRedirect(url_page(request, rel_path))
def search(request):
context = Context(request) # needs to be executed first because of time mesurement
context_adaption(
context,
request,
page_content="Search is not yet implemented..."
)
return render(request, 'pages/page.html', context=context)
def helpview(request, page='main'):
context = Context(request) # needs to be executed first because of time mesurement
page_content = help_pages[page]
context_adaption(
context, # the base context
request, # the request object to be used in context_adaption
current_help_page=page, # the current help_page to identify which taskbar entry has to be highlighted
page_content=page_content, # the help content itself (template)
title=_('Help') # the title for the page (template)
)
return render(request, 'pages/page.html', context=context)