From 77dfeb42995a7f08c4c94f257ab44681d83c9e02 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 23 Mar 2018 00:33:39 -0700 Subject: [PATCH] Add more _assert methods to hide clutter. --- etiquette/photodb.py | 46 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 0b44133..9e0017d 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -152,6 +152,14 @@ class PDBPhotoMixin: super().__init__() 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): return self.get_thing_by_id('photo', id) @@ -216,12 +224,7 @@ class PDBPhotoMixin: raise FileNotFoundError(filepath.absolute_path) if not allow_duplicates: - try: - existing = self.get_photo_by_path(filepath) - except exceptions.NoSuchPhoto: - pass - else: - raise exceptions.PhotoExists(existing) + self._assert_no_such_photo(filepath=filepath) self.log.debug('New Photo: %s', filepath.absolute_path) author_id = self.get_user_id_or_none(author) @@ -772,6 +775,14 @@ class PDBTagMixin: super().__init__() 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): ''' 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. ''' tagname = self.normalize_tagname(tagname) - try: - existing_tag = self.get_tag_by_name(tagname) - except exceptions.NoSuchTag: - pass - else: - raise exceptions.TagExists(existing_tag) + self._assert_no_such_tag(tagname=tagname) description = objects.Tag.normalize_description(description) self.log.debug('New Tag: %s', tagname) @@ -892,6 +898,14 @@ class PDBUserMixin: super().__init__() 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): if len(password) < 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') self._assert_valid_password(password) - - try: - existing_user = self.get_user(username=username) - except exceptions.NoSuchUser: - pass - else: - raise exceptions.UserExists(existing_user) + self._assert_no_such_user(username=username) self.log.debug('New User: %s', username)