Django Library PyGal
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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from django.conf import settings
  2. from django.utils.translation import gettext as _
  3. from .help import actionbar as actionbar_add_help
  4. import inspect
  5. import logging
  6. import os
  7. from themes import gray_icon_url, color_icon_url
  8. from users.context import menubar as menubar_user
  9. from users.context import PROFILE_ENTRY_UID
  10. import pygal
  11. from pygal.models import get_item_by_rel_path
  12. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  13. MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
  14. HELP_UID = 'help-main'
  15. NAVIGATION_ENTRY_UID = 'navigation-main-%s'
  16. HOME_ENTRY_UID = 'home-main'
  17. SEARCH_ENTRY_UID = 'search-main'
  18. BACK_ENTRY_UID = 'back-main'
  19. def context_adaption(context, request, **kwargs):
  20. caller_name = inspect.currentframe().f_back.f_code.co_name
  21. wrapper_instance = kwargs.get("wrapper_instance")
  22. #
  23. menubar(context, request, **kwargs)
  24. navigationbar(context, request, **kwargs)
  25. if wrapper_instance is None:
  26. # Pages without direct connection to an item (e.g. Helpview, ...)
  27. context.set_additional_title(kwargs.get("title", ""))
  28. #
  29. actionbar(context, request, caller_name)
  30. else:
  31. wrapper_instance.context_adaption(context)
  32. # HELP
  33. if pygal.is_helpview(request):
  34. actionbar_add_help(context, request, current_help_page=kwargs.get("current_help_page"))
  35. for key in kwargs:
  36. context[key] = kwargs[key]
  37. logger.debug("context adapted: %s", repr(context))
  38. def navigationbar(context, request, **kwargs):
  39. bar = context[context.NAVIGATIONBAR]
  40. rel_path = kwargs.get("rel_path", '')
  41. #
  42. if pygal.is_favouriteview(request):
  43. if rel_path:
  44. bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
  45. anchor = get_item_by_rel_path(rel_path).name
  46. else:
  47. anchor = None
  48. elif pygal.is_searchview(request):
  49. if rel_path:
  50. bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
  51. anchor = get_item_by_rel_path(rel_path).name
  52. else:
  53. anchor = None
  54. else:
  55. anchor = None
  56. while len(rel_path) > 0 and rel_path != os.path.sep:
  57. bar.append_entry(*navigation_entry_parameters(request, rel_path, anchor))
  58. anchor = get_item_by_rel_path(rel_path).name
  59. rel_path = os.path.dirname(rel_path)
  60. bar.append_entry(*home_entry_parameters(request, anchor))
  61. def menubar(context, request, **kwargs):
  62. bar = context[context.MENUBAR]
  63. rel_path = kwargs.get("rel_path", '')
  64. #
  65. bar.append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
  66. menubar_user(bar, request)
  67. if request.user.is_authenticated:
  68. bar.append_entry(*my_favourites_entry_parameters(request, rel_path))
  69. try:
  70. bar.replace_entry(PROFILE_ENTRY_UID, *profile_entry_parameters(request))
  71. except ValueError:
  72. pass # Profile entry does not exist, so exchange is not needed (e.g. no user is logged in)
  73. def actionbar(context, request, caller_name, **kwargs):
  74. bar = context[context.ACTIONBAR]
  75. bar.append_entry(
  76. BACK_ENTRY_UID, # uid
  77. _('Back'), # name
  78. color_icon_url(request, 'back.png'), # icon
  79. 'javascript:history.back()', # url
  80. True, # left
  81. False # active
  82. )
  83. def profile_entry_parameters(request):
  84. return (
  85. PROFILE_ENTRY_UID, # uid
  86. request.user.username, # name
  87. color_icon_url(request, 'user.png'), # icon
  88. pygal.url_profile(request), # url
  89. False, # left
  90. False # active
  91. )
  92. def home_entry_parameters(request, anchor=None):
  93. if pygal.is_favouriteview(request):
  94. icon = 'favourite.png'
  95. elif pygal.is_searchview(request):
  96. icon = 'search.png'
  97. else:
  98. icon = 'home.png'
  99. return (
  100. HOME_ENTRY_UID,
  101. ':',
  102. gray_icon_url(request, icon),
  103. pygal.url_userview(request, rel_path='', search=pygal.SEARCH_KEEP) + ('#%s' % anchor if anchor is not None else ''),
  104. True,
  105. False
  106. )
  107. def search_entry_parameters(request):
  108. return (
  109. SEARCH_ENTRY_UID,
  110. ':',
  111. gray_icon_url(request, 'search.png'),
  112. pygal.url_search(request),
  113. True,
  114. False
  115. )
  116. def navigation_entry_parameters(request, path, anchor=None):
  117. return (
  118. NAVIGATION_ENTRY_UID % os.path.basename(path), # uid
  119. '/' + os.path.basename(path), # name
  120. None, # icon
  121. pygal.url_userview(request, path, search=pygal.SEARCH_KEEP) + ('#%s' % anchor if anchor is not None else ''), # url
  122. False, # left
  123. False # active
  124. )
  125. def my_favourites_entry_parameters(request, rel_path):
  126. if pygal.is_favouriteview(request) and rel_path != '':
  127. url_addon = '#%s' % get_item_by_rel_path(rel_path).name
  128. else:
  129. url_addon = ''
  130. return (
  131. MY_FAVOURITES_ENTRY_UID, # uid
  132. _('My Favourites'), # name
  133. color_icon_url(request, 'favourite.png'), # icon
  134. pygal.url_favouriteview(request) + url_addon, # url
  135. True, # left
  136. pygal.is_favouriteview(request) # active
  137. )