Django Library Users
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

context.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.urls.base import reverse
  2. from django.utils.translation import gettext as _
  3. from themes import empty_entry_parameters, color_icon_url
  4. from . import url_login, url_logout, url_register, url_profile, url_recover
  5. from . import parameter
  6. ADMIN_ENTRY_UID = 'admin-main'
  7. LOGIN_ENTRY_UID = 'login-main'
  8. LOGOUT_ENTRY_UID = 'logout-main'
  9. PROFILE_ENTRY_UID = 'profile-main'
  10. RECOVER_ENTRY_UID = 'recover-main'
  11. REGISTER_ENTRY_UID = 'register-main'
  12. def context_adaption(context, request, title, **kwargs):
  13. context.set_additional_title(title)
  14. menubar(context[context.MENUBAR], request)
  15. context[context.NAVIGATIONBAR].append_entry(*empty_entry_parameters(request))
  16. actionbar(context[context.ACTIONBAR], request)
  17. for key in kwargs:
  18. context[key] = kwargs[key]
  19. def menubar(bar, request):
  20. if not request.user.is_authenticated:
  21. bar.append_entry(*login_entry_parameters(request))
  22. else:
  23. bar.append_entry(*logout_entry_parameters(request))
  24. if request.user.is_staff or request.user.is_superuser:
  25. bar.append_entry(*admin_entry_parameters(request))
  26. bar.append_entry(*profile_entry_parameters(request))
  27. def actionbar(bar, request):
  28. bar.append_entry(*login_entry_parameters(request, left=True))
  29. if parameter.get(parameter.USERS_PASSWORD_RECOVERY) or request.user.is_superuser:
  30. bar.append_entry(*recover_entry_parameters(request, left=True))
  31. if parameter.get(parameter.USERS_SELF_REGISTRATION) or request.user.is_superuser:
  32. bar.append_entry(*register_entry_parameters(request, left=True))
  33. def login_entry_parameters(request, left=False):
  34. return (
  35. LOGIN_ENTRY_UID, # uid
  36. _('Login'), # name
  37. color_icon_url(request, 'login.png'), # icon
  38. url_login(request), # url
  39. left, # left
  40. request.path == url_login(request, True) # active
  41. )
  42. def recover_entry_parameters(request, left=False):
  43. return (
  44. RECOVER_ENTRY_UID, # uid
  45. _('Recover'), # name
  46. color_icon_url(request, 'recover.png'), # icon
  47. url_recover(request), # url
  48. left, # left
  49. request.path == url_recover(request, True) # active
  50. )
  51. def register_entry_parameters(request, left=False):
  52. return (
  53. REGISTER_ENTRY_UID, # uid
  54. _('Register'), # name
  55. color_icon_url(request, 'register.png'), # icon
  56. url_register(request), # url
  57. left, # left
  58. request.path == url_register(request, True) # active
  59. )
  60. def logout_entry_parameters(request):
  61. return (
  62. LOGOUT_ENTRY_UID, # uid
  63. _('Logout'), # name
  64. color_icon_url(request, 'logout.png'), # icon
  65. url_logout(request), # url
  66. False, # left
  67. False, # active
  68. )
  69. def admin_entry_parameters(request):
  70. return (
  71. ADMIN_ENTRY_UID, # uid
  72. _('Administration'), # name
  73. color_icon_url(request, 'admin.png'), # icon
  74. reverse('admin:index'), # url
  75. False, # left
  76. False # active
  77. )
  78. def profile_entry_parameters(request):
  79. return (
  80. PROFILE_ENTRY_UID, # uid
  81. request.user.username, # name
  82. color_icon_url(request, 'user.png'), # icon
  83. url_profile(request), # url
  84. False, # left
  85. request.path == url_profile(request, True) # active
  86. )