update.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.core.user.update
  4. ~~~~~~~~~~~~~~~~~~~~~~~~
  5. This modules provides services used in updating user details
  6. across FlaskBB.
  7. :copyright: (c) 2014-2018 by the FlaskBB Team.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. from abc import abstractmethod
  11. import attr
  12. from ..._compat import ABC
  13. from ..changesets import empty, is_empty
  14. def _should_assign(current, new):
  15. return not is_empty(new) and current != new
  16. @attr.s(hash=True, cmp=True, repr=True, frozen=True)
  17. class UserDetailsChange(object):
  18. """
  19. Object representing a change user details.
  20. """
  21. birthday = attr.ib(default=empty)
  22. gender = attr.ib(default=empty)
  23. location = attr.ib(default=empty)
  24. website = attr.ib(default=empty)
  25. avatar = attr.ib(default=empty)
  26. signature = attr.ib(default=empty)
  27. notes = attr.ib(default=empty)
  28. def assign_to_user(self, user):
  29. for (name, value) in attr.asdict(self).items():
  30. if _should_assign(getattr(user, name), value):
  31. setattr(user, name, value)
  32. @attr.s(hash=True, cmp=True, repr=False, frozen=True)
  33. class PasswordUpdate(object):
  34. """
  35. Object representing an update to a user's password.
  36. """
  37. old_password = attr.ib()
  38. new_password = attr.ib()
  39. @attr.s(hash=True, cmp=True, repr=True, frozen=True)
  40. class EmailUpdate(object):
  41. """
  42. Object representing a change to a user's email address.
  43. """
  44. # TODO(anr): Change to str.lower once Python2 is dropped
  45. old_email = attr.ib(converter=lambda x: x.lower())
  46. new_email = attr.ib(converter=lambda x: x.lower())
  47. @attr.s(hash=True, cmp=True, repr=True, frozen=True)
  48. class SettingsUpdate(object):
  49. """
  50. Object representing an update to a user's settings.
  51. """
  52. language = attr.ib()
  53. theme = attr.ib()
  54. def assign_to_user(self, user):
  55. for (name, value) in attr.asdict(self).items():
  56. if _should_assign(getattr(user, name), value):
  57. setattr(user, name, value)
  58. class UserSettingsUpdatePostProcessor(ABC):
  59. """
  60. Used to react to a user updating their settings. This post processor
  61. recieves the user that updated their settings and the change set that was
  62. applied to the user. This post processor is called after the update has
  63. been persisted so further changes must be persisted separately.
  64. """
  65. @abstractmethod
  66. def post_process_settings_update(self, user, settings_update):
  67. """
  68. This method is abstract
  69. """
  70. pass