users_app.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from .user import user
  2. from .user import user_factory
  3. from .user import user_builder
  4. from .users_collection import users_collection
  5. from .app_route import app_route
  6. class users_app(app_route):
  7. def __init__(self, collection: users_collection) -> None:
  8. self.__collection = collection
  9. def login(self, sended: dict) -> dict:
  10. self._require(sended, "nick", "password")
  11. nick = sended["nick"]
  12. password = sended["password"]
  13. target = self.collection.login(nick, password)
  14. if target is None:
  15. return self._fail("Bad nick or password.")
  16. return self._success(apikey = target.apikey)
  17. def get(self, sended: dict) -> dict:
  18. self._require(sended, "apikey")
  19. apikey = sended["apikey"]
  20. target = self.collection.get(apikey)
  21. if target is None:
  22. return self._fail("User with that ApiKey not exists.")
  23. return self._success(
  24. apikey = target.apikey,
  25. nick = target.nick
  26. )
  27. @property
  28. def collection(self) -> users_collection:
  29. return self.__collection