def __init__(self, title, description='', owner=None): # test to ensure a user is never None if owner: if utilities.check_type(owner, User): self.owner = owner if self.owner: if utilities.check_type(title, str): self.title = title if utilities.check_type(description, str): self.description = description else: raise ValueError('Invalid arguments')
def __init__(self, name, email, password, username): if utilities.check_type( name, str, error_string="A user's name can only be a string"): self.name = name if utilities.check_email_format(email): self.email = email if utilities.check_password_format(password): self.password = Bcrypt().generate_password_hash(password).decode() if utilities.check_type( username, str, error_string="A user's username can only be a string"): self.username = username
def __init__(self, name, quantity=0, unit='', parent_list=None): # , parent_list=None): # test that an item is never created parent_list = None if parent_list: if utilities.check_type(parent_list, GroceryList): self.parent_list = parent_list if self.parent_list: if utilities.check_type(name, str): self.name = name if utilities.check_type(quantity, float, int): self.quantity = float(quantity) if utilities.check_type(unit, str): self.unit = unit else: raise ValueError('Invalid arguments')
def set_username(self, username): if ' ' in username: raise ValueError('Username should have no space') if utilities.check_type( username, str, error_string="A user's username can only be a string"): self.username = username db.session.commit()
def check_blacklist(token): """ Check whether the token exists in the blacklist """ if utilities.check_type(token, str): result = BlacklistToken.query.filter_by(token=token).first() if result: return True else: return False
def get_grocery_list_by_title(self, title): """ Get the shopping list with the given title if it belongs to the user """ shopping_list = None if utilities.check_type(title, str): shopping_list = GroceryList.query.filter_by(title=title, owner=self).first() return shopping_list
def __init__(self, token): if isinstance(token, bytes): # convert the bytes to string token = token.decode('utf-8') if utilities.check_type(token, str): # attempt to decode it user_id = User.decode_token(token) if not isinstance(user_id, str): self.token = token else: raise ValueError('The token should be valid') self.blacklisted_on = datetime.now()
def set_quantity(self, quantity): if utilities.check_type(quantity, float, int): self.quantity = float(quantity) db.session.commit()
def set_unit(self, unit): if utilities.check_type(unit, str): self.unit = unit db.session.commit()
def set_name(self, name): if utilities.check_type(name, str): self.name = name db.session.commit()
def get_grocery_item_by_name(self, name): grocery_item = None if utilities.check_type(name, str): grocery_list = GroceryList.query.filter_by( name=name, parent_list=self).first() return grocery_item
def add_item(self, item_name): if utilities.check_type(item_name, str): grocery_item = GroceryItem(item_name, parent_list=self) grocery_item.save() return GroceryItem.query.filter_by(name=item_name, parent_list=self).first()
def set_description(self, description): if utilities.check_type(description, str): self.description = description db.session.commit()
def set_title(self, title): if utilities.check_type(title, str): self.title = title db.session.commit()
def set_name(self, name): if utilities.check_type( name, str, error_string="A user's name can only be a string"): self.name = name db.session.commit()