Piki is a minimal wiki
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

context.py 6.6KB

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