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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import inspect
  2. import logging
  3. import os
  4. from django.utils.translation import gettext as _
  5. from pages import access
  6. from .help import actionbar as actionbar_add_help
  7. import mycreole
  8. import pages
  9. from themes import empty_entry_parameters, gray_icon_url, color_icon_url
  10. from users.context import menubar as menubar_users
  11. try:
  12. from config import APP_NAME as ROOT_LOGGER_NAME
  13. except ImportError:
  14. ROOT_LOGGER_NAME = 'root'
  15. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  16. ATTACHMENT_UID = 'attachment'
  17. BACK_UID = 'back'
  18. EDIT_UID = 'edit'
  19. HELP_UID = 'help'
  20. INDEX_UID = 'index'
  21. NAVIGATION_ENTRY_UID = 'navigation-%s'
  22. def context_adaption(context, request, **kwargs):
  23. caller_name = inspect.currentframe().f_back.f_code.co_name
  24. try:
  25. context.set_additional_title(kwargs.pop('title'))
  26. except KeyError:
  27. pass # no title in kwargs
  28. menubar(context, request, caller_name, **kwargs)
  29. actionbar(context, request, caller_name, **kwargs)
  30. navigationbar(context, request, caller_name, **kwargs)
  31. for key in kwargs:
  32. context[key] = kwargs[key]
  33. logger.debug("context adapted: %s", repr(context))
  34. def navigationbar(context, request, caller_name, **kwargs):
  35. bar = context[context.NAVIGATIONBAR]
  36. if caller_name == "mycreole-attachments":
  37. next = kwargs.get("next")
  38. if next.count("/") >= 2:
  39. path = next[next.find("/", 2) + 1:]
  40. else:
  41. path = ""
  42. else:
  43. path = kwargs.get("rel_path", "")
  44. while len(path) > 0 and path != os.path.sep:
  45. bar.append_entry(*navigation_entry_parameters(request, path))
  46. path = os.path.dirname(path)
  47. bar.append_entry(*empty_entry_parameters(request))
  48. add_back_menu(request, bar)
  49. finalise_bar(request, bar)
  50. def add_back_menu(request, bar):
  51. bar.append_entry(
  52. BACK_UID, # uid
  53. _('Back'), # name
  54. gray_icon_url(request, 'back.png'), # icon
  55. 'javascript:history.back()', # url
  56. True, # left
  57. False # active
  58. )
  59. def navigation_entry_parameters(request, path):
  60. return (
  61. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  62. '/' + os.path.basename(path), # name
  63. None, # icon
  64. pages.url_page(request, path), # url
  65. False, # left
  66. False # active
  67. )
  68. def menubar(context, request, caller_name, **kwargs):
  69. bar = context[context.MENUBAR]
  70. menubar_users(bar, request)
  71. add_help_menu(request, bar, "current_help_page" in kwargs)
  72. add_index_menu(request, bar, kwargs.get("rel_path", ''))
  73. finalise_bar(request, bar)
  74. def add_help_menu(request, bar, active):
  75. bar.append_entry(
  76. HELP_UID, # uid
  77. _('Help'), # name
  78. color_icon_url(request, 'help.png'), # icon
  79. pages.url_helpview(request, 'main'), # url
  80. True, # left
  81. active # active
  82. )
  83. def add_index_menu(request, bar, rel_path):
  84. bar.append_entry(
  85. INDEX_UID, # uid
  86. _('Index'), # name
  87. color_icon_url(request, 'info.png'), # icon
  88. pages.url_page(request, 'index'), # url
  89. True, # left
  90. request.path == "/page/index" # active
  91. )
  92. def actionbar(context, request, caller_name, **kwargs):
  93. bar = context[context.ACTIONBAR]
  94. if caller_name == 'page':
  95. if access.write_page(request, kwargs["rel_path"]):
  96. add_edit_menu(request, bar, kwargs["rel_path"])
  97. if access.modify_attachment(request, kwargs["rel_path"]):
  98. add_manageupload_menu(request, bar, kwargs['upload_path'])
  99. elif caller_name == 'helpview':
  100. actionbar_add_help(context, request, **kwargs)
  101. finalise_bar(request, bar)
  102. def add_edit_menu(request, bar, rel_path):
  103. bar.append_entry(
  104. EDIT_UID, # uid
  105. _('Edit'), # name
  106. color_icon_url(request, 'edit.png'), # icon
  107. pages.url_edit(request, rel_path), # url
  108. True, # left
  109. False # active
  110. )
  111. def add_manageupload_menu(request, bar, upload_path):
  112. bar.append_entry(
  113. ATTACHMENT_UID, # uid
  114. _("Attachments"), # name
  115. color_icon_url(request, 'upload.png'), # icon
  116. mycreole.url_manage_uploads(request, upload_path), # url
  117. True, # left
  118. False, # active
  119. )
  120. def finalise_bar(request, bar):
  121. if len(bar) == 0:
  122. bar.append_entry(*empty_entry_parameters(request))