Piki is a minimal wiki
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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 menubar(context, request, caller_name, **kwargs):
  48. bar = context[context.MENUBAR]
  49. menubar_users(bar, request)
  50. add_help_menu(request, bar, "current_help_page" in kwargs)
  51. add_index_menu(request, bar, kwargs.get("rel_path", ''))
  52. finalise_bar(request, bar)
  53. def actionbar(context, request, caller_name, **kwargs):
  54. bar = context[context.ACTIONBAR]
  55. if caller_name == 'page':
  56. if access.write_page(request, kwargs["rel_path"]):
  57. add_edit_menu(request, bar, kwargs["rel_path"])
  58. if access.modify_attachment(request, kwargs["rel_path"]):
  59. add_manageupload_menu(request, bar, kwargs['upload_path'])
  60. if access.read_page(request, kwargs["rel_path"]):
  61. add_meta_menu(request, bar, kwargs["rel_path"])
  62. elif caller_name == 'helpview':
  63. actionbar_add_help(context, request, **kwargs)
  64. finalise_bar(request, bar)
  65. def add_back_menu(request, bar):
  66. bar.append_entry(
  67. BACK_UID, # uid
  68. _('Back'), # name
  69. gray_icon_url(request, 'back.png'), # icon
  70. 'javascript:history.back()', # url
  71. True, # left
  72. False # active
  73. )
  74. def navigation_entry_parameters(request, path):
  75. return (
  76. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  77. '/' + os.path.basename(path), # name
  78. None, # icon
  79. pages.url_page(request, path), # url
  80. False, # left
  81. False # active
  82. )
  83. def add_help_menu(request, bar, active):
  84. bar.append_entry(
  85. HELP_UID, # uid
  86. _('Help'), # name
  87. color_icon_url(request, 'help.png'), # icon
  88. pages.url_helpview(request, 'main'), # url
  89. True, # left
  90. active # active
  91. )
  92. def add_index_menu(request, bar, rel_path):
  93. bar.append_entry(
  94. INDEX_UID, # uid
  95. _('Index'), # name
  96. color_icon_url(request, 'edit.png'), # icon
  97. pages.url_page(request, 'index'), # url
  98. True, # left
  99. request.path == "/page/index" # active
  100. )
  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))