piki/pages/views.py

242 lines
8.7 KiB
Python
Raw Normal View History

2024-10-08 22:46:18 +02:00
from django.conf import settings
from django.contrib import messages as django_messages
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-09 09:57:05 +02:00
from . import get_search_query
2024-10-05 16:22:40 +02:00
import config
from .context import context_adaption
2024-10-18 20:47:33 +02:00
from .forms import EditForm, RenameForm
from .help import help_pages
2024-10-05 16:22:40 +02:00
import mycreole
from .page import page_wrapped, page_list
2024-10-18 20:47:33 +02:00
from .search import whoosh_search, load_index, delete_item, add_item
from themes import Context
2024-10-08 22:46:18 +02:00
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
def root(request):
2024-10-16 07:09:06 +02:00
return HttpResponseRedirect(url_page(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
#
2024-10-09 13:07:38 +02:00
meta = "meta" in request.GET
2024-10-11 21:28:04 +02:00
history = request.GET.get("history")
if history:
history = int(history)
2024-10-09 13:07:38 +02:00
#
p = page_wrapped(request, rel_path, history_version=history)
2024-10-05 16:22:40 +02:00
if access.read_page(request, rel_path):
2024-10-09 13:07:38 +02:00
if meta:
page_content = p.render_meta()
else:
page_content = p.render_to_html()
2024-10-11 21:28:04 +02:00
if history:
messages.history_version_display(request, rel_path, history)
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-18 20:47:33 +02:00
page_content=page_content,
is_available=p.userpage_is_available()
)
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
#
if not request.POST:
2024-10-11 21:28:04 +02:00
history = request.GET.get("history")
if history:
history = int(history)
#
p = page_wrapped(request, rel_path, history_version=history)
2024-10-11 21:28:04 +02:00
#
form = EditForm(page_data=p.raw_page_src, page_tags=p.tags)
2024-10-05 16:22:40 +02:00
#
context_adaption(
context,
request,
2024-10-18 20:47:33 +02:00
rel_path=rel_path,
is_available=p.userpage_is_available(),
2024-10-05 16:22:40 +02:00
form=form,
# TODO: Add translation
title=_("Edit page %s") % repr(p.title),
upload_path=p.attachment_path,
)
2024-10-18 20:47:33 +02:00
return render(request, 'pages/page_edit.html', context=context)
2024-10-05 16:22:40 +02:00
else:
p = page_wrapped(request, rel_path)
2024-10-11 21:28:04 +02:00
#
2024-10-05 16:22:40 +02:00
save = request.POST.get("save")
page_txt = request.POST.get("page_txt")
2024-10-11 14:23:50 +02:00
tags = request.POST.get("page_tags")
2024-10-05 16:22:40 +02:00
preview = request.POST.get("preview")
#
if save is not None:
2024-10-11 14:23:50 +02:00
if p.update_page(page_txt, tags):
2024-10-09 16:25:24 +02:00
messages.edit_success(request)
else:
2024-10-18 20:47:33 +02:00
messages.no_change(request)
2024-10-16 07:09:06 +02:00
return HttpResponseRedirect(url_page(rel_path))
2024-10-05 16:22:40 +02:00
elif preview is not None:
2024-10-11 22:46:25 +02:00
form = EditForm(page_data=page_txt, page_tags=tags)
2024-10-05 16:22:40 +02:00
#
context_adaption(
context,
request,
2024-10-18 20:47:33 +02:00
rel_path=rel_path,
is_available=p.userpage_is_available(),
2024-10-05 16:22:40 +02:00
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
)
2024-10-18 20:47:33 +02:00
return render(request, 'pages/page_edit.html', context=context)
2024-10-05 16:22:40 +02:00
else:
2024-10-16 07:09:06 +02:00
return HttpResponseRedirect(url_page(rel_path))
2024-10-05 16:22:40 +02:00
else:
messages.permission_denied_msg_page(request, rel_path)
2024-10-16 07:09:06 +02:00
return HttpResponseRedirect(url_page(rel_path))
2024-10-05 16:22:40 +02:00
2024-10-18 20:47:33 +02:00
def delete(request, rel_path):
if access.write_page(request, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
#
if not request.POST:
p = page_wrapped(request, rel_path)
#
# form = DeleteForm(page_data=p.raw_page_src, page_tags=p.tags)
#
context_adaption(
context,
request,
rel_path=rel_path,
is_available=p.userpage_is_available(),
# form=form,
# TODO: Add translation
title=_("Delete page %s") % repr(p.title),
upload_path=p.attachment_path,
page_content=p.render_to_html(),
)
else:
p = page_wrapped(request, rel_path)
#
delete = request.POST.get("delete")
#
if delete:
# delete page from search index
ix = load_index()
delete_item(ix, p)
# delete move files to history
p.delete()
# add delete message
messages.page_deleted(request, p.title)
return HttpResponseRedirect("/")
else:
messages.operation_canceled(request)
return HttpResponseRedirect(url_page(rel_path))
return render(request, 'pages/page_delete.html', context=context)
else:
messages.permission_denied_msg_page(request, rel_path)
return HttpResponseRedirect(url_page(rel_path))
def rename(request, rel_path):
if access.write_page(request, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
#
if not request.POST:
p = page_wrapped(request, rel_path)
#
form = RenameForm(page_name=p.rel_path)
#
context_adaption(
context,
request,
rel_path=rel_path,
is_available=p.userpage_is_available(),
form=form,
# TODO: Add translation
title=_("Delete page %s") % repr(p.title),
upload_path=p.attachment_path,
page_content=p.render_to_html(),
)
else:
p = page_wrapped(request, rel_path)
#
rename = request.POST.get("rename")
page_name = request.POST.get("page_name")
if rename:
if page_name == p.rel_path:
messages.no_change(request)
else:
# delete page from search index
ix = load_index()
delete_item(ix, p)
# rename the storage folder
p.rename(page_name)
# add the renamed page to the search index
add_item(ix, p)
# add rename message
messages.page_renamed(request)
else:
messages.operation_canceled(request)
return HttpResponseRedirect(url_page(p.rel_path))
return render(request, 'pages/page_rename.html', context=context)
else:
messages.permission_denied_msg_page(request, rel_path)
return HttpResponseRedirect(url_page(rel_path))
def search(request):
context = Context(request) # needs to be executed first because of time mesurement
2024-10-09 09:57:05 +02:00
#
search_txt = get_search_query(request)
sr = whoosh_search(search_txt)
if sr is None:
django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
2024-10-09 09:57:05 +02:00
sr = []
pl = page_list(request, [page_wrapped(request, rel_path) for rel_path in set(sr)])
2024-10-09 09:57:05 +02:00
#
context_adaption(
context,
request,
title=_("Searchresults"),
page_content=mycreole.render_simple(pl.creole_list())
)
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)