Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. NAVIGATION_ENTRY_UID = 'navigation-%s'
  21. def context_adaption(context, request, **kwargs):
  22. caller_name = inspect.currentframe().f_back.f_code.co_name
  23. try:
  24. context.set_additional_title(kwargs.pop('title'))
  25. except KeyError:
  26. pass # no title in kwargs
  27. menubar_users(context[context.MENUBAR], request)
  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. path = kwargs.get("rel_path", "")
  37. while len(path) > 0 and path != os.path.sep:
  38. bar.append_entry(*navigation_entry_parameters(request, path))
  39. path = os.path.dirname(path)
  40. add_back_menu(request, bar)
  41. finalise_bar(request, bar)
  42. def add_back_menu(request, bar):
  43. bar.append_entry(
  44. BACK_UID, # uid
  45. _('Back'), # name
  46. gray_icon_url(request, 'back.png'), # icon
  47. 'javascript:history.back()', # url
  48. True, # left
  49. False # active
  50. )
  51. def navigation_entry_parameters(request, path):
  52. return (
  53. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  54. '/' + os.path.basename(path), # name
  55. None, # icon
  56. pages.url_page(request, path), # url
  57. False, # left
  58. False # active
  59. )
  60. def menubar(context, request, caller_name, **kwargs):
  61. bar = context[context.MENUBAR]
  62. add_help_menu(request, bar)
  63. finalise_bar(request, bar)
  64. def add_help_menu(request, bar):
  65. bar.append_entry(
  66. HELP_UID, # uid
  67. _('Help'), # name
  68. color_icon_url(request, 'help.png'), # icon
  69. pages.url_helpview(request, 'main'), # url
  70. True, # left
  71. False # active
  72. )
  73. def actionbar(context, request, caller_name, **kwargs):
  74. bar = context[context.ACTIONBAR]
  75. if caller_name == 'page':
  76. if access.write_page(request, kwargs["rel_path"]):
  77. add_edit_menu(request, bar, kwargs["rel_path"])
  78. if access.modify_attachment(request, kwargs["rel_path"]):
  79. add_manageupload_menu(request, bar, kwargs['upload_path'])
  80. elif caller_name == 'helpview':
  81. actionbar_add_help(context, request, **kwargs)
  82. finalise_bar(request, bar)
  83. def add_edit_menu(request, bar, rel_path):
  84. bar.append_entry(
  85. EDIT_UID, # uid
  86. _('Edit'), # name
  87. color_icon_url(request, 'edit.png'), # icon
  88. pages.url_edit(request, rel_path), # url
  89. True, # left
  90. False # active
  91. )
  92. def add_manageupload_menu(request, bar, upload_path):
  93. bar.append_entry(
  94. ATTACHMENT_UID, # uid
  95. _("Attachments"), # name
  96. color_icon_url(request, 'upload.png'), # icon
  97. mycreole.url_manage_uploads(request, upload_path), # url
  98. True, # left
  99. False, # active
  100. )
  101. def finalise_bar(request, bar):
  102. if len(bar) == 0:
  103. bar.append_entry(*empty_entry_parameters(request))