Django Library PyGal
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

context.py 5.3KB

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