users_collection.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from .user import user
  2. from .user import user_builder
  3. from .user import user_factory
  4. from .exception import in_collection_exception
  5. class users_collection:
  6. """
  7. This is responsible for managing users in the system. It could login new
  8. users, or load them by ApiKey.
  9. """
  10. def __init__(self):
  11. """
  12. This create new collection.
  13. """
  14. self.__by_apikey = dict()
  15. self.__by_nick = dict()
  16. def get_by_nick(self, nick: str) -> user | None:
  17. """
  18. This function return user by nick.
  19. Parameters:
  20. nick (str): Nick of the user to get
  21. Returns:
  22. (user | None): User with that nick, or None
  23. """
  24. if self.exists(nick):
  25. return self.__by_nick[nick]
  26. return None
  27. def exists(self, nick: str) -> bool:
  28. """
  29. This function check that user with given nick exists in the
  30. collection.
  31. Parameters:
  32. nick (str): Nick of the user to check
  33. Returns:
  34. (bool): True when user exists, or False if not
  35. """
  36. return nick in self.__by_nick
  37. @property
  38. def all(self) -> list:
  39. """
  40. This return all users in the collection, as list, like in the
  41. config file.
  42. """
  43. return list(self.__by_apikey.values())
  44. def add(self, target: user) -> object:
  45. """
  46. This add new user to the collection. When ApiKey or login already
  47. exists in the collection, then error had been raised.
  48. Parameters:
  49. target (user): New user to add
  50. Returns:
  51. (object): This object itself
  52. """
  53. if target.apikey in self.__by_apikey:
  54. error = "User with ApiKey \"" + target.apikey + "\" "
  55. error = error + "already in collection."
  56. raise in_collection_exception(error)
  57. if target.nick in self.__by_nick:
  58. error = "User with nick \"" + target.nick + "\" "
  59. error = error + "already in collection."
  60. raise in_collection_exception(error)
  61. self.__by_apikey[target.apikey] = target
  62. self.__by_nick[target.nick] = target
  63. return self
  64. def login(self, nick: str, password: str) -> user | None:
  65. """
  66. This try to login user by nick and password. When nick or password
  67. had been wrong, then None had been returned. When nick and password
  68. are correct, then return that user.
  69. Parameters:
  70. nick (str): Nick of the user
  71. password (str): Password of the user
  72. Returns:
  73. (user): User with that nick and password, if exists
  74. (None): When user with that nick and password not exists
  75. """
  76. if not nick in self.__by_nick:
  77. return None
  78. target = self.__by_nick[nick]
  79. if target.password != password:
  80. return None
  81. return target
  82. def remove(self, target: user) -> object:
  83. """
  84. This function remove target user from collection.
  85. Parameters:
  86. target (user): Target user to remove from collection
  87. Returns:
  88. (object): This object itself
  89. """
  90. exists = target.nick in self.__by_nick
  91. exists = target.apikey in self.__by_apikey
  92. if not exists:
  93. raise in_collection_exception("Can not found required user.")
  94. by_nick = self.__by_nick.copy()
  95. by_apikey = self.__by_apikey.copy()
  96. by_nick.pop(target.nick)
  97. by_apikey.pop(target.apikey)
  98. self.__by_nick = by_nick
  99. self.__by_apikey = by_apikey
  100. return self
  101. def get(self, apikey: str) -> user | None:
  102. """
  103. This try to load user by ApiKey. When user with that ApiKey exists
  104. then return it. When not, return None.
  105. Parameters:
  106. apikey (str): ApiKey of the user to load
  107. Returns:
  108. (user): User with that ApiKey
  109. (None): When user with that ApiKey not exists
  110. """
  111. if not apikey in self.__by_apikey:
  112. return None
  113. return self.__by_apikey[apikey]