A simple web application that allows invitation based registration to a matrix instance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

  1. from datetime import datetime
  2. from typing import Tuple
  3. class RegistrationCode:
  4. def __init__(self,
  5. code: str,
  6. creation_time: datetime = datetime.now(),
  7. expiration_time: datetime = None,
  8. usages: int = 0,
  9. max_usages: int = 1):
  10. self.code = code
  11. self.creation_time = creation_time
  12. self.expiration_time = expiration_time
  13. self.usages = usages
  14. self.max_usages = max_usages
  15. @staticmethod
  16. def from_db(db_registration_code: Tuple) -> 'RegistrationCode':
  17. expiration_time = None if db_registration_code[2] is None else datetime.fromisoformat(db_registration_code[2])
  18. return RegistrationCode(
  19. db_registration_code[0],
  20. datetime.fromisoformat(db_registration_code[1]),
  21. expiration_time,
  22. db_registration_code[3],
  23. db_registration_code[4]
  24. )
  25. def is_expired(self):
  26. return self.expiration_time is not None and self.expiration_time < datetime.now()
  27. def has_available_uses(self):
  28. return self.usages < self.max_usages
  29. class RegisteredUser:
  30. def __init__(self,
  31. registration_code: str,
  32. username: str,
  33. registered_time: datetime = datetime.now()):
  34. self.registration_code = registration_code
  35. self.username = username
  36. self.registered_time = registered_time
  37. @staticmethod
  38. def from_db(db_registered_user: Tuple) -> 'RegisteredUser':
  39. return RegisteredUser(
  40. db_registered_user[0],
  41. db_registered_user[1],
  42. datetime.fromisoformat(db_registered_user[2])
  43. )