def delete_user(): # TODO: Ask to delete all related information logger.info("Deleting user") print("Choose what user to delete: ") UserActions.list_users() while True: id_to_delete: str = input("ID of user to delete: ") try: with DBController() as db: number_of_users: int = len(db.fetch_all(table="users")) id_to_delete: int = int(id_to_delete) if 1 <= id_to_delete <= number_of_users: break else: raise InvalidArgumentError( f"User id must be in range 1 - {number_of_users}") except (TypeError, InvalidArgumentError): logger.error("Incorrect choice, try again") while True: with DBController() as db: # TODO: Fix condition choice = input( f"Are you sure you want to delete this user - " f"{db.fetch_user_by_id(row_id=id_to_delete)[0][0]}? " ) or "n" if choice.lower().startswith("y"): correct = True db.delete_user(row_id=id_to_delete) logger.info("User deleted") print("User was deleted from the database") break elif choice.lower().startswith("n"): correct = False break
def print_table(header: list, data: list): if len(header) != len(data[0]): raise InvalidArgumentError( "Header and data must have the same number of columns") table = Texttable() table.add_rows([header] + data) print(table.draw())
def activity_level(self, value): if isinstance(value, int): if 1 <= value <= 5: self._activity_level = value else: raise InvalidArgumentError("Your activity level must fall in interval 1 - 5.") else: raise TypeError("Activity level must be a number.")
def height(self, value): if isinstance(value, int): if 70 < value < 250: self._height = value else: raise InvalidArgumentError("Your height must fall in interval 71 - 249.") else: raise TypeError("Height must be a number.")
def weight(self, value): if isinstance(value, int): if 0 < value < 250: self._age = value else: raise InvalidArgumentError("Your weight must fall in interval 1 - 249.") else: raise TypeError("Weight must be a number.")
def age(self, value): if isinstance(value, int): if 0 < value < 150: self._age = value else: raise InvalidArgumentError("Your age must fall in interval 1 - 149.") else: raise TypeError("Age must be a number.")
def distance(self, value): if isinstance(value, int): self._distance = value else: try: unit_reg = UnitRegistry() self._distance = unit_reg.parse_expression(value).to(unit_reg.meter).magnitude except InvalidArgumentError("Not valid distance"): # TODO: Verify raise
def modify_user(): logger.info("Modifying user") print("Choose what user to modify: ") UserActions.list_users() while True: id_to_modify: str = input("ID of user to modify: ") try: with DBController() as db: number_of_users: int = len(db.fetch_all(table="users")) id_to_modify: int = int(id_to_modify) if 1 <= id_to_modify <= number_of_users: break else: raise InvalidArgumentError( f"User id must be in range 1 - {number_of_users}") except (TypeError, InvalidArgumentError): logger.error("Incorrect choice, try again") logger.info( "Obtaining all the pieces of information about edited user") with DBController() as db: fetched_info: list = db.fetch_user_by_id(row_id=id_to_modify)[0] fields: list = [ 'user_name', 'date_of_birth', 'weight', 'height', 'gender', 'activity_level' ] default_info: dict = { field: info for field, info in zip(fields, fetched_info) } logger.info("Obtaining new info") user_name, date_of_birth, weight, height, gender, activity_level = UserActions._obtain_user_info( default_values=default_info) with DBController() as db: db.update_user(name=user_name, date_born=date_of_birth, weight=weight, height=height, gender=gender, activity=ActivityLevel(activity_level), id_to_modify=id_to_modify) logger.info("Update: success") print("User was updated successfully.")
def duration(self, value): if isinstance(value, int): self. _duration = value elif isinstance(value, str): values = value.split(":") if len(values) == 2: try: self._duration = 60 * int(values[0]) + int(values[1]) except TypeError: raise elif len(values) == 3: try: self._duration = 3600 * int(values[0]) + 60 * int(values[1]) + int(values[2]) except TypeError: raise else: raise InvalidArgumentError("Illegal number of arguments.") else: raise TypeError
def _execute(self, statement: str, *args) -> Cursor: """ Executes statement from :statements: dictionary with given parameters :param statement: statement from :statements" dictionary to be executed :param args: :return: cursor from the connection """ if statement not in self.statements: raise InvalidArgumentError( f"statement argument must be one of the {self.statements.keys()}" ) c = self.connection.cursor() try: c.execute(self.statements[statement], args) except IntegrityError: raise except SQLError as e: logger.error(str(e)) self.connection.close() raise else: self.connection.commit() return c