piki/pages/context.py

149 lines
5.3 KiB
Python
Raw Normal View History

import inspect
import logging
2024-10-05 22:13:30 +02:00
import os
from django.utils.translation import gettext as _
2024-10-05 16:22:40 +02:00
from pages import access
from .help import actionbar as actionbar_add_help
import mycreole
import pages
from themes import empty_entry_parameters, gray_icon_url, color_icon_url
from users.context import menubar as menubar_users
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
ATTACHMENT_UID = 'attachment'
BACK_UID = 'back'
2024-10-05 16:22:40 +02:00
EDIT_UID = 'edit'
HELP_UID = 'help'
2024-10-07 21:52:46 +02:00
INDEX_UID = 'index'
2024-10-05 22:13:30 +02:00
NAVIGATION_ENTRY_UID = 'navigation-%s'
def context_adaption(context, request, **kwargs):
caller_name = inspect.currentframe().f_back.f_code.co_name
try:
context.set_additional_title(kwargs.pop('title'))
except KeyError:
pass # no title in kwargs
menubar(context, request, caller_name, **kwargs)
actionbar(context, request, caller_name, **kwargs)
navigationbar(context, request, caller_name, **kwargs)
for key in kwargs:
context[key] = kwargs[key]
logger.debug("context adapted: %s", repr(context))
def navigationbar(context, request, caller_name, **kwargs):
bar = context[context.NAVIGATIONBAR]
if caller_name == "mycreole-attachments":
next = kwargs.get("next")
if next.count("/") >= 2:
path = next[next.find("/", 2) + 1:]
else:
path = ""
else:
path = kwargs.get("rel_path", "")
2024-10-05 22:13:30 +02:00
while len(path) > 0 and path != os.path.sep:
bar.append_entry(*navigation_entry_parameters(request, path))
path = os.path.dirname(path)
add_back_menu(request, bar)
finalise_bar(request, bar)
def add_back_menu(request, bar):
bar.append_entry(
BACK_UID, # uid
_('Back'), # name
gray_icon_url(request, 'back.png'), # icon
'javascript:history.back()', # url
True, # left
False # active
)
2024-10-05 22:13:30 +02:00
def navigation_entry_parameters(request, path):
return (
NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
'/' + os.path.basename(path), # name
None, # icon
pages.url_page(request, path), # url
False, # left
False # active
)
def menubar(context, request, caller_name, **kwargs):
bar = context[context.MENUBAR]
2024-10-07 00:44:22 +02:00
menubar_users(bar, request)
2024-10-07 21:52:46 +02:00
add_help_menu(request, bar, "current_help_page" in kwargs)
add_index_menu(request, bar, kwargs.get("rel_path", ''))
finalise_bar(request, bar)
2024-10-07 21:52:46 +02:00
def add_help_menu(request, bar, active):
bar.append_entry(
HELP_UID, # uid
_('Help'), # name
color_icon_url(request, 'help.png'), # icon
pages.url_helpview(request, 'main'), # url
True, # left
2024-10-07 21:52:46 +02:00
active # active
)
def add_index_menu(request, bar, rel_path):
bar.append_entry(
INDEX_UID, # uid
_('Index'), # name
color_icon_url(request, 'info.png'), # icon
pages.url_page(request, 'index'), # url
True, # left
request.path == "/page/index" # active
)
def actionbar(context, request, caller_name, **kwargs):
bar = context[context.ACTIONBAR]
2024-10-05 16:22:40 +02:00
if caller_name == 'page':
if access.write_page(request, kwargs["rel_path"]):
add_edit_menu(request, bar, kwargs["rel_path"])
if access.modify_attachment(request, kwargs["rel_path"]):
add_manageupload_menu(request, bar, kwargs['upload_path'])
elif caller_name == 'helpview':
actionbar_add_help(context, request, **kwargs)
finalise_bar(request, bar)
2024-10-05 16:22:40 +02:00
def add_edit_menu(request, bar, rel_path):
bar.append_entry(
EDIT_UID, # uid
_('Edit'), # name
color_icon_url(request, 'edit.png'), # icon
pages.url_edit(request, rel_path), # url
True, # left
False # active
)
def add_manageupload_menu(request, bar, upload_path):
bar.append_entry(
ATTACHMENT_UID, # uid
_("Attachments"), # name
color_icon_url(request, 'upload.png'), # icon
mycreole.url_manage_uploads(request, upload_path), # url
True, # left
False, # active
)
def finalise_bar(request, bar):
if len(bar) == 0:
bar.append_entry(*empty_entry_parameters(request))