Some more stricter type checks and exceptions.
This commit is contained in:
		
							parent
							
								
									2db1f12bfb
								
							
						
					
					
						commit
						0df556da3f
					
				
					 2 changed files with 14 additions and 4 deletions
				
			
		|  | @ -22,9 +22,13 @@ from . import helpers | ||||||
| BAIL = sentinel.Sentinel('BAIL') | BAIL = sentinel.Sentinel('BAIL') | ||||||
| 
 | 
 | ||||||
| def normalize_db_row(db_row, table): | def normalize_db_row(db_row, table): | ||||||
|  |     if isinstance(db_row, dict): | ||||||
|  |         return db_row | ||||||
|  | 
 | ||||||
|     if isinstance(db_row, (list, tuple)): |     if isinstance(db_row, (list, tuple)): | ||||||
|         db_row = dict(zip(constants.SQL_COLUMNS[table], db_row)) |         return dict(zip(constants.SQL_COLUMNS[table], db_row)) | ||||||
|     return db_row | 
 | ||||||
|  |     raise TypeError(f'db_row should be {dict}, {list}, or {tuple}, not {type(db_row)}.') | ||||||
| 
 | 
 | ||||||
| class ObjectBase: | class ObjectBase: | ||||||
|     def __init__(self, photodb): |     def __init__(self, photodb): | ||||||
|  | @ -581,7 +585,7 @@ class Bookmark(ObjectBase): | ||||||
|         url = url.strip() |         url = url.strip() | ||||||
| 
 | 
 | ||||||
|         if not url: |         if not url: | ||||||
|             raise ValueError(f'Invalid URL "{url}".') |             raise ValueError(f'URL can not be blank.') | ||||||
| 
 | 
 | ||||||
|         return url |         return url | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1210,10 +1210,16 @@ class PDBUserMixin: | ||||||
|             raise exceptions.UserExists(existing_user) |             raise exceptions.UserExists(existing_user) | ||||||
| 
 | 
 | ||||||
|     def assert_valid_password(self, password): |     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']: |         if len(password) < self.config['user']['min_password_length']: | ||||||
|             raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length']) |             raise exceptions.PasswordTooShort(min_length=self.config['user']['min_password_length']) | ||||||
| 
 | 
 | ||||||
|     def assert_valid_username(self, username): |     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']: |         if len(username) < self.config['user']['min_username_length']: | ||||||
|             raise exceptions.UsernameTooShort( |             raise exceptions.UsernameTooShort( | ||||||
|                 username=username, |                 username=username, | ||||||
|  | @ -1287,7 +1293,7 @@ class PDBUserMixin: | ||||||
|         be workable but fails. |         be workable but fails. | ||||||
|         ''' |         ''' | ||||||
|         if user_obj_or_id is None: |         if user_obj_or_id is None: | ||||||
|             author_id = None |             return None | ||||||
| 
 | 
 | ||||||
|         elif isinstance(user_obj_or_id, objects.User): |         elif isinstance(user_obj_or_id, objects.User): | ||||||
|             if user_obj_or_id.photodb != self: |             if user_obj_or_id.photodb != self: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue