piki/pages/context.py

169 lines
6.3 KiB
Python

import inspect
import logging
import os
from django.conf import settings
from django.utils.translation import gettext as _
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
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
ATTACHMENT_UID = 'attachment'
BACK_UID = 'back'
EDIT_UID = 'edit'
HELP_UID = 'help'
INDEX_UID = 'index'
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", "")
while len(path) > 0 and path != os.path.sep:
bar.append_entry(*navigation_entry_parameters(request, path))
path = os.path.dirname(path)
bar.append_entry(*empty_entry_parameters(request))
add_back_menu(request, bar)
finalise_bar(request, bar)
def menubar(context, request, caller_name, **kwargs):
bar = context[context.MENUBAR]
menubar_users(bar, request)
add_help_menu(request, bar, "current_help_page" in kwargs)
add_index_menu(request, bar, kwargs.get("rel_path", ''))
finalise_bar(request, bar)
def actionbar(context, request, caller_name, **kwargs):
bar = context[context.ACTIONBAR]
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'])
if access.read_page(request, kwargs["rel_path"]):
add_meta_menu(request, bar, kwargs["rel_path"])
elif caller_name == 'helpview':
actionbar_add_help(context, request, **kwargs)
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
)
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 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
active # active
)
def add_index_menu(request, bar, rel_path):
bar.append_entry(
INDEX_UID, # uid
_('Index'), # name
color_icon_url(request, 'edit.png'), # icon
pages.url_page(request, 'index'), # url
True, # left
request.path == "/page/index" # active
)
def add_edit_menu(request, bar, rel_path):
bar.append_entry(
EDIT_UID, # uid
_('Edit'), # name
color_icon_url(request, 'edit2.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 add_meta_menu(request, bar, rel_path):
if "meta" in request.GET:
bar.append_entry(
EDIT_UID, # uid
_('Page'), # name
color_icon_url(request, 'display.png'), # icon
pages.url_page(request, rel_path), # url
True, # left
False # active
)
else:
bar.append_entry(
EDIT_UID, # uid
_('Meta'), # name
color_icon_url(request, 'info.png'), # icon
pages.url_page(request, rel_path, meta=None), # url
True, # left
False # active
)
def finalise_bar(request, bar):
if len(bar) == 0:
bar.append_entry(*empty_entry_parameters(request))