Add more _assert methods to hide clutter.
This commit is contained in:
		
							parent
							
								
									229530580f
								
							
						
					
					
						commit
						77dfeb4299
					
				
					 1 changed files with 27 additions and 19 deletions
				
			
		|  | @ -152,6 +152,14 @@ class PDBPhotoMixin: | ||||||
|         super().__init__() |         super().__init__() | ||||||
|         self._photo_cache = cacheclass.Cache() |         self._photo_cache = cacheclass.Cache() | ||||||
| 
 | 
 | ||||||
|  |     def _assert_no_such_photo(self, filepath): | ||||||
|  |         try: | ||||||
|  |             existing = self.get_photo_by_path(filepath) | ||||||
|  |         except exceptions.NoSuchPhoto: | ||||||
|  |             return | ||||||
|  |         else: | ||||||
|  |             raise exceptions.PhotoExists(existing) | ||||||
|  | 
 | ||||||
|     def get_photo(self, id): |     def get_photo(self, id): | ||||||
|         return self.get_thing_by_id('photo', id) |         return self.get_thing_by_id('photo', id) | ||||||
| 
 | 
 | ||||||
|  | @ -216,12 +224,7 @@ class PDBPhotoMixin: | ||||||
|             raise FileNotFoundError(filepath.absolute_path) |             raise FileNotFoundError(filepath.absolute_path) | ||||||
| 
 | 
 | ||||||
|         if not allow_duplicates: |         if not allow_duplicates: | ||||||
|             try: |             self._assert_no_such_photo(filepath=filepath) | ||||||
|                 existing = self.get_photo_by_path(filepath) |  | ||||||
|             except exceptions.NoSuchPhoto: |  | ||||||
|                 pass |  | ||||||
|             else: |  | ||||||
|                 raise exceptions.PhotoExists(existing) |  | ||||||
| 
 | 
 | ||||||
|         self.log.debug('New Photo: %s', filepath.absolute_path) |         self.log.debug('New Photo: %s', filepath.absolute_path) | ||||||
|         author_id = self.get_user_id_or_none(author) |         author_id = self.get_user_id_or_none(author) | ||||||
|  | @ -772,6 +775,14 @@ class PDBTagMixin: | ||||||
|         super().__init__() |         super().__init__() | ||||||
|         self._tag_cache = cacheclass.Cache() |         self._tag_cache = cacheclass.Cache() | ||||||
| 
 | 
 | ||||||
|  |     def _assert_no_such_tag(self, tagname): | ||||||
|  |         try: | ||||||
|  |             existing_tag = self.get_tag_by_name(tagname) | ||||||
|  |         except exceptions.NoSuchTag: | ||||||
|  |             return | ||||||
|  |         else: | ||||||
|  |             raise exceptions.TagExists(existing_tag) | ||||||
|  | 
 | ||||||
|     def get_tag(self, name=None, id=None): |     def get_tag(self, name=None, id=None): | ||||||
|         ''' |         ''' | ||||||
|         Redirect to get_tag_by_id or get_tag_by_name after xor-checking the parameters. |         Redirect to get_tag_by_id or get_tag_by_name after xor-checking the parameters. | ||||||
|  | @ -836,12 +847,7 @@ class PDBTagMixin: | ||||||
|         Register a new tag and return the Tag object. |         Register a new tag and return the Tag object. | ||||||
|         ''' |         ''' | ||||||
|         tagname = self.normalize_tagname(tagname) |         tagname = self.normalize_tagname(tagname) | ||||||
|         try: |         self._assert_no_such_tag(tagname=tagname) | ||||||
|             existing_tag = self.get_tag_by_name(tagname) |  | ||||||
|         except exceptions.NoSuchTag: |  | ||||||
|             pass |  | ||||||
|         else: |  | ||||||
|             raise exceptions.TagExists(existing_tag) |  | ||||||
|         description = objects.Tag.normalize_description(description) |         description = objects.Tag.normalize_description(description) | ||||||
| 
 | 
 | ||||||
|         self.log.debug('New Tag: %s', tagname) |         self.log.debug('New Tag: %s', tagname) | ||||||
|  | @ -892,6 +898,14 @@ class PDBUserMixin: | ||||||
|         super().__init__() |         super().__init__() | ||||||
|         self._user_cache = cacheclass.Cache() |         self._user_cache = cacheclass.Cache() | ||||||
| 
 | 
 | ||||||
|  |     def _assert_no_such_user(self, username): | ||||||
|  |         try: | ||||||
|  |             existing_user = self.get_user(username=username) | ||||||
|  |         except exceptions.NoSuchUser: | ||||||
|  |             return | ||||||
|  |         else: | ||||||
|  |             raise exceptions.UserExists(existing_user) | ||||||
|  | 
 | ||||||
|     def _assert_valid_password(self, password): |     def _assert_valid_password(self, 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']) | ||||||
|  | @ -1012,13 +1026,7 @@ class PDBUserMixin: | ||||||
|             password = password.encode('utf-8') |             password = password.encode('utf-8') | ||||||
| 
 | 
 | ||||||
|         self._assert_valid_password(password) |         self._assert_valid_password(password) | ||||||
| 
 |         self._assert_no_such_user(username=username) | ||||||
|         try: |  | ||||||
|             existing_user = self.get_user(username=username) |  | ||||||
|         except exceptions.NoSuchUser: |  | ||||||
|             pass |  | ||||||
|         else: |  | ||||||
|             raise exceptions.UserExists(existing_user) |  | ||||||
| 
 | 
 | ||||||
|         self.log.debug('New User: %s', username) |         self.log.debug('New User: %s', username) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue