Skip to content

Commit

Permalink
Turn check_length into a utility method
Browse files Browse the repository at this point in the history
Instead of making it a staticmethod of BaseModel
Also fix a typo in BaseModel doc
  • Loading branch information
rtzll committed Oct 16, 2016
1 parent ca80e9e commit a8ded05
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
USERNAME_REGEX = re.compile(r'^\S+$')


class BaseModel:
"""Base for all models, providing save and delte methods."""
def check_length(attribute, length):
"""Checks the attribute's length."""
try:
return bool(attribute) and len(attribute) <= length
except:
return False

@staticmethod
def _check_length(attribute, length):
try:
return bool(attribute) and len(attribute) <= length
except:
return False

class BaseModel:
"""Base for all models, providing providing save and delete methods."""

def __commit(self):
"""Commits the current db.session, does rollback on failure."""
Expand Down Expand Up @@ -66,18 +67,17 @@ def __repr__(self):

@staticmethod
def is_valid_username(username):
is_valid_length = BaseModel._check_length(username, 64)
is_valid_length = check_length(username, 64)
return is_valid_length and bool(USERNAME_REGEX.match(username))

@staticmethod
def is_valid_email(email):
is_valid_length = BaseModel._check_length(email, 64)
return is_valid_length and bool(EMAIL_REGEX.match(email))
return check_length(email, 64) and bool(EMAIL_REGEX.match(email))

@staticmethod
def is_valid_password(passwd):
passwd_hash = generate_password_hash(passwd)
return bool(passwd) and BaseModel._check_length(passwd_hash, 128)
return bool(passwd) and check_length(passwd_hash, 128)

@property
def password(self):
Expand Down Expand Up @@ -143,7 +143,7 @@ def __repr__(self):

@staticmethod
def is_valid_title(list_title):
return BaseModel._check_length(list_title, 128)
return check_length(list_title, 128)

def change_title(self, new_title):
self.title = new_title
Expand Down

0 comments on commit a8ded05

Please sign in to comment.