from datetime import datetime from typing import Tuple class RegistrationCode: def __init__(self, code: str, creation_time: datetime = datetime.now(), expiration_time: datetime = None, usages: int = 0, max_usages: int = 1): self.code = code self.creation_time = creation_time self.expiration_time = expiration_time self.usages = usages self.max_usages = max_usages @staticmethod def from_db(db_registration_code: Tuple) -> 'RegistrationCode': expiration_time = None if db_registration_code[2] is None else datetime.fromisoformat(db_registration_code[2]) return RegistrationCode( db_registration_code[0], datetime.fromisoformat(db_registration_code[1]), expiration_time, db_registration_code[3], db_registration_code[4] ) def is_expired(self): return self.expiration_time is not None and self.expiration_time < datetime.now() def has_available_uses(self): return self.usages < self.max_usages class RegisteredUser: def __init__(self, registration_code: str, username: str, registered_time: datetime = datetime.now()): self.registration_code = registration_code self.username = username self.registered_time = registered_time @staticmethod def from_db(db_registered_user: Tuple) -> 'RegisteredUser': return RegisteredUser( db_registered_user[0], db_registered_user[1], datetime.fromisoformat(db_registered_user[2]) )