Piki is a minimal wiki
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

context.py 6.3KB

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