Piki is a minimal wiki
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

context.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, 'info.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. elif caller_name == 'helpview':
  97. actionbar_add_help(context, request, **kwargs)
  98. finalise_bar(request, bar)
  99. def add_edit_menu(request, bar, rel_path):
  100. bar.append_entry(
  101. EDIT_UID, # uid
  102. _('Edit'), # name
  103. color_icon_url(request, 'edit.png'), # icon
  104. pages.url_edit(request, rel_path), # url
  105. True, # left
  106. False # active
  107. )
  108. def add_manageupload_menu(request, bar, upload_path):
  109. bar.append_entry(
  110. ATTACHMENT_UID, # uid
  111. _("Attachments"), # name
  112. color_icon_url(request, 'upload.png'), # icon
  113. mycreole.url_manage_uploads(request, upload_path), # url
  114. True, # left
  115. False, # active
  116. )
  117. def finalise_bar(request, bar):
  118. if len(bar) == 0:
  119. bar.append_entry(*empty_entry_parameters(request))