import sqlmodel from .apikey import apikey from .password import password from .builder import builder class user(sqlmodel.SQLModel, table = True): """ This represents user in database. All users has own unique nick and api key. Nick and password is used to login, api key is long, random and used to access database via API. """ id: int | None = sqlmodel.Field(default = None, primary_key = True) nick: str = sqlmodel.Field(index = True, unique = True) password: str = sqlmodel.Field(index = False) apikey: str = sqlmodel.Field(index = True, unique = True) @property def in_database(self) -> bool: """ True when user exists in database. """ return self.id is not None @property def ready(self) -> bool: """ True when all fields are filled. """ if self.nick is None: return False if self.password is None: return False if self.apikey is None: return False return True def __str__(self) -> str: """ This function dump user to string, very usefull for debug. Returns: (str): User as string """ result = "" result = result + "User " if self.id is not None: result = result + "(" + str(self.id) + ")" result = result + "\n" result = result + "Nick: " + self.nick + "\n" result = result + "Password: " + self.password + "\n" result = result + "API key: " + self.apikey + "\n" return result class user_builder(builder, target_type = user): """ This class is responsible for building new user. """ def __init__(self, target: user | None = None) -> None: """ This create new user builder. It can be initialized by already created user. When None create empty user. Parameters: target(user | None): Target to initialize builder with """ super().__init__(target) self._target.apikey = apikey() @property def nick(self) -> str | None: """ return: str | None - Current nick of the user in builder """ return self._target.nick @property def password(self) -> bool: """ return: bool - True when password is set, false if not """ return self._target.password is not None @nick.setter def nick(self, target: str) -> None: """ target: str - New nick for the user """ self._target.nick = target.upper() @password.setter def password(self, target: str) -> None: """ target: str - New password to hash and set """ self._target.password = password(target).result