Some more stricter type checks and exceptions.

This commit is contained in:
voussoir 2020-09-19 03:52:42 -07:00
parent 2db1f12bfb
commit 0df556da3f
2 changed files with 14 additions and 4 deletions

View file

@ -22,10 +22,14 @@ from . import helpers
BAIL = sentinel.Sentinel('BAIL')
def normalize_db_row(db_row, table):
if isinstance(db_row, (list, tuple)):
db_row = dict(zip(constants.SQL_COLUMNS[table], db_row))
if isinstance(db_row, dict):
return db_row
if isinstance(db_row, (list, tuple)):
return dict(zip(constants.SQL_COLUMNS[table], db_row))
raise TypeError(f'db_row should be {dict}, {list}, or {tuple}, not {type(db_row)}.')
class ObjectBase:
def __init__(self, photodb):
super().__init__()
@ -581,7 +585,7 @@ class Bookmark(ObjectBase):
url = url.strip()
if not url:
raise ValueError(f'Invalid URL "{url}".')
raise ValueError(f'URL can not be blank.')
return url

View file

@ -1210,10 +1210,16 @@ class PDBUserMixin:
raise exceptions.UserExists(existing_user)
def assert_valid_password(self, password):
if not isinstance(password, bytes):
raise TypeError(f'Password must be {bytes}, not {type(password)}.')
if len(password) < self.config['user']['min_password_length']:
raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length'])
def assert_valid_username(self, username):
if not isinstance(username, str):
raise TypeError(f'Username must be {str}, not {type(username)}.')
if len(username) < self.config['user']['min_username_length']:
raise exceptions.UsernameTooShort(
username=username,
@ -1287,7 +1293,7 @@ class PDBUserMixin:
be workable but fails.
'''
if user_obj_or_id is None:
author_id = None
return None
elif isinstance(user_obj_or_id, objects.User):
if user_obj_or_id.photodb != self: