Fix Album.add_associated_directory not letting duplicates.

The docstring and the actual code were disagreeing.
This commit is contained in:
voussoir 2018-07-29 16:30:30 -07:00
parent 30e3aa9c6f
commit fb052a2d56

View file

@ -293,32 +293,22 @@ class Album(ObjectBase, GroupableMixin):
@decorators.required_feature('album.edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
def add_associated_directory(self, filepath, *, commit=True): def add_associated_directory(self, path, *, commit=True):
''' '''
Add a directory from which this album will pull files during rescans. Add a directory from which this album will pull files during rescans.
These relationships are not unique and multiple albums These relationships are not unique and multiple albums
can associate with the same directory if desired. can associate with the same directory if desired.
''' '''
filepath = pathclass.Path(filepath) path = pathclass.Path(path)
if not filepath.is_dir:
raise ValueError(f'{filepath} is not a directory')
try: if not path.is_dir:
existing = self.photodb.get_album_by_path(filepath) raise ValueError(f'{path} is not a directory.')
except exceptions.NoSuchAlbum:
existing = None
if existing is None: if self.has_associated_directory(path):
pass
elif existing == self:
return return
else:
raise exceptions.AlbumExists(filepath)
data = { self.photodb.log.debug('Adding directory %s to %s.', path, self)
'albumid': self.id, data = {'albumid': self.id, 'directory': path.absolute_path}
'directory': filepath.absolute_path,
}
self.photodb.sql_insert(table='album_associated_directories', data=data) self.photodb.sql_insert(table='album_associated_directories', data=data)
if commit: if commit:
@ -448,7 +438,7 @@ class Album(ObjectBase, GroupableMixin):
def has_any_photo(self, recurse=False): def has_any_photo(self, recurse=False):
row = self.photodb.sql_select_one( row = self.photodb.sql_select_one(
'SELECT photoid FROM album_photo_rel WHERE albumid == ? LIMIT 1', 'SELECT 1 FROM album_photo_rel WHERE albumid == ? LIMIT 1',
[self.id] [self.id]
) )
if row is not None: if row is not None:
@ -458,17 +448,26 @@ class Album(ObjectBase, GroupableMixin):
return False return False
def has_any_subalbum_photo(self): def has_any_subalbum_photo(self):
'''
Return True if any descendent album has any photo, ignoring whether
this particular album itself has photos.
'''
return any(child.has_any_photo(recurse=True) for child in self.get_children()) return any(child.has_any_photo(recurse=True) for child in self.get_children())
def has_photo(self, photo): def has_associated_directory(self, path):
if not isinstance(photo, Photo): path = pathclass.Path(path)
raise TypeError(f'`photo` must be of type {Photo}, not {type(photo)}.') row = self.photodb.sql_select_one(
'SELECT 1 FROM album_associated_directories WHERE albumid == ? AND directory == ?',
[self.id, path.absolute_path]
)
return row is not None
rel_row = self.photodb.sql_select_one( def has_photo(self, photo):
row = self.photodb.sql_select_one(
'SELECT 1 FROM album_photo_rel WHERE albumid == ? AND photoid == ?', 'SELECT 1 FROM album_photo_rel WHERE albumid == ? AND photoid == ?',
[self.id, photo.id] [self.id, photo.id]
) )
return rel_row is not None return row is not None
@decorators.required_feature('album.edit') @decorators.required_feature('album.edit')
# GroupableMixin.remove_child already has @transaction. # GroupableMixin.remove_child already has @transaction.