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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import inspect
  2. import logging
  3. import os
  4. from django.conf import settings
  5. from django.utils.translation import gettext as _
  6. import pages.parameter
  7. from .help import actionbar as actionbar_add_help
  8. import mycreole
  9. import pages
  10. from themes import empty_entry_parameters, gray_icon_url, color_icon_url
  11. from users.context import menubar as menubar_users
  12. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  13. ATTACHMENT_UID = 'attachment'
  14. BACK_UID = 'back'
  15. EDIT_UID = 'edit'
  16. HELP_UID = 'help'
  17. INDEX_UID = 'index'
  18. TREE_UID = 'tree'
  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_nav_links(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 in ['page', 'edit', 'delete', 'rename']:
  62. acc = kwargs["acc"]
  63. if acc.may_write():
  64. add_page_menu(request, bar, kwargs["rel_path"], kwargs.get('is_available', False))
  65. if acc.may_modify_attachment():
  66. add_manageupload_menu(request, bar, kwargs['upload_path'], kwargs.get('is_available', False))
  67. if acc.may_read():
  68. add_meta_menu(request, bar, kwargs["rel_path"], kwargs.get('is_available', False))
  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_page_menu(request, bar, rel_path, is_available):
  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. request.path == pages.url_edit(rel_path) # active
  124. )
  125. if is_available:
  126. bar.append_entry(
  127. EDIT_UID, # uid
  128. _('Rename'), # name
  129. color_icon_url(request, 'shuffle.png'), # icon
  130. pages.url_rename(rel_path), # url
  131. True, # left
  132. request.path == pages.url_rename(rel_path) # active
  133. )
  134. bar.append_entry(
  135. EDIT_UID, # uid
  136. _('Delete'), # name
  137. color_icon_url(request, 'delete.png'), # icon
  138. pages.url_delete(rel_path), # url
  139. True, # left
  140. request.path == pages.url_delete(rel_path) # active
  141. )
  142. def add_manageupload_menu(request, bar, upload_path, is_available):
  143. if is_available:
  144. bar.append_entry(
  145. ATTACHMENT_UID, # uid
  146. _("Attachments"), # name
  147. color_icon_url(request, 'upload.png'), # icon
  148. mycreole.url_manage_uploads(request, upload_path), # url
  149. True, # left
  150. False, # active
  151. )
  152. def add_meta_menu(request, bar, rel_path, is_available):
  153. if is_available:
  154. if "meta" in request.GET:
  155. bar.append_entry(
  156. EDIT_UID, # uid
  157. _('Page'), # name
  158. color_icon_url(request, 'display.png'), # icon
  159. pages.url_page(rel_path), # url
  160. True, # left
  161. False # active
  162. )
  163. else:
  164. bar.append_entry(
  165. EDIT_UID, # uid
  166. _('Meta'), # name
  167. color_icon_url(request, 'info.png'), # icon
  168. pages.url_page(rel_path, meta=None), # url
  169. True, # left
  170. False # active
  171. )
  172. def finalise_bar(request, bar):
  173. if len(bar) == 0:
  174. bar.append_entry(*empty_entry_parameters(request))