Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.py 7.0KB

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