Piki is a minimal wiki
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

context.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import inspect
  2. import logging
  3. import os
  4. from django.utils.translation import gettext as _
  5. from pages import access
  6. from .help import actionbar as actionbar_add_help
  7. import mycreole
  8. import pages
  9. from themes import empty_entry_parameters, gray_icon_url, color_icon_url
  10. from users.context import menubar as menubar_users
  11. try:
  12. from config import APP_NAME as ROOT_LOGGER_NAME
  13. except ImportError:
  14. ROOT_LOGGER_NAME = 'root'
  15. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  16. ATTACHMENT_UID = 'attachment'
  17. BACK_UID = 'back'
  18. EDIT_UID = 'edit'
  19. HELP_UID = 'help'
  20. INDEX_UID = 'index'
  21. NAVIGATION_ENTRY_UID = 'navigation-%s'
  22. def context_adaption(context, request, **kwargs):
  23. caller_name = inspect.currentframe().f_back.f_code.co_name
  24. try:
  25. context.set_additional_title(kwargs.pop('title'))
  26. except KeyError:
  27. pass # no title in kwargs
  28. menubar(context, request, caller_name, **kwargs)
  29. actionbar(context, request, caller_name, **kwargs)
  30. navigationbar(context, request, caller_name, **kwargs)
  31. for key in kwargs:
  32. context[key] = kwargs[key]
  33. logger.debug("context adapted: %s", repr(context))
  34. def navigationbar(context, request, caller_name, **kwargs):
  35. bar = context[context.NAVIGATIONBAR]
  36. if caller_name == "mycreole-attachments":
  37. next = kwargs.get("next")
  38. if next.count("/") >= 2:
  39. path = next[next.find("/", 2) + 1:]
  40. else:
  41. path = ""
  42. else:
  43. path = kwargs.get("rel_path", "")
  44. while len(path) > 0 and path != os.path.sep:
  45. bar.append_entry(*navigation_entry_parameters(request, path))
  46. path = os.path.dirname(path)
  47. add_back_menu(request, bar)
  48. finalise_bar(request, bar)
  49. def add_back_menu(request, bar):
  50. bar.append_entry(
  51. BACK_UID, # uid
  52. _('Back'), # name
  53. gray_icon_url(request, 'back.png'), # icon
  54. 'javascript:history.back()', # url
  55. True, # left
  56. False # active
  57. )
  58. def navigation_entry_parameters(request, path):
  59. return (
  60. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  61. '/' + os.path.basename(path), # name
  62. None, # icon
  63. pages.url_page(request, path), # url
  64. False, # left
  65. False # active
  66. )
  67. def menubar(context, request, caller_name, **kwargs):
  68. bar = context[context.MENUBAR]
  69. menubar_users(bar, request)
  70. add_help_menu(request, bar, "current_help_page" in kwargs)
  71. add_index_menu(request, bar, kwargs.get("rel_path", ''))
  72. finalise_bar(request, bar)
  73. def add_help_menu(request, bar, active):
  74. bar.append_entry(
  75. HELP_UID, # uid
  76. _('Help'), # name
  77. color_icon_url(request, 'help.png'), # icon
  78. pages.url_helpview(request, 'main'), # url
  79. True, # left
  80. active # active
  81. )
  82. def add_index_menu(request, bar, rel_path):
  83. bar.append_entry(
  84. INDEX_UID, # uid
  85. _('Index'), # name
  86. color_icon_url(request, 'info.png'), # icon
  87. pages.url_page(request, 'index'), # url
  88. True, # left
  89. request.path == "/page/index" # active
  90. )
  91. def actionbar(context, request, caller_name, **kwargs):
  92. bar = context[context.ACTIONBAR]
  93. if caller_name == 'page':
  94. if access.write_page(request, kwargs["rel_path"]):
  95. add_edit_menu(request, bar, kwargs["rel_path"])
  96. if access.modify_attachment(request, kwargs["rel_path"]):
  97. add_manageupload_menu(request, bar, kwargs['upload_path'])
  98. elif caller_name == 'helpview':
  99. actionbar_add_help(context, request, **kwargs)
  100. finalise_bar(request, bar)
  101. def add_edit_menu(request, bar, rel_path):
  102. bar.append_entry(
  103. EDIT_UID, # uid
  104. _('Edit'), # name
  105. color_icon_url(request, 'edit.png'), # icon
  106. pages.url_edit(request, rel_path), # url
  107. True, # left
  108. False # active
  109. )
  110. def add_manageupload_menu(request, bar, upload_path):
  111. bar.append_entry(
  112. ATTACHMENT_UID, # uid
  113. _("Attachments"), # name
  114. color_icon_url(request, 'upload.png'), # icon
  115. mycreole.url_manage_uploads(request, upload_path), # url
  116. True, # left
  117. False, # active
  118. )
  119. def finalise_bar(request, bar):
  120. if len(bar) == 0:
  121. bar.append_entry(*empty_entry_parameters(request))