plugins.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.user.plugins
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Plugin implementations for the FlaskBB user module.
  6. :copyright: (c) 2018 the FlaskBB Team
  7. :license: BSD, see LICENSE for details
  8. """
  9. from itertools import chain
  10. from flask_babelplus import gettext as _
  11. from pluggy import HookimplMarker
  12. from ..display.navigation import NavigationLink
  13. from .models import User
  14. from .services.validators import (
  15. CantShareEmailValidator,
  16. EmailsMustBeDifferent,
  17. OldEmailMustMatch,
  18. OldPasswordMustMatch,
  19. PasswordsMustBeDifferent,
  20. ValidateAvatarURL,
  21. )
  22. impl = HookimplMarker("flaskbb")
  23. @impl(hookwrapper=True, tryfirst=True)
  24. def flaskbb_tpl_profile_settings_menu():
  25. """
  26. Flattens the lists that come back from the hook
  27. into a single iterable that can be used to populate
  28. the menu
  29. """
  30. results = [
  31. (None, "Account Settings"),
  32. ("user.settings", "General Settings"),
  33. ("user.change_user_details", "Change User Details"),
  34. ("user.change_email", "Change E-Mail Address"),
  35. ("user.change_password", "Change Password"),
  36. ]
  37. outcome = yield
  38. outcome.force_result(chain(results, *outcome.get_result()))
  39. @impl(hookwrapper=True, tryfirst=True)
  40. def flaskbb_tpl_profile_links(user):
  41. results = [
  42. NavigationLink(
  43. endpoint="user.profile",
  44. name=_("Overview"),
  45. icon="fa fa-home",
  46. urlforkwargs={"username": user.username},
  47. ),
  48. NavigationLink(
  49. endpoint="user.view_all_topics",
  50. name=_("Topics"),
  51. icon="fa fa-comments",
  52. urlforkwargs={"username": user.username},
  53. ),
  54. NavigationLink(
  55. endpoint="user.view_all_posts",
  56. name=_("Posts"),
  57. icon="fa fa-comment",
  58. urlforkwargs={"username": user.username},
  59. ),
  60. ]
  61. outcome = yield
  62. outcome.force_result(chain(results, *outcome.get_result()))
  63. @impl
  64. def flaskbb_gather_password_validators():
  65. return [OldPasswordMustMatch(), PasswordsMustBeDifferent()]
  66. @impl
  67. def flaskbb_gather_email_validators():
  68. return [OldEmailMustMatch(), EmailsMustBeDifferent(), CantShareEmailValidator(User)]
  69. @impl
  70. def flaskbb_gather_details_update_validators():
  71. return [ValidateAvatarURL()]