context.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.urls import resolve
  2. from django.utils.translation import gettext_lazy as _
  3. menu = [
  4. {'title':_('Home/Balance'), 'link':'home', 'sel':'house'},
  5. {'title':_('Payment information'), 'link':'test-page', 'sel':'credit-card'},
  6. {'title':_('Payment history'), 'link':'trans','sel':'clock-history'},
  7. {'title':_('Courses'), 'link':'course', 'sel':'education'},
  8. {'title':_('Personal information'), 'link':'test-page', 'sel':'person'},
  9. {'title':_('Service management'), 'link':'test-page', 'sel':'hdd-network'},
  10. {'title':_('My connections'), 'link':'test-page', 'sel':'people'},
  11. {'title':_('Partnership'), 'link':'test-page', 'sel':'sotrud'},
  12. {'title':_('Techsupport'), 'link':'test-page', 'sel':'gear'},
  13. {'title':_('My tickets'), 'link':'tickets', 'sel':'tikets'},
  14. {'title':_('Performers'), 'link':'provider', 'sel':'people',
  15. 'roles':['METASERVICE-ADMIN']},
  16. {'title':_('Service rates'), 'link':'service_tariff', 'sel':'person',
  17. 'roles':['PARTNER-ADMIN']},
  18. {'title':_('Partners'), 'link':'partners', 'sel':'people',
  19. 'roles':['METASERVICE-ADMIN']},
  20. {'title':_('Resources'), 'link':'resource', 'sel':'sotrud',
  21. 'roles':['PARTNER-ADMIN']},
  22. {'title':_('Service services'), 'link':'service_type', 'sel':'hdd-network',
  23. 'roles':['METASERVICE-ADMIN']},
  24. {'title':_('Information about the service'), 'link':'service_information/add/', 'sel':'hdd-network',
  25. 'roles':['METASERVICE-ADMIN']},
  26. {'title':_('Partner Information'), 'link':'partner_information/', 'sel':'person',
  27. 'roles':['PARTNER-ADMIN']},
  28. {'title':_('Rates'), 'link':'service', 'sel':'tikets',
  29. 'roles':['PARTNER-ADMIN']},
  30. {'title':_('User Management'),'link':'user_information', 'sel':'person'}
  31. ]
  32. def get_context(request, page_context) -> dict:
  33. # Получаем роли текущего пользователя
  34. user_roles = set(group.name for group in request.user.groups.all())
  35. is_superuser = request.user.is_superuser
  36. menu_items = []
  37. # Добавляем только те страницы к которым должен быть доступ
  38. for item in menu:
  39. if not item.get('roles') or is_superuser or set(item['roles']) & set(user_roles):
  40. menu_items.append(item)
  41. base_context = {
  42. "title":page_context['title'],
  43. 'url_path':resolve(request.path_info).url_name,
  44. 'menu':menu_items
  45. }
  46. context = dict(list(base_context.items()) + list(page_context.items()))
  47. return context