apps.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from django.apps import AppConfig
  2. from django.utils.translation import gettext_lazy as _
  3. from ..conf import settings
  4. from .pages import user_profile, usercp, users_list
  5. class MisagoUsersConfig(AppConfig):
  6. name = "misago.users"
  7. label = "misago_users"
  8. verbose_name = "Misago Auth"
  9. def ready(self):
  10. from . import signals as _
  11. from .admin import tasks # pylint: disable=unused-import
  12. self.register_default_usercp_pages()
  13. self.register_default_users_list_pages()
  14. self.register_default_user_profile_pages()
  15. def register_default_usercp_pages(self):
  16. def auth_is_not_delegated(request):
  17. return not request.settings.enable_oauth2_client
  18. usercp.add_section(
  19. link="misago:usercp-change-forum-options",
  20. name=_("Forum options"),
  21. component="forum-options",
  22. icon="settings",
  23. )
  24. usercp.add_section(
  25. link="misago:usercp-edit-details",
  26. name=_("Edit details"),
  27. component="edit-details",
  28. icon="person_outline",
  29. )
  30. usercp.add_section(
  31. link="misago:usercp-change-username",
  32. name=_("Change username"),
  33. component="change-username",
  34. icon="card_membership",
  35. visible_if=auth_is_not_delegated,
  36. )
  37. usercp.add_section(
  38. link="misago:usercp-change-email-password",
  39. name=_("Change email or password"),
  40. component="sign-in-credentials",
  41. icon="vpn_key",
  42. visible_if=auth_is_not_delegated,
  43. )
  44. def can_download_own_data(request):
  45. return request.settings.allow_data_downloads
  46. usercp.add_section(
  47. link="misago:usercp-download-data",
  48. name=_("Download data"),
  49. component="download-data",
  50. icon="save_alt",
  51. visible_if=can_download_own_data,
  52. )
  53. def can_delete_own_account(request):
  54. if not auth_is_not_delegated(request):
  55. return False
  56. return request.settings.allow_delete_own_account
  57. usercp.add_section(
  58. link="misago:usercp-delete-account",
  59. name=_("Delete account"),
  60. component="delete-account",
  61. icon="cancel",
  62. visible_if=can_delete_own_account,
  63. )
  64. def register_default_users_list_pages(self):
  65. users_list.add_section(
  66. link="misago:users-active-posters",
  67. component="active-posters",
  68. name=_("Top posters"),
  69. )
  70. def register_default_user_profile_pages(self):
  71. def can_see_names_history(request, profile):
  72. if request.user.is_authenticated:
  73. is_account_owner = profile.pk == request.user.pk
  74. has_permission = request.user_acl["can_see_users_name_history"]
  75. return is_account_owner or has_permission
  76. return False
  77. def can_see_ban_details(request, profile):
  78. if request.user.is_authenticated:
  79. if request.user_acl["can_see_ban_details"]:
  80. from .bans import get_user_ban
  81. return bool(get_user_ban(profile, request.cache_versions))
  82. return False
  83. return False
  84. user_profile.add_section(
  85. link="misago:user-posts", name=_("Posts"), icon="message", component="posts"
  86. )
  87. user_profile.add_section(
  88. link="misago:user-threads",
  89. name=_("Threads"),
  90. icon="forum",
  91. component="threads",
  92. )
  93. user_profile.add_section(
  94. link="misago:user-followers",
  95. name=_("Followers"),
  96. icon="favorite",
  97. component="followers",
  98. )
  99. user_profile.add_section(
  100. link="misago:user-follows",
  101. name=_("Follows"),
  102. icon="favorite_border",
  103. component="follows",
  104. )
  105. user_profile.add_section(
  106. link="misago:user-details",
  107. name=_("Details"),
  108. icon="person_outline",
  109. component="details",
  110. )
  111. user_profile.add_section(
  112. link="misago:username-history",
  113. name=_("Username history"),
  114. icon="card_membership",
  115. component="username-history",
  116. visible_if=can_see_names_history,
  117. )
  118. user_profile.add_section(
  119. link="misago:user-ban",
  120. name=_("Ban details"),
  121. icon="remove_circle_outline",
  122. component="ban-details",
  123. visible_if=can_see_ban_details,
  124. )