Piki is a minimal wiki
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

context.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.access import access_control
  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 in ['page', 'edit', 'delete', 'rename']:
  63. acc = access_control(request, kwargs["rel_path"])
  64. if acc.may_write():
  65. add_page_menu(request, bar, kwargs["rel_path"], kwargs.get('is_available', False))
  66. if acc.may_modify_attachment():
  67. add_manageupload_menu(request, bar, kwargs['upload_path'], kwargs.get('is_available', False))
  68. if acc.may_read():
  69. add_meta_menu(request, bar, kwargs["rel_path"], kwargs.get('is_available', False))
  70. elif caller_name == 'helpview':
  71. actionbar_add_help(context, request, **kwargs)
  72. finalise_bar(request, bar)
  73. def add_back_menu(request, bar):
  74. bar.append_entry(
  75. BACK_UID, # uid
  76. _('Back'), # name
  77. gray_icon_url(request, 'back.png'), # icon
  78. 'javascript:history.back()', # url
  79. True, # left
  80. False # active
  81. )
  82. def navigation_entry_parameters(request, path):
  83. return (
  84. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  85. '/' + os.path.basename(path), # name
  86. None, # icon
  87. pages.url_page(path), # url
  88. False, # left
  89. False # active
  90. )
  91. def add_help_menu(request, bar, active):
  92. bar.append_entry(
  93. HELP_UID, # uid
  94. _('Help'), # name
  95. color_icon_url(request, 'help.png'), # icon
  96. pages.url_helpview('main'), # url
  97. True, # left
  98. active # active
  99. )
  100. def add_nav_links(request, bar, rel_path):
  101. bar.append_entry(
  102. INDEX_UID, # uid
  103. _('Index'), # name
  104. color_icon_url(request, 'edit.png'), # icon
  105. pages.url_page('index'), # url
  106. True, # left
  107. request.path == "/page/index" # active
  108. )
  109. bar.append_entry(
  110. TREE_UID, # uid
  111. _('Tree'), # name
  112. color_icon_url(request, 'tree.png'), # icon
  113. pages.url_page('tree'), # url
  114. True, # left
  115. request.path == "/page/tree" # active
  116. )
  117. def add_page_menu(request, bar, rel_path, is_available):
  118. bar.append_entry(
  119. EDIT_UID, # uid
  120. _('Edit'), # name
  121. color_icon_url(request, 'edit2.png'), # icon
  122. pages.url_edit(rel_path), # url
  123. True, # left
  124. request.path == pages.url_edit(rel_path) # active
  125. )
  126. if is_available:
  127. bar.append_entry(
  128. EDIT_UID, # uid
  129. _('Rename'), # name
  130. color_icon_url(request, 'shuffle.png'), # icon
  131. pages.url_rename(rel_path), # url
  132. True, # left
  133. request.path == pages.url_rename(rel_path) # active
  134. )
  135. bar.append_entry(
  136. EDIT_UID, # uid
  137. _('Delete'), # name
  138. color_icon_url(request, 'delete.png'), # icon
  139. pages.url_delete(rel_path), # url
  140. True, # left
  141. request.path == pages.url_delete(rel_path) # active
  142. )
  143. def add_manageupload_menu(request, bar, upload_path, is_available):
  144. if is_available:
  145. bar.append_entry(
  146. ATTACHMENT_UID, # uid
  147. _("Attachments"), # name
  148. color_icon_url(request, 'upload.png'), # icon
  149. mycreole.url_manage_uploads(request, upload_path), # url
  150. True, # left
  151. False, # active
  152. )
  153. def add_meta_menu(request, bar, rel_path, is_available):
  154. if is_available:
  155. if "meta" in request.GET:
  156. bar.append_entry(
  157. EDIT_UID, # uid
  158. _('Page'), # name
  159. color_icon_url(request, 'display.png'), # icon
  160. pages.url_page(rel_path), # url
  161. True, # left
  162. False # active
  163. )
  164. else:
  165. bar.append_entry(
  166. EDIT_UID, # uid
  167. _('Meta'), # name
  168. color_icon_url(request, 'info.png'), # icon
  169. pages.url_page(rel_path, meta=None), # url
  170. True, # left
  171. False # active
  172. )
  173. def finalise_bar(request, bar):
  174. if len(bar) == 0:
  175. bar.append_entry(*empty_entry_parameters(request))